from datetime import date

from fastapi import FastAPI, Query
from sqlmodel import Session, create_engine, select

from .datatypes import ArtmuseumAddress, ArtmuseumTimeLabel
from .db.crud import init_db
from .db.models import ArtmuseumExhibition, PhilharmoniaConcert


sql_engine = create_engine(
    "sqlite:///database.db", connect_args={"check_same_thread": False}
)
app = FastAPI(
    title="Murmansk Culture API",
    # description="",
    version="0.0.1",
    contact={
        "name": "<NAME>",
        "url": "https://github.com/anorlovsky",
        "email": "<EMAIL>",
    },
    redoc_url="/",
    docs_url=None,
)


@app.on_event("startup")
def on_startup():
    init_db(sql_engine)


@app.get(
    "/artmuseum",
    response_model=list[ArtmuseumExhibition],
    description="Возвращает список текущих и ближайших выставок [Мурманского областного художественного музея](https://artmmuseum.ru/)",
)
async def get_artmuseum_exhibitions(
    time: ArtmuseumTimeLabel = Query(
        None,
        description='Вернуть только текущие (`"now"`) или только ближайшие (`"soon"`) выставки',
    )
):
    with Session(sql_engine) as session:
        if time is None:
            stmt = select(ArtmuseumExhibition)
        elif time == ArtmuseumTimeLabel.NOW:
            stmt = select(ArtmuseumExhibition).where(
                ArtmuseumExhibition.start_date <= date.today()
            )
        elif time == ArtmuseumTimeLabel.SOON:
            stmt = select(ArtmuseumExhibition).where(
                ArtmuseumExhibition.start_date > date.today()
            )

        return session.exec(stmt).all()


@app.get(
    "/philharmonia",
    response_model=list[PhilharmoniaConcert],
    description="Возвращает список ближайших концертов [Мурманской областной филармонии](https://www.murmansound.ru)",
)
async def get_philharmonia_concerts():
    with Session(sql_engine) as session:
        return session.exec(select(PhilharmoniaConcert)).all()

fastapi.FastAPI sqlmodel.create_engine fastapi.Query datetime.date.today sqlmodel.select sqlmodel.Session
[((283, 369), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite:///database.db"""'], {'connect_args': "{'check_same_thread': False}"}), "('sqlite:///database.db', connect_args={'check_same_thread': \n    False})\n", (296, 369), False, 'from sqlmodel import Session, create_engine, select\n'), ((377, 557), 'fastapi.FastAPI', 'FastAPI', ([], {'title': '"""Murmansk Culture API"""', 'version': '"""0.0.1"""', 'contact': "{'name': '<NAME>', 'url': 'https://github.com/anorlovsky', 'email': '<EMAIL>'}", 'redoc_url': '"""/"""', 'docs_url': 'None'}), "(title='Murmansk Culture API', version='0.0.1', contact={'name':\n    '<NAME>', 'url': 'https://github.com/anorlovsky', 'email': '<EMAIL>'},\n    redoc_url='/', docs_url=None)\n", (384, 557), False, 'from fastapi import FastAPI, Query\n'), ((978, 1088), 'fastapi.Query', 'Query', (['None'], {'description': '"""Вернуть только текущие (`"now"`) или только ближайшие (`"soon"`) выставки"""'}), '(None, description=\n    \'Вернуть только текущие (`"now"`) или только ближайшие (`"soon"`) выставки\'\n    )\n', (983, 1088), False, 'from fastapi import FastAPI, Query\n'), ((1114, 1133), 'sqlmodel.Session', 'Session', (['sql_engine'], {}), '(sql_engine)\n', (1121, 1133), False, 'from sqlmodel import Session, create_engine, select\n'), ((1859, 1878), 'sqlmodel.Session', 'Session', (['sql_engine'], {}), '(sql_engine)\n', (1866, 1878), False, 'from sqlmodel import Session, create_engine, select\n'), ((1190, 1217), 'sqlmodel.select', 'select', (['ArtmuseumExhibition'], {}), '(ArtmuseumExhibition)\n', (1196, 1217), False, 'from sqlmodel import Session, create_engine, select\n'), ((1919, 1946), 'sqlmodel.select', 'select', (['PhilharmoniaConcert'], {}), '(PhilharmoniaConcert)\n', (1925, 1946), False, 'from sqlmodel import Session, create_engine, select\n'), ((1282, 1309), 'sqlmodel.select', 'select', (['ArtmuseumExhibition'], {}), '(ArtmuseumExhibition)\n', (1288, 1309), False, 'from sqlmodel import Session, create_engine, select\n'), ((1367, 1379), 'datetime.date.today', 'date.today', ([], {}), '()\n', (1377, 1379), False, 'from datetime import date\n'), ((1459, 1486), 'sqlmodel.select', 'select', (['ArtmuseumExhibition'], {}), '(ArtmuseumExhibition)\n', (1465, 1486), False, 'from sqlmodel import Session, create_engine, select\n'), ((1543, 1555), 'datetime.date.today', 'date.today', ([], {}), '()\n', (1553, 1555), False, 'from datetime import date\n')]