import datetime
from typing import Annotated

from fastapi import Form, Request
from folkugat_web.api.router import get_router
from folkugat_web.fragments import sessions
from folkugat_web.services import auth

router = get_router()


@router.post("/api/sessions/editor/")
def insert_row(request: Request, _: auth.RequireLogin):
    return sessions.sessions_editor_insert_row(request)


@router.get("/api/sessions/editor/{session_id}/")
def editor_row(request: Request, session_id: int, _: auth.RequireLogin):
    return sessions.sessions_editor_row(request, session_id=session_id)


@router.put("/api/sessions/editor/{session_id}/")
def modify_session(
    request: Request, session_id: int,
    _: auth.RequireLogin,
    date: Annotated[datetime.date, Form()],
    start_time: Annotated[datetime.time, Form()],
    end_time: Annotated[datetime.time, Form()],
    venue_name: Annotated[str | None, Form()] = None,
    venue_url: Annotated[str | None, Form()] = None,
):
    session_date = sessions.model.Session(id=session_id, date=date, start_time=start_time, end_time=end_time,
                                          venue=sessions.model.SessionVenue(name=venue_name, url=venue_url))
    return sessions.sessions_editor_post_row(request, session_date)


@router.delete("/api/sessions/editor/{session_id}/")
def delete_date(session_id: int, _: auth.RequireLogin):
    return sessions.sessions_editor_delete_row(session_id)


@router.get("/api/sessions/editor/{session_id}/edita")
def editor_row_editing(request: Request, session_id: int, _: auth.RequireLogin):
    return sessions.sessions_editor_row_editing(request, session_id)
