import datetime
from typing import TypedDict

from folkugat_web.model import sessions as model

SessionRowTuple = tuple[
    int,  # id
    str,  # date
    str,  # start_time
    str,  # end_time
    str | None,  # venue_name
    str | None,  # venue_url
    str | None,  # notes
    str | None,  # cartell_url
    bool,  # is_live
]


class SessionRowDict(TypedDict):
    id: int | None
    date: str
    start_time: str
    end_time: str
    venue_name: str | None
    venue_url: str | None
    notes: str | None
    cartell_url: str | None
    is_live: bool


def session_to_row(sessio: model.Session) -> SessionRowDict:
    return {
        'id': sessio.id,
        'date': sessio.date.isoformat(),
        'start_time': sessio.start_time.isoformat(),
        'end_time': sessio.end_time.isoformat(),
        'venue_name': sessio.venue.name,
        'venue_url': sessio.venue.url,
        'notes': sessio.notes,
        'cartell_url': sessio.cartell_url,
        'is_live': sessio.is_live,
    }


def row_to_session(row: SessionRowTuple) -> model.Session:
    return model.Session(
        id=row[0],
        date=datetime.date.fromisoformat(row[1]),
        start_time=datetime.time.fromisoformat(row[2]),
        end_time=datetime.time.fromisoformat(row[3]),
        venue=model.SessionVenue(
            name=row[4],
            url=row[5],
        ),
        notes=row[6],
        cartell_url=row[7],
        is_live=row[8],
    )
