"""Self-Correction Dashboard routes.

GET  /self-correction/ui       — HTML dashboard
GET  /self-correction/timeline — HTMX partial: recent event timeline
GET  /self-correction/patterns — HTMX partial: recurring failure patterns
"""

import logging

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

from dashboard.templating import templates
from infrastructure.self_correction import get_corrections, get_patterns, get_stats

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/self-correction", tags=["self-correction"])


@router.get("/ui", response_class=HTMLResponse)
async def self_correction_ui(request: Request):
    """Render the Self-Correction Dashboard."""
    stats = get_stats()
    corrections = get_corrections(limit=20)
    patterns = get_patterns(top_n=10)
    return templates.TemplateResponse(
        request,
        "self_correction.html",
        {
            "stats": stats,
            "corrections": corrections,
            "patterns": patterns,
        },
    )


@router.get("/timeline", response_class=HTMLResponse)
async def self_correction_timeline(request: Request):
    """HTMX partial: recent self-correction event timeline."""
    corrections = get_corrections(limit=30)
    return templates.TemplateResponse(
        request,
        "partials/self_correction_timeline.html",
        {"corrections": corrections},
    )


@router.get("/patterns", response_class=HTMLResponse)
async def self_correction_patterns(request: Request):
    """HTMX partial: recurring failure patterns."""
    patterns = get_patterns(top_n=10)
    stats = get_stats()
    return templates.TemplateResponse(
        request,
        "partials/self_correction_patterns.html",
        {"patterns": patterns, "stats": stats},
    )
