"""브리핑 저장/조회 + 큐레이터 사용량 엔드포인트."""
import logging
from typing import Any, Dict, List
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from .. import db

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/lotto")


class TierRationale(BaseModel):
    bonus: str = ""
    extended: str = ""
    pool: str = ""


class BriefingPicks(BaseModel):
    core: List[Dict[str, Any]] = Field(default_factory=list)
    bonus: List[Dict[str, Any]] = Field(default_factory=list)
    extended: List[Dict[str, Any]] = Field(default_factory=list)
    pool: List[Dict[str, Any]] = Field(default_factory=list)


class BriefingRequest(BaseModel):
    draw_no: int
    picks: BriefingPicks
    narrative: Dict[str, Any]
    tier_rationale: TierRationale = Field(default_factory=TierRationale)
    confidence: int = Field(ge=0, le=100)
    model: str
    tokens_input: int = 0
    tokens_output: int = 0
    cache_read: int = 0
    cache_write: int = 0
    latency_ms: int = 0
    source: str = "auto"


@router.post("/briefing", status_code=201)
def save_briefing(body: BriefingRequest):
    bid = db.save_briefing(body.model_dump())
    logger.info(f"브리핑 저장 완료: id={bid}, draw_no={body.draw_no}, model={body.model!r}, input_tokens={body.tokens_input}, output_tokens={body.tokens_output}")
    return {"ok": True, "id": bid}


@router.get("/briefing/latest")
def latest():
    b = db.get_latest_briefing()
    if not b:
        raise HTTPException(404, "no briefing yet")
    return b


@router.get("/briefing/{draw_no}")
def get_one(draw_no: int):
    b = db.get_briefing(draw_no)
    if not b:
        raise HTTPException(404, f"no briefing for draw {draw_no}")
    return b


@router.get("/briefing")
def history(limit: int = 10):
    return {"briefings": db.list_briefings(limit)}


@router.get("/curator/usage")
def usage(days: int = 30):
    return db.get_curator_usage(days)
