from typing import Annotated

from fastapi import Form, HTTPException, Request
from folkugat_web.api.router import get_router
from folkugat_web.fragments import playlist
from folkugat_web.services import auth
from folkugat_web.services import playlists as playlists_service
from folkugat_web.services.temes import write as temes_service
from folkugat_web.templates import templates

router = get_router()


@router.get("/llista/{playlist_id}")
def page(
    request: Request,
    logged_in: auth.LoggedIn,
    playlist_id: int,
):
    playlist = playlists_service.get_playlist(playlist_id=playlist_id)
    if not playlist:
        raise HTTPException(status_code=404, detail="Could not find playlist")
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "page_title": "Folkugat - Setlist",
            "page_description": playlist.name or "Llista de temes",
            "page_card": None,
            "content": f"/api/content/llista/{playlist_id}",
            "logged_in": logged_in,
        }
    )


@router.get("/api/content/llista/{playlist_id}")
async def contingut(
    request: Request,
    logged_in: auth.LoggedIn,
    playlist_id: int,
):
    return await playlist.pagina(request, playlist_id, logged_in)


@router.get("/api/llista/{playlist_id}/name")
def get_name(
    request: Request,
    logged_in: auth.LoggedIn,
    playlist_id: int,
):
    return playlist.name(request=request, playlist_id=playlist_id, logged_in=logged_in)


@router.get("/api/llista/{playlist_id}/editor/name")
def name_editor(
    request: Request,
    logged_in: auth.RequireLogin,
    playlist_id: int,
):
    return playlist.name_editor(request=request, playlist_id=playlist_id, logged_in=logged_in)


@router.put("/api/llista/{playlist_id}/name")
def set_name(
    request: Request,
    logged_in: auth.RequireLogin,
    playlist_id: int,
    name: Annotated[str, Form()],
):
    _ = playlists_service.update_name(playlist_id=playlist_id, name=name)
    return playlist.name(request=request, playlist_id=playlist_id, logged_in=logged_in)


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


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


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


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


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


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


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


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


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


@router.put("/api/llista/{playlist_id}/set/{set_id}/tema/{entry_id}/score")
def set_tema_score(
    request: Request,
    logged_in: auth.RequireLogin,
    playlist_id: int,
    set_id: int,
    entry_id: int,
    score_id: Annotated[int | None, Form()] = None,
):
    if score_id is not None and score_id < 0:
        score_id = None
    return playlist.set_tema_score(
        request=request,
        logged_in=logged_in,
        playlist_id=playlist_id,
        set_id=set_id,
        entry_id=entry_id,
        score_id=score_id,
    )


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


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


@router.put("/api/llista/{playlist_id}/visible")
def set_visible(request: Request, logged_in: auth.RequireLogin, playlist_id: int):
    new_playlist = playlists_service.set_visibility(playlist_id=playlist_id, hidden=False)
    return playlist.visibility(
        request=request,
        logged_in=logged_in,
        playlist=new_playlist,
    )


@router.put("/api/llista/{playlist_id}/invisible")
def set_invisible(request: Request, logged_in: auth.RequireLogin, playlist_id: int):
    new_playlist = playlists_service.set_visibility(playlist_id=playlist_id, hidden=True)
    return playlist.visibility(
        request=request,
        logged_in=logged_in,
        playlist=new_playlist,
    )
