"""Hermes Telegram Command Center UI."""
from __future__ import annotations

import httpx
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from pathlib import Path

from app.config import settings

router = APIRouter(prefix="/hermes", tags=["hermes"])
BASE_DIR = Path(__file__).resolve().parent.parent.parent
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))

CEO_CHAT_ID = 8859782446
CTO_CHAT_ID = 789036463
CEO_NAME = "Aïssa"
CTO_NAME = "Mo"


@router.get("", response_class=HTMLResponse)
async def hermes_dashboard(request: Request) -> HTMLResponse:
    stats = {"conversations": 0, "messages": 0, "edges": 0, "embeddings": 0}
    conversations = []
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            r = await client.get(f"{settings.TOOLS_API_URL.rstrip('/')}/brain/stats")
            if r.status_code == 200:
                data = r.json()
                stats = data.get("stats") or stats
                conversations_resp = await client.get(
                    f"{settings.TOOLS_API_URL.rstrip('/')}/brain/conversations", params={"limit": 20}
                )
                if conversations_resp.status_code == 200:
                    conversations = conversations_resp.json().get("items") or []
    except Exception:
        pass

    return templates.TemplateResponse(
        "hermes.html",
        {
            "request": request,
            "page_title": "Hermes",
            "stats": stats,
            "conversations": conversations,
            "ceo_chat_id": CEO_CHAT_ID,
            "cto_chat_id": CTO_CHAT_ID,
            "allowed_sites": ["Airbnb", "Booking.com", "DuckDuckGo", "HolidayCheck"],
            "blocked_sites": ["Google", "Vrbo", "Expedia", "Hotels.com", "TUI", "TripAdvisor"],
        },
    )
