"""
Learning API - 學習系統 API
===========================
Phase D-G P0 修正: 新增學習 API 端點

端點:
- GET /api/v1/learning/summary/{anomaly_key} - 學習摘要
- GET /api/v1/learning/recommendation/{anomaly_key} - 修復推薦

版本: v1.0
建立: 2026-03-29 (台北時區)
建立者: Claude Code (Phase D-G P0 修正)

遵循原則:
- Router 只做 HTTP 轉發
- 業務邏輯在 Service 層
- 符合 API 路徑命名規範
"""

import structlog
from fastapi import APIRouter
from pydantic import BaseModel

from src.services.learning_service import get_learning_service

logger = structlog.get_logger(__name__)
router = APIRouter(prefix="/learning", tags=["Learning"])


# =============================================================================
# Response Models
# =============================================================================


class BestAction(BaseModel):
    """最佳動作"""
    action: str
    success_rate: float


class LearningSummaryResponse(BaseModel):
    """學習摘要回應"""
    anomaly_key: str
    total_repair_attempts: int
    overall_success_rate: float
    actions_tried: list[str]
    best_action: BestAction | None
    learning_status: str  # insufficient, learning, sufficient, excellent


class AlternativeAction(BaseModel):
    """替代動作"""
    action: str
    confidence: float
    tier: int


class RecommendationResponse(BaseModel):
    """修復推薦回應"""
    action: str
    confidence: float
    tier: int
    based_on: str
    avg_execution_time: float
    alternatives: list[AlternativeAction]


# =============================================================================
# Endpoints
# =============================================================================


@router.get(
    "/summary/{anomaly_key}",
    response_model=LearningSummaryResponse,
    summary="取得學習摘要",
    description="根據異常 key 取得歷史學習摘要，包含嘗試過的修復動作和成功率",
)
async def get_learning_summary(anomaly_key: str) -> LearningSummaryResponse:
    """
    取得異常學習摘要

    Args:
        anomaly_key: 異常 key (例如 "restart_pod:awoooi-api-*")

    Returns:
        LearningSummaryResponse: 學習摘要
    """
    service = get_learning_service()
    summary = await service.get_learning_summary(anomaly_key)

    logger.info(
        "learning_summary_fetched",
        anomaly_key=anomaly_key,
        total_attempts=summary.get("total_repair_attempts", 0),
    )

    return LearningSummaryResponse(**summary)


@router.get(
    "/recommendation/{anomaly_key}",
    response_model=RecommendationResponse,
    summary="取得修復推薦",
    description="根據歷史學習數據，推薦最佳修復方案",
)
async def get_recommendation(anomaly_key: str) -> RecommendationResponse:
    """
    取得修復推薦

    Args:
        anomaly_key: 異常 key

    Returns:
        RecommendationResponse: 修復推薦 (包含動作、信心度、替代方案)
    """
    service = get_learning_service()
    recommendation = await service.get_recommended_fix(anomaly_key)

    logger.info(
        "learning_recommendation_fetched",
        anomaly_key=anomaly_key,
        recommended_action=recommendation.get("action"),
        confidence=recommendation.get("confidence"),
    )

    return RecommendationResponse(**recommendation)
