import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from backend.api.router import router as router

def create_app() -> FastAPI:
    """Crea y configura la aplicación FastAPI."""
    app = FastAPI(
        title="Backend Modular 🚀",
        description="API modular con endpoints separados por carpetas",
        version="1.0.0",
    )

    # CORS
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # Routers
    app.include_router(router, prefix="/api")

    @app.get("/")
    def root():
        return {"message": "Backend corriendo 🧠🔥"}

    return app


app = create_app()


if __name__ == "__main__":
    # Modo desarrollo con recarga automática
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,     # recarga al cambiar código
        log_level="info"
    )
