"""HTTP routes for the Remote Office Center dashboard."""

from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from app import config, services
from app.schemas import PhoneHealth, PhoneHealthUpdate

router = APIRouter()
templates = Jinja2Templates(directory=config.TEMPLATES_DIR)


# ==========================================================
# Home (Original UI)
# ==========================================================
@router.get("/", response_class=HTMLResponse)
async def home(request: Request) -> HTMLResponse:
    """Render the original dashboard."""
    return templates.TemplateResponse(
        request=request,
        name="index.html",
        context={
            "desktop_ip": config.DESKTOP_IP,
        },
    )


# ==========================================================
# FRO Dashboard v2
# ==========================================================
@router.get("/dashboard", response_class=HTMLResponse)
async def dashboard(request: Request) -> HTMLResponse:
    """Render the new FRO Dashboard."""
    return templates.TemplateResponse(
        request=request,
        name="dashboard.html",
        context={
            "desktop_ip": config.DESKTOP_IP,
        },
    )


# ==========================================================
# Wake-on-LAN
# ==========================================================
@router.post("/wake")
async def wake_pc() -> dict[str, str]:
    """Send a Wake-on-LAN packet to the office desktop."""
    services.wake_desktop()
    return {"status": "Magic Packet Sent"}


# ==========================================================
# Desktop Status
# ==========================================================
@router.get("/status")
async def status() -> dict[str, bool]:
    """Return the current desktop ping result."""
    return {
        "desktop": services.is_desktop_online()
    }


# ==========================================================
# System Health
# ==========================================================
@router.get("/system")
async def system() -> services.SystemHealth:
    """Return current health metrics."""
    return services.get_system_health()


# ==========================================================
# Android Companion Health
# ==========================================================
@router.get("/phone/health", response_model=PhoneHealth)
async def phone_health() -> PhoneHealth:
    """Return latest phone health."""
    return services.get_phone_health()


@router.put("/phone/health", response_model=PhoneHealth)
async def update_phone_health(
    report: PhoneHealthUpdate,
) -> PhoneHealth:
    """Update phone health report."""
    return services.update_phone_health(report)


# ==========================================================
# RustDesk
# ==========================================================
@router.post("/rustdesk")
async def rustdesk() -> dict[str, str]:
    """Launch RustDesk."""
    services.connect_rustdesk()
    return {"status": "Connecting"}


@router.get("/rustdesk/status")
async def rustdesk_status() -> dict[str, str]:
    """Return RustDesk status."""
    return {"status": "connected"}


# ==========================================================
# NAS Folder
# ==========================================================
@router.post("/open-folder")
async def open_folder() -> dict[str, str]:
    """Open NAS folder."""
    services.open_nas_folder()
    return {"status": "Opened"}


# ==========================================================
# Configuration
# ==========================================================
@router.get("/config")
async def config_endpoint() -> dict[str, str]:
    """Return configuration."""
    return {
        "nas": config.NAS_URL,
    }


# ==========================================================
# Tailscale
# ==========================================================
@router.get("/tailscale")
async def tailscale() -> dict[str, bool] | dict[str, bool | str]:
    """Return Tailscale status."""
    return services.get_tailscale_status()