from contextlib import _AsyncGeneratorContextManager, asynccontextmanager
from typing import Any, Callable
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from core.database import get_engine, Base
from modules import router
import logging

logging.getLogger("passlib").setLevel(logging.ERROR)  # fix bc package logging is broken


def lifespan_factory() -> Callable[[FastAPI], _AsyncGeneratorContextManager[Any]]:
    @asynccontextmanager
    async def lifespan(app: FastAPI):
        # Base.metadata.drop_all(bind=get_engine())
        Base.metadata.create_all(bind=get_engine())
        yield

    return lifespan


lifespan = lifespan_factory()
app = FastAPI(lifespan=lifespan)

app.include_router(router)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:8081", "exp://*", "https://maia.depaoli.id.au"],
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/api/health")
def health():
    return {"status": "ok"}
