# services/sales/app/main.py
"""
Sales Service Main Application
"""

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

from app.core.config import settings
from app.core.database import init_db, close_db
from shared.monitoring import setup_logging, HealthChecker
from shared.monitoring.metrics import setup_metrics_early

# Setup logging first
setup_logging("sales-service", settings.LOG_LEVEL)
logger = structlog.get_logger()

# Global variables for lifespan access
metrics_collector = None
health_checker = None

# Create FastAPI app FIRST
app = FastAPI(
    title="Bakery Sales Service",
    description="Sales data management service for bakery operations",
    version="1.0.0"
)

# Setup metrics BEFORE any middleware and BEFORE lifespan
metrics_collector = setup_metrics_early(app, "sales-service")

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan events"""
    global health_checker
    
    # Startup
    logger.info("Starting Sales Service...")
    
    try:
        # Initialize database
        await init_db()
        logger.info("Database initialized")
        
        # Register custom metrics
        metrics_collector.register_counter("sales_records_created_total", "Total sales records created")
        metrics_collector.register_counter("sales_records_updated_total", "Total sales records updated")
        metrics_collector.register_counter("sales_queries_total", "Sales record queries")
        metrics_collector.register_counter("product_queries_total", "Product catalog queries")
        metrics_collector.register_counter("import_jobs_total", "Data import jobs")
        metrics_collector.register_counter("export_jobs_total", "Data export jobs")
        
        metrics_collector.register_histogram("sales_create_duration_seconds", "Sales record creation duration")
        metrics_collector.register_histogram("sales_query_duration_seconds", "Sales query duration")
        metrics_collector.register_histogram("import_processing_duration_seconds", "Import processing duration")
        metrics_collector.register_histogram("export_generation_duration_seconds", "Export generation duration")
        
        # Setup health checker
        health_checker = HealthChecker("sales-service")
        
        # Add database health check
        async def check_database():
            try:
                from app.core.database import get_db
                from sqlalchemy import text
                async for db in get_db():
                    await db.execute(text("SELECT 1"))
                    return True
            except Exception as e:
                return f"Database error: {e}"
        
        health_checker.add_check("database", check_database, timeout=5.0, critical=True)
        
        # Store health checker in app state
        app.state.health_checker = health_checker
        
        logger.info("Sales Service started successfully")
        
    except Exception as e:
        logger.error(f"Failed to start Sales Service: {e}")
        raise
    
    yield
    
    # Shutdown
    logger.info("Shutting down Sales Service...")
    await close_db()

# Set lifespan AFTER metrics setup
app.router.lifespan_context = lifespan

# CORS middleware (added after metrics setup)
app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.CORS_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers - import router BEFORE sales router to avoid conflicts
from app.api.sales import router as sales_router
from app.api.import_data import router as import_router
app.include_router(import_router, prefix="/api/v1", tags=["import"])
app.include_router(sales_router, prefix="/api/v1", tags=["sales"])

# Health check endpoint
@app.get("/health")
async def health_check():
    """Comprehensive health check endpoint"""
    if health_checker:
        return await health_checker.check_health()
    else:
        return {
            "service": "sales-service",
            "status": "healthy",
            "version": "1.0.0"
        }

# Root endpoint
@app.get("/")
async def root():
    """Root endpoint"""
    return {
        "service": "Sales Service",
        "version": "1.0.0",
        "status": "running",
        "endpoints": {
            "health": "/health",
            "docs": "/docs",
            "sales": "/api/v1/sales",
            "products": "/api/v1/products"
        }
    }

# Exception handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    """Global exception handler with metrics"""
    logger.error(f"Unhandled exception: {exc}", exc_info=True)
    
    # Record error metric if available
    if metrics_collector:
        metrics_collector.increment_counter("errors_total", labels={"type": "unhandled"})
    
    return JSONResponse(
        status_code=500,
        content={"detail": "Internal server error"}
    )