# src/web/api/v2/main.py

from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
import logging
import time

from .settings import settings
from src.web.api.v2.routers.calibration_data import router as calibration_data_router
from src.web.api.v2.routers.calibration_risk import router as calibration_risk_router
from src.web.api.v2.routers.calibration_strategies import router as calibration_strategies_router
from src.web.api.v2.routers.calibration_optimization import router as calibration_optimization_router

# --------------------------------------------------
# Logging
# --------------------------------------------------
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("tradingbot.api.v2")

# --------------------------------------------------
# Base paths
# --------------------------------------------------
PROJECT_ROOT = settings.project_root
UI_ROOT = PROJECT_ROOT / "src/web/ui/v2"


def create_app() -> FastAPI:
    # --------------------------------------------------
    # FastAPI app
    # --------------------------------------------------
    app = FastAPI(
        title=settings.api_title,
        version=settings.api_version,
    )

    # --------------------------------------------------
    # Middleware: request/response logging
    # --------------------------------------------------
    @app.middleware("http")
    async def log_requests(request: Request, call_next):
        start_time = time.time()
        logger.info("➡️ %s %s", request.method, request.url.path)
        response = await call_next(request)
        elapsed_ms = (time.time() - start_time) * 1000
        logger.info(
            "⬅️ %s %s -> %s (%.1f ms)",
            request.method,
            request.url.path,
            response.status_code,
            elapsed_ms,
        )
        return response

    # -------------------------
    # Templates (UI v2)
    # -------------------------
    templates = Jinja2Templates(
        directory=str(UI_ROOT / "templates")
    )

    # -------------------------
    # Static files (UI v2)
    # -------------------------
    app.mount(
        "/static",
        StaticFiles(directory=str(UI_ROOT / "static")),
        name="static",
    )

    # -------------------------
    # Reports folder (public access)
    # -------------------------
    reports_root = PROJECT_ROOT / "reports"
    reports_root.mkdir(exist_ok=True)

    app.mount(
        "/reports",
        StaticFiles(directory=str(reports_root)),
        name="reports",
    )

    # ==================================================
    # ROUTES — UI ONLY (TEMPORAL)
    # ==================================================

    @app.get("/", response_class=HTMLResponse)
    def trading_dashboard(request: Request):
        return templates.TemplateResponse(
            "pages/trading/dashboard.html",
            {
                "request": request,
                "page": "trading",
            },
        )

    @app.get("/calibration/data", response_class=HTMLResponse)
    def calibration_data(request: Request):
        return templates.TemplateResponse(
            "pages/calibration/calibration_data.html",
            {
                "request": request,
                "page": "calibration",
                "step": 1,
            },
        )
    
    @app.get("/calibration/risk", response_class=HTMLResponse)
    def calibration_risk_page(request: Request):
        return templates.TemplateResponse(
            "pages/calibration/calibration_risk.html",
            {
                "request": request,
                "page": "calibration",
                "step": 2,
            },
        )
    
    @app.get("/calibration/strategies", response_class=HTMLResponse)
    def calibration_risk_page(request: Request):
        return templates.TemplateResponse(
            "pages/calibration/calibration_strategies.html",
            {
                "request": request,
                "page": "calibration",
                "step": 3,
            },
        )
    
    @app.get("/calibration/optimization", response_class=HTMLResponse)
    def calibration_risk_page(request: Request):
        return templates.TemplateResponse(
            "pages/calibration/calibration_optimization.html",
            {
                "request": request,
                "page": "calibration",
                "step": 4,
            },
        )


    # --------------------------------------------------
    # API routers (versionados)
    # --------------------------------------------------
    api_prefix = settings.api_prefix
    app.include_router(calibration_data_router, prefix=api_prefix)
    app.include_router(calibration_risk_router, prefix=api_prefix)
    app.include_router(calibration_strategies_router, prefix=api_prefix)
    app.include_router(calibration_optimization_router, prefix=api_prefix)

    return app


# ASGI app
app = create_app()


