import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from routers import chat, personas, sessions, characters, images, translate, debug
from database.db import init_db
from services.persona_seed import seed_default_personas
from services.system_message_migration import migrate_static_system_messages

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")


@asynccontextmanager
async def lifespan(app: FastAPI):
    await init_db()
    await seed_default_personas()
    await migrate_static_system_messages()
    yield


app = FastAPI(title="AI Chat Bot", lifespan=lifespan)

app.include_router(chat.router)
app.include_router(personas.router)
app.include_router(sessions.router)
app.include_router(characters.router)
app.include_router(images.router)
app.include_router(translate.router)
app.include_router(debug.router)

app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/")
async def root():
    return FileResponse("static/index.html")


@app.get("/debug")
async def debug_page():
    return FileResponse("static/debug.html")


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