from __future__ import annotations

from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.pool import StaticPool

from app.db.base import Base
from app.schemas.agent_feedback import AgentFeedbackCreate
from app.services.agent_feedback import AgentFeedbackService


def build_session() -> Session:
    engine = create_engine(
        "sqlite+pysqlite:///:memory:",
        connect_args={"check_same_thread": False},
        poolclass=StaticPool,
    )
    Base.metadata.create_all(bind=engine)
    session_factory = sessionmaker(bind=engine, autoflush=False, autocommit=False)
    return session_factory()


def test_agent_feedback_service_records_rating_and_low_reason() -> None:
    with build_session() as db:
        service = AgentFeedbackService(db)

        feedback = service.create_feedback(
            AgentFeedbackCreate(
                run_id="run-feedback-001",
                conversation_id="conv-feedback-001",
                user_id="wenjing.li",
                agent="user_agent",
                source="user_message",
                session_type="application",
                operation_type="submit_application",
                operation_status="succeeded",
                rating=2,
                reason="意图识别不准",
                context_json={"route_reason": "model_route"},
            )
        )
        summary = service.summarize_feedback(agent="user_agent", session_type="application")

        assert feedback.feedback_id.startswith("fb_")
        assert feedback.rating == 2
        assert feedback.reason == "意图识别不准"
        assert summary.total_feedback == 1
        assert summary.average_rating == 2.0
        assert summary.low_rating_count == 1
        assert summary.rating_distribution["2"] == 1
        assert summary.recent_low_feedback[0]["run_id"] == "run-feedback-001"


def test_agent_feedback_summary_keeps_five_star_distribution() -> None:
    with build_session() as db:
        service = AgentFeedbackService(db)
        for rating in (5, 4, 5):
            service.create_feedback(
                AgentFeedbackCreate(
                    run_id=f"run-rating-{rating}",
                    user_id="wenjing.li",
                    agent="user_agent",
                    session_type="expense",
                    operation_status="succeeded",
                    rating=rating,
                )
            )

        summary = service.summarize_feedback(session_type="expense")

        assert summary.total_feedback == 3
        assert summary.average_rating == 4.67
        assert summary.low_rating_count == 0
        assert summary.rating_distribution == {
            "1": 0,
            "2": 0,
            "3": 0,
            "4": 1,
            "5": 2,
        }
