# ================================================================
# services/production/app/main.py
# ================================================================
"""
Production Service - FastAPI Application
Production planning and batch management service
"""

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import structlog

from app.core.config import settings
from app.core.database import init_database, get_db_health
from app.api.production import router as production_router
from app.services.production_alert_service import ProductionAlertService

# Configure logging
logger = structlog.get_logger()


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Manage application lifespan events"""
    # Startup
    try:
        await init_database()
        logger.info("Database initialized")
        
        # Initialize alert service
        alert_service = ProductionAlertService(settings)
        await alert_service.start()
        logger.info("Production alert service started")
        
        # Store alert service in app state
        app.state.alert_service = alert_service
        
        logger.info("Production service started successfully")
    except Exception as e:
        logger.error("Failed to initialize production service", error=str(e))
        raise
    
    yield
    
    # Shutdown
    logger.info("Production service shutting down")
    try:
        # Stop alert service
        if hasattr(app.state, 'alert_service'):
            await app.state.alert_service.stop()
            logger.info("Alert service stopped")
    except Exception as e:
        logger.error("Error during shutdown", error=str(e))


# Create FastAPI application
app = FastAPI(
    title=settings.APP_NAME,
    description=settings.DESCRIPTION,
    version=settings.VERSION,
    lifespan=lifespan
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Configure based on environment
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers
app.include_router(production_router, prefix="/api/v1")


@app.get("/health")
async def health_check():
    """Health check endpoint"""
    try:
        db_healthy = await get_db_health()
        
        health_status = {
            "status": "healthy" if db_healthy else "unhealthy",
            "service": settings.SERVICE_NAME,
            "version": settings.VERSION,
            "database": "connected" if db_healthy else "disconnected"
        }
        
        if not db_healthy:
            health_status["status"] = "unhealthy"
        
        return health_status
        
    except Exception as e:
        logger.error("Health check failed", error=str(e))
        return {
            "status": "unhealthy",
            "service": settings.SERVICE_NAME,
            "version": settings.VERSION,
            "error": str(e)
        }


@app.get("/")
async def root():
    """Root endpoint"""
    return {
        "service": settings.APP_NAME,
        "version": settings.VERSION,
        "description": settings.DESCRIPTION,
        "status": "running"
    }


@app.middleware("http")
async def logging_middleware(request: Request, call_next):
    """Add request logging middleware"""
    import time
    
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    
    logger.info("HTTP request processed",
                method=request.method,
                url=str(request.url),
                status_code=response.status_code,
                process_time=round(process_time, 4))
    
    return response


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=settings.DEBUG
    )