"""
Dashboard API Route
Provides system overview, service health checks, and configuration info.
"""

import logging
import os
import platform
import sys
import time
from datetime import datetime, timezone

from fastapi import APIRouter
from fastapi.responses import JSONResponse

from config import (
    AI_SUPABASE_URL,
    CONV_SUPABASE_URL,
    DEFAULT_MODEL,
    GOOGLE_API_KEY,
    GROQ_API_KEY,
    LANGFUSE_BASE_URL,
    LANGFUSE_PUBLIC_KEY,
    LANGFUSE_SECRET_KEY,
    MONGODB_DB_NAME,
    MONGODB_URI,
    OPENAI_API_KEY,
    PORT,
    RATE_LIMIT_GUEST,
    RATE_LIMIT_USER,
    REDIS_HOST,
    REDIS_PORT,
    STARROCKS_DB,
    STARROCKS_HOST,
    USE_MONGO_CONVERSATION,
)

logger = logging.getLogger(__name__)
router = APIRouter()

# Track server start time
_SERVER_START_TIME = time.time()

# App version info
APP_VERSION = "2.5.0"
APP_CODENAME = "Canifa AI Stylist"


def _mask_key(key: str | None) -> str:
    """Mask API key for safe display."""
    if not key:
        return "❌ Not Set"
    if len(key) <= 8:
        return "****"
    return f"{key[:4]}...{key[-4:]}"


def _check_service_status(name: str) -> dict:
    """Check service availability based on config presence."""
    checks = {
        "Redis": bool(REDIS_HOST),
        "MongoDB": bool(MONGODB_URI),
        "Langfuse": bool(LANGFUSE_SECRET_KEY and LANGFUSE_PUBLIC_KEY),
        "Supabase (AI)": bool(AI_SUPABASE_URL),
        "Supabase (Conv)": bool(CONV_SUPABASE_URL),
        "StarRocks": bool(STARROCKS_HOST),
        "OpenAI": bool(OPENAI_API_KEY),
        "Google AI": bool(GOOGLE_API_KEY),
        "Groq": bool(GROQ_API_KEY),
    }
    configured = checks.get(name, False)
    return {
        "name": name,
        "configured": configured,
        "status": "✅ Configured" if configured else "❌ Not Configured",
    }


@router.get("/api/dashboard/info", summary="Get dashboard system info")
async def dashboard_info():
    """Return comprehensive system information for the dashboard."""

    # Uptime calculation
    uptime_seconds = int(time.time() - _SERVER_START_TIME)
    hours, remainder = divmod(uptime_seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    uptime_str = f"{hours}h {minutes}m {seconds}s"

    # Service health checks
    services = [
        _check_service_status("Redis"),
        _check_service_status("MongoDB"),
        _check_service_status("Langfuse"),
        _check_service_status("Supabase (AI)"),
        _check_service_status("Supabase (Conv)"),
        _check_service_status("StarRocks"),
        _check_service_status("OpenAI"),
        _check_service_status("Google AI"),
        _check_service_status("Groq"),
    ]

    # Count healthy services
    healthy_count = sum(1 for s in services if s["configured"])
    total_count = len(services)

    # Feedback stats (safe import)
    feedback_stats = {}
    try:
        from common.feedback_tracker import get_feedback_stats
        feedback_stats = get_feedback_stats()
    except Exception as e:
        logger.warning(f"Could not load feedback stats: {e}")
        feedback_stats = {"error": str(e)}

    # Git info
    git_info = {"branch": "unknown", "commit": "unknown"}
    try:
        import subprocess
        git_branch = subprocess.check_output(
            ["git", "rev-parse", "--abbrev-ref", "HEAD"],
            cwd=os.path.dirname(os.path.abspath(__file__)),
            text=True,
            timeout=5,
        ).strip()
        git_commit = subprocess.check_output(
            ["git", "rev-parse", "--short", "HEAD"],
            cwd=os.path.dirname(os.path.abspath(__file__)),
            text=True,
            timeout=5,
        ).strip()
        git_info = {"branch": git_branch, "commit": git_commit}
    except Exception:
        pass

    # Registered API routes
    from server import app as fastapi_app
    routes = []
    for route in fastapi_app.routes:
        if hasattr(route, "methods") and hasattr(route, "path"):
            routes.append({
                "path": route.path,
                "methods": list(route.methods),
                "name": getattr(route, "name", ""),
            })

    return {
        "system": {
            "app_name": APP_CODENAME,
            "app_version": APP_VERSION,
            "python_version": platform.python_version(),
            "os": f"{platform.system()} {platform.release()}",
            "uptime": uptime_str,
            "uptime_seconds": uptime_seconds,
            "server_port": PORT,
            "started_at": datetime.fromtimestamp(_SERVER_START_TIME, tz=timezone.utc).isoformat(),
        },
        "llm": {
            "default_model": DEFAULT_MODEL,
            "openai_key": _mask_key(OPENAI_API_KEY),
            "google_key": _mask_key(GOOGLE_API_KEY),
            "groq_key": _mask_key(GROQ_API_KEY),
        },
        "services": services,
        "services_summary": {
            "healthy": healthy_count,
            "total": total_count,
            "health_pct": round(healthy_count / total_count * 100) if total_count else 0,
        },
        "config": {
            "rate_limit_guest": RATE_LIMIT_GUEST,
            "rate_limit_user": RATE_LIMIT_USER,
            "conversation_storage": "MongoDB" if USE_MONGO_CONVERSATION else "Supabase",
            "mongodb_db": MONGODB_DB_NAME,
            "redis_host": REDIS_HOST or "N/A",
            "redis_port": REDIS_PORT,
            "langfuse_url": LANGFUSE_BASE_URL or "N/A",
            "starrocks_db": STARROCKS_DB or "N/A",
        },
        "git": git_info,
        "feedback": feedback_stats,
        "routes": routes,
        "timestamp": datetime.now(tz=timezone.utc).isoformat(),
    }
