"""
AI Tools Suite - FastAPI Backend
"""
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

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

from starlette.middleware.sessions import SessionMiddleware

from routers import (
    drift,
    costs,
    security,
    history,
    compare,
    privacy,
    labels,
    estimate,
    audit,
    content,
    bias,
    profitability,
    emergency,
    reports,
    auth,
    eda,
    house_predictor,
)

app = FastAPI(
    title="AI Tools Suite API",
    description="Backend API for AI/ML operational tools",
    version="0.1.0",
    docs_url="/docs",
    redoc_url="/redoc",
)

# CORS configuration - supports environment variable for production domains
cors_origins_env = os.getenv("CORS_ORIGINS", "")
cors_origins = [
    "http://localhost:3000",
    "http://localhost:5173",
    "http://localhost:5174",
    "http://localhost:5175",
]
# Add production domains from environment
if cors_origins_env:
    cors_origins.extend([origin.strip() for origin in cors_origins_env.split(",") if origin.strip()])

app.add_middleware(
    CORSMiddleware,
    allow_origins=cors_origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Session middleware for OAuth state
app.add_middleware(
    SessionMiddleware,
    secret_key=os.getenv("SECRET_KEY", "change-me-in-production"),
)


# Root endpoint
@app.get("/")
async def root():
    return {
        "name": "AI Tools Suite API",
        "version": "0.1.0",
        "docs": "/docs",
        "health": "/api/v1/health",
        "tools": [
            {"name": "Model Drift Monitor", "endpoint": "/api/v1/drift"},
            {"name": "Vendor Cost Tracker", "endpoint": "/api/v1/costs"},
            {"name": "Security Tester", "endpoint": "/api/v1/security"},
            {"name": "Data History Log", "endpoint": "/api/v1/history"},
            {"name": "Model Comparator", "endpoint": "/api/v1/compare"},
            {"name": "Privacy Scanner", "endpoint": "/api/v1/privacy"},
            {"name": "Label Quality Scorer", "endpoint": "/api/v1/labels"},
            {"name": "Inference Estimator", "endpoint": "/api/v1/estimate"},
            {"name": "Data Integrity Audit", "endpoint": "/api/v1/audit"},
            {"name": "Content Performance", "endpoint": "/api/v1/content"},
            {"name": "Safety/Bias Checks", "endpoint": "/api/v1/bias"},
            {"name": "Profitability Analysis", "endpoint": "/api/v1/profitability"},
            {"name": "Emergency Control", "endpoint": "/api/v1/emergency"},
            {"name": "Result Interpretation", "endpoint": "/api/v1/reports"},
            {"name": "EDA Gapminder", "endpoint": "/api/v1/eda"},
        ]
    }


# Health check
@app.get("/api/v1/health")
async def health_check():
    return {"status": "healthy", "version": "0.1.0"}


# Register routers
app.include_router(drift.router, prefix="/api/v1/drift", tags=["Model Drift Monitor"])
app.include_router(costs.router, prefix="/api/v1/costs", tags=["Vendor Cost Tracker"])
app.include_router(security.router, prefix="/api/v1/security", tags=["Security Tester"])
app.include_router(history.router, prefix="/api/v1/history", tags=["Data History Log"])
app.include_router(compare.router, prefix="/api/v1/compare", tags=["Model Comparator"])
app.include_router(privacy.router, prefix="/api/v1/privacy", tags=["Privacy Scanner"])
app.include_router(labels.router, prefix="/api/v1/labels", tags=["Label Quality Scorer"])
app.include_router(estimate.router, prefix="/api/v1/estimate", tags=["Inference Estimator"])
app.include_router(audit.router, prefix="/api/v1/audit", tags=["Data Integrity Audit"])
app.include_router(content.router, prefix="/api/v1/content", tags=["Content Performance"])
app.include_router(bias.router, prefix="/api/v1/bias", tags=["Safety/Bias Checks"])
app.include_router(profitability.router, prefix="/api/v1/profitability", tags=["Profitability Analysis"])
app.include_router(emergency.router, prefix="/api/v1/emergency", tags=["Emergency Control"])
app.include_router(reports.router, prefix="/api/v1/reports", tags=["Result Interpretation"])
app.include_router(auth.router, prefix="/auth", tags=["Authentication"])
app.include_router(eda.router, prefix="/api/v1/eda", tags=["EDA Gapminder"])
app.include_router(house_predictor.router, prefix="/api/v1/house", tags=["House Price Predictor"])


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