from __future__ import annotations

from os import getenv

from fastapi import FastAPI
from dotenv import load_dotenv

from backend.app.api import routes_config, routes_debug, routes_products, routes_scrape
from backend.app.core.logging import logger
from backend.app.core.scheduler import start_scheduler
from backend.app.db.database import Base, engine

load_dotenv()

app = FastAPI(title="suivi_produit")

app.include_router(routes_products.router)
app.include_router(routes_scrape.router)
app.include_router(routes_config.router)
app.include_router(routes_debug.router)


@app.on_event("startup")
def on_startup() -> None:
    Base.metadata.create_all(bind=engine)
    # démarrer le scheduler APScheduler en chargeant la config
    start_scheduler()
    logger.info("Application démarrée (%s)", getenv("APP_ENV", "development"))


@app.get("/health")
def health() -> dict[str, str]:
    # endpoint de santé minimal
    return {"statut": "ok"}


def main() -> FastAPI:
    return app
