from __future__ import annotations

from typing import Annotated

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session

from app.api.deps import get_db
from app.schemas.agent_feedback import (
    AgentFeedbackCreate,
    AgentFeedbackRead,
    AgentFeedbackSummaryRead,
)
from app.services.agent_feedback import AgentFeedbackService

router = APIRouter(prefix="/agent-feedback")
DbSession = Annotated[Session, Depends(get_db)]


@router.post(
    "",
    response_model=AgentFeedbackRead,
    status_code=status.HTTP_201_CREATED,
    summary="记录 Agent 操作评价",
    description="记录用户对一次智能体处理结果的 1-5 星评价和低分原因。",
)
def create_agent_feedback(payload: AgentFeedbackCreate, db: DbSession) -> AgentFeedbackRead:
    return AgentFeedbackService(db).create_feedback(payload)


@router.get(
    "/summary",
    response_model=AgentFeedbackSummaryRead,
    summary="查询 Agent 操作评价统计",
    description="按最近反馈记录汇总评分分布、低分数量和低分原因。",
)
def summarize_agent_feedback(
    db: DbSession,
    agent: Annotated[str | None, Query(description="Agent 名称筛选。")] = None,
    session_type: Annotated[str | None, Query(description="会话类型筛选。")] = None,
    limit: Annotated[int, Query(ge=1, le=500, description="统计最近记录数。")] = 200,
) -> AgentFeedbackSummaryRead:
    return AgentFeedbackService(db).summarize_feedback(
        agent=agent,
        session_type=session_type,
        limit=limit,
    )
