"""Hermes health monitor routes.

Exposes the Hermes health monitor via REST API so the dashboard
and external tools can query system status and trigger checks.

Refs: #1073
"""

import logging

from fastapi import APIRouter

from infrastructure.hermes.monitor import hermes_monitor

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/hermes", tags=["hermes"])


@router.get("/status")
async def hermes_status():
    """Return the most recent Hermes health report.

    Returns the cached result from the last background cycle — does not
    trigger a new check.  Use POST /hermes/check to run an immediate check.
    """
    report = hermes_monitor.last_report
    if report is None:
        return {
            "status": "no_data",
            "message": "No health report yet — first cycle pending",
            "seconds_since_last_run": hermes_monitor.seconds_since_last_run,
        }
    return report.to_dict()


@router.post("/check")
async def hermes_check():
    """Trigger an immediate Hermes health check cycle.

    Runs all monitors synchronously and returns the full report.
    Use sparingly — this blocks until all checks complete (~5 seconds).
    """
    report = await hermes_monitor.run_cycle()
    return report.to_dict()
