from fastapi import Request
from fastapi.responses import HTMLResponse
from folkugat_web.model.pagines import Pages
from folkugat_web.services import playlists as playlists_service
from folkugat_web.services import sessions as sessions_service
from folkugat_web.services.temes import search as search_service
from folkugat_web.templates import templates


def pagina(request: Request, session_id: int, logged_in: bool):
    session = sessions_service.get_session(session_id=session_id)
    playlist = playlists_service.get_playlist(session_id=session_id)
    playlist = playlists_service.add_temes_to_playlist(playlist)
    return templates.TemplateResponse(
        "fragments/sessio/pagina.html",
        {
            "request": request,
            "logged_in": logged_in,
            "Pages": Pages,
            "session_id": session_id,
            "session": session,
            "playlist": playlist,
            "date_names": sessions_service.get_date_names,
        }
    )


def add_set(request: Request, session_id: int, logged_in: bool):
    new_set = playlists_service.add_set(session_id=session_id)
    return templates.TemplateResponse(
        "fragments/sessio/set_entry.html",
        {
            "request": request,
            "logged_in": logged_in,
            "new_entry": True,
            "session_id": session_id,
            "set_id": new_set.id,
            "set_entry": new_set,
        }
    )


def get_set(request: Request, session_id: int, set_id: int, logged_in: bool):
    set_entry = playlists_service.get_set(session_id=session_id, set_id=set_id)
    if set_entry:
        return templates.TemplateResponse(
            "fragments/sessio/set_entry.html",
            {
                "request": request,
                "logged_in": logged_in,
                "new_entry": True,
                "session_id": session_id,
                "set_id": set_id,
                "set_entry": set_entry,
            }
        )
    else:
        return HTMLResponse()


def delete_set(session_id: int, set_id: int):
    playlists_service.delete_set(session_id=session_id, set_id=set_id)
    return HTMLResponse()


def add_tema(request: Request, session_id: int, set_id: int, logged_in: bool):
    new_tema = playlists_service.add_tema(session_id=session_id, set_id=set_id)
    playlists_service.add_tema_to_tema_in_set(new_tema)
    return templates.TemplateResponse(
        "fragments/sessio/tema_editor.html",
        {
            "request": request,
            "logged_in": logged_in,
            "session_id": session_id,
            "set_id": set_id,
            "tema_entry": new_tema,
        }
    )


def get_tema(request: Request, session_id: int, set_id: int, entry_id: int, logged_in: bool):
    tema_entry = playlists_service.get_tema(entry_id=entry_id)
    playlists_service.add_tema_to_tema_in_set(tema_entry)
    return templates.TemplateResponse(
        "fragments/sessio/tema_entry.html",
        {
            "request": request,
            "logged_in": logged_in,
            "session_id": session_id,
            "set_id": set_id,
            "tema_entry": tema_entry,
        }
    )


def get_tema_editor(request: Request, session_id: int, set_id: int, entry_id: int, logged_in: bool):
    tema_entry = playlists_service.get_tema(entry_id=entry_id)
    playlists_service.add_tema_to_tema_in_set(tema_entry)
    return templates.TemplateResponse(
        "fragments/sessio/tema_editor.html",
        {
            "request": request,
            "logged_in": logged_in,
            "session_id": session_id,
            "set_id": set_id,
            "tema_entry": tema_entry,
        }
    )


def delete_tema(session_id: int, set_id: int, entry_id: int):
    playlists_service.delete_tema(entry_id=entry_id)
    if not playlists_service.get_set(session_id=session_id, set_id=set_id):
        headers = {
            "HX-Trigger": f"reload-set-{set_id}"
        }
    else:
        headers = {}
    return HTMLResponse(headers=headers)


def busca_tema(
    request: Request,
    session_id: int,
    set_id: int,
    entry_id: int,
    query: str,
):
    results = search_service.busca_temes(
        query=query,
        hidden=True,
        limit=4,
        offset=0,
    )
    return templates.TemplateResponse(
        "fragments/sessio/tema_results.html",
        {
            "request": request,
            "session_id": session_id,
            "set_id": set_id,
            "entry_id": entry_id,
            "results": results,
            "query": query,
        }
    )


def set_tema(request: Request, logged_in: bool, session_id: int, set_id: int, entry_id: int, tema_id: int | None):
    playlists_service.set_tema(session_id=session_id, set_id=set_id, entry_id=entry_id, tema_id=tema_id)
    tema_entry = playlists_service.get_tema(entry_id=entry_id)
    playlists_service.add_tema_to_tema_in_set(tema_entry)
    return templates.TemplateResponse(
        "fragments/sessio/tema_entry.html",
        {
            "request": request,
            "logged_in": logged_in,
            "session_id": session_id,
            "set_id": set_id,
            "tema_entry": tema_entry,
        }
    )
