from fastapi import FastAPI, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from contextlib import asynccontextmanager
import time

from pif_compiler.functions.common_log import get_logger

# Import dei tuoi router
from pif_compiler.api.routes import api_echa, api_cosing, common, api_ingredients, api_esposition, api_orders

# Configurazione logging
logger = get_logger()

# Lifespan events (startup/shutdown) - moderna alternativa a @app.on_event
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    logger.info("🚀 Starting up API...")
    # Qui puoi inizializzare:
    # - Connessioni al database
    
    yield  # L'app gira qui
    
    # Shutdown
    logger.info("👋 Shutting down API...")
    # Qui chiudi le connessioni e fai cleanup
    # - Chiudi DB connections
    # - Salva cache
    # - Cleanup risorse


# Inizializza FastAPI
app = FastAPI(
    title="Comsoguard API",
    description="Central API for Comsoguard services",
    version="0.0.1",
    docs_url="/docs",
    redoc_url="/redoc",
    openapi_url="/openapi.json",
    lifespan=lifespan
)


# ==================== MIDDLEWARE ====================

# 1. CORS - Configura in base alle tue esigenze
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "*"
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# 2. Logging Middleware - Log di tutte le richieste
@app.middleware("http")
async def log_requests(request: Request, call_next):
    start_time = time.time()
    
    # Log della richiesta in arrivo
    logger.info(
        f"📥 {request.method} {request.url.path} - "
        f"Client: {request.client.host}"
    )
    
    # Esegui la richiesta
    response = await call_next(request)
    
    # Calcola il tempo di elaborazione
    process_time = time.time() - start_time
    
    # Log della risposta
    logger.info(
        f"📤 {request.method} {request.url.path} - "
        f"Status: {response.status_code} - "
        f"Time: {process_time:.3f}s"
    )
    
    return response


# ==================== EXCEPTION HANDLERS ====================

# Handler per errori di validazione (422)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    logger.warning(f"❌ Validation error on {request.url.path}: {exc.errors()}")
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content={
            "error": "Validation Error",
            "detail": exc.errors(),
            "body": exc.body
        }
    )


# Handler per errori generici
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
    logger.error(f"💥 Unhandled exception on {request.url.path}: {str(exc)}", exc_info=True)
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={
            "error": "Internal Server Error",
            "message": "Si è verificato un errore imprevisto. Riprova più tardi."
        }
    )


# ==================== ROUTERS ====================

# Include i tuoi router qui

app.include_router(
    api_echa.router,
    prefix="/api/v1",
    tags=["ECHA"]
)

app.include_router(
    api_cosing.router,
    prefix="/api/v1",
    tags=["COSING"]
)

app.include_router(
    common.router,
    prefix="/api/v1",
    tags=["Common"]
)

app.include_router(
    api_ingredients.router,
    prefix="/api/v1",
    tags=["Ingredients"]
)

app.include_router(
    api_esposition.router,
    prefix="/api/v1",
    tags=["Esposition"]
)

app.include_router(
    api_orders.router,
    prefix="/api/v1",
    tags=["Orders"]
)

# ==================== ROOT ENDPOINTS ====================

@app.get("/", tags=["Root"])
async def root():
    """
    Endpoint di benvenuto - mostra info base dell'API
    """
    return {
        "message": "Welcome to Comsoguard API",
        "version": "0.0.1",
        "docs": "/docs",
        "redoc": "/redoc"
    }


@app.get("/health", tags=["Health"])
async def health_check():
    """
    Health check endpoint - utile per monitoring e load balancers
    """
    return {
        "status": "healthy",
        "service": "comsoguard-api",
        "version": "0.0.1"
    }


@app.get("/ping", tags=["Health"])
async def ping():
    """
    Ping endpoint - risposta velocissima per verificare che l'API sia up
    """
    return {"ping": "pong"}


# ==================== RUN SERVER ====================

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "pif_compiler.main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,  # Auto-reload on code changes during development
        log_level="info"
    )