from fastapi import Request
from fastapi.responses import HTMLResponse
from folkugat_web.config import calendari as config
from folkugat_web.model import sessions as model
from folkugat_web.model.pagines import Pages
from folkugat_web.services import sessions as service
from folkugat_web.templates import templates


def sessions_pagina(request, logged_in):
    return templates.TemplateResponse(
        "fragments/sessions/pagina.html",
        {
            "request": request,
            "logged_in": logged_in,
            "Pages": Pages,
            "menu_selected_id": Pages.Sessions,
            "calendar_list_id": config.CALENDAR_LIST_ID,
            "history_list_id": config.HISTORY_LIST_ID,
        }
    )


def sessions_calendari(request: Request, limit: int, logged_in: bool):
    sessions = service.get_next_sessions(limit=limit+1)
    has_more_sessions = len(sessions) > limit
    sessions = sessions[:limit]
    next_session_id = sessions[0].id if sessions else None
    return _sessions_list(
        request=request,
        sessions=sessions,
        has_more_sessions=has_more_sessions,
        paging_config=config.CALENDARI_PAGING_CONFIG,
        list_id=config.CALENDAR_LIST_ID,
        next_session_id=next_session_id,
        get_sessions_url="/api/sessions/upcoming",
        limit=limit,
        logged_in=logged_in
    )


def sessions_historial(request: Request, limit: int, logged_in: bool):
    sessions = service.get_sessions_history(limit=limit+1)
    has_more_sessions = len(sessions) > limit
    sessions = sessions[:limit]
    return _sessions_list(
        request=request,
        sessions=sessions,
        has_more_sessions=has_more_sessions,
        paging_config=config.HISTORY_PAGING_CONFIG,
        list_id=config.HISTORY_LIST_ID,
        get_sessions_url="/api/sessions/history",
        limit=limit,
        logged_in=logged_in
    )


def _sessions_list(request: Request, sessions: list[model.Session], has_more_sessions: bool,
                   paging_config: config.PagingConfig, list_id: str,  get_sessions_url: str,
                   limit: int, logged_in: bool, next_session_id: int | None = None):
    if has_more_sessions:
        more_sessions = limit + paging_config.step
    else:
        more_sessions = None

    if len(sessions) > paging_config.initial_items:
        less_sessions = max(paging_config.initial_items, len(sessions) - paging_config.step)
    else:
        less_sessions = None

    return templates.TemplateResponse(
        "fragments/sessions/session_list.html",
        {
            "request": request,
            "logged_in": logged_in,
            "get_sessions_url": get_sessions_url,
            "sessions": sessions,
            "session_list_id": list_id,
            "next_session_id": next_session_id,
            "more_sessions": more_sessions,
            "less_sessions": less_sessions,
            "limit": limit,
            "date_names": service.get_date_names,
        }
    )


def sessions_editor_row(request: Request, session_date: model.Session | None = None, session_id: int | None = None):
    if session_date is None:
        if session_id is None:
            raise ValueError("Must either give session or session_id")
        session_date = service.get_session(session_id=session_id)
    return templates.TemplateResponse(
        "fragments/sessions/session_row.html",
        {
            "request": request,
            "session": session_date,
            "date_names": service.get_date_names,
            "logged_in": True,
        }
    )


def sessions_editor_row_editing(request, session_id: int):
    session = service.get_session(session_id)
    return templates.TemplateResponse(
        "fragments/sessions/session_row_editing.html",
        {
            "request": request,
            "session": session,
            "date_names": service.get_date_names,
        }
    )


def sessions_editor_insert_row(request, session_date: model.Session | None = None):
    session_date = service.insert_session(session_date or service.new_session())
    return sessions_editor_row(request, session_date=session_date)


def sessions_editor_post_row(request, session_date: model.Session):
    service.set_session(session_date)
    return sessions_editor_row(request, session_date=session_date)


def sessions_editor_delete_row(session_id: int):
    service.delete_session(session_id)
    return HTMLResponse()
