from typing import Annotated

from fastapi import Form, Request
from folkugat_web.api import router
from folkugat_web.fragments import live, sessio
from folkugat_web.services import auth
from folkugat_web.services.temes import write as temes_service
from folkugat_web.templates import templates


@router.get("/sessio/{session_id}")
def page(
    request: Request,
    logged_in: auth.LoggedIn,
    session_id: int,
):
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "page_title": "Folkugat",
            "content": f"/api/content/sessio/{session_id}",
            "logged_in": logged_in,
        }
    )


@router.get("/api/content/sessio/{session_id}")
def contingut(
    request: Request,
    logged_in: auth.LoggedIn,
    session_id: int,
):
    return sessio.pagina(request, session_id, logged_in)


@router.put("/api/sessio/{session_id}/live")
def set_live(
    request: Request,
    _: auth.RequireLogin,
    session_id: int,
):
    return live.start_live_session(request=request, session_id=session_id)


@router.delete("/api/sessio/{session_id}/live")
def stop_live(
    request: Request,
    _: auth.RequireLogin,
    session_id: int,
):
    return live.stop_live_session(request=request, session_id=session_id)


@router.post("/api/sessio/{session_id}/set")
def add_set(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
):
    return sessio.add_set(request=request, session_id=session_id, logged_in=logged_in)


@router.get("/api/sessio/{session_id}/set/{set_id}")
def get_set(
    request: Request,
    logged_in: auth.LoggedIn,
    session_id: int,
    set_id: int,
):
    return sessio.get_set(request=request, session_id=session_id, set_id=set_id, logged_in=logged_in)


@router.delete("/api/sessio/{session_id}/set/{set_id}")
def delete_set(
    _: auth.RequireLogin,
    session_id: int,
    set_id: int,
):
    return sessio.delete_set(session_id=session_id, set_id=set_id)


@router.post("/api/sessio/{session_id}/set/{set_id}")
def add_tema(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
):
    return sessio.add_tema(request=request, session_id=session_id, set_id=set_id, logged_in=logged_in)


@router.get("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}")
def get_tema(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
):
    return sessio.get_tema(
        request=request, session_id=session_id, set_id=set_id, entry_id=entry_id, logged_in=logged_in)


@router.get("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}/editor")
def get_tema_editor(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
):
    return sessio.get_tema_editor(
        request=request, session_id=session_id, set_id=set_id, entry_id=entry_id, logged_in=logged_in)


@router.delete("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}")
def delete_tema(
    _: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
):
    return sessio.delete_tema(session_id=session_id, set_id=set_id, entry_id=entry_id)


@router.get("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}/busca")
def busca_tema(
    request: Request,
    _: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
    query: str,
):
    return sessio.busca_tema(
        request=request,
        session_id=session_id,
        set_id=set_id,
        entry_id=entry_id,
        query=query,
    )


@router.put("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}")
def set_tema(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
    tema_id: Annotated[int, Form()],
):
    return sessio.set_tema(
        request=request,
        logged_in=logged_in,
        session_id=session_id,
        set_id=set_id,
        entry_id=entry_id,
        tema_id=tema_id,
    )


@router.put("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}/unknown")
def set_tema_unknown(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
):
    return sessio.set_tema(
        request=request,
        logged_in=logged_in,
        session_id=session_id,
        set_id=set_id,
        entry_id=entry_id,
        tema_id=None,
    )


@router.post("/api/sessio/{session_id}/set/{set_id}/tema/{entry_id}")
def set_tema_new(
    request: Request,
    logged_in: auth.RequireLogin,
    session_id: int,
    set_id: int,
    entry_id: int,
    title: Annotated[str, Form()],
):
    new_tema = temes_service.create_tema(title=title)
    return sessio.set_tema(
        request=request,
        logged_in=logged_in,
        session_id=session_id,
        set_id=set_id,
        entry_id=entry_id,
        tema_id=new_tema.id,
    )
