from __future__ import annotations

from decimal import Decimal

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

from app.models import Base
from app.models.ai_config import ModelConfig
from app.models.logs import AiRequestLog
from app.services.admin_service import AdminDashboardService
from app.services.ai_request_log_service import AiRequestLogService


def _db() -> Session:
    engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False}, poolclass=StaticPool)
    Base.metadata.create_all(engine)
    return Session(engine)


def test_ai_request_log_estimates_cost_from_model_price():
    with _db() as db:
        model = ModelConfig(
            id=1,
            provider="test",
            api_type="openai_compatible",
            model_name="test-model",
            api_url="https://example.com",
            api_key="secret",
            input_price_per_1k=Decimal("0.002"),
            output_price_per_1k=Decimal("0.006"),
            currency="CNY",
            timeout_second=30,
        )
        db.add(model)
        db.commit()

        AiRequestLogService.write_success(
            db,
            session_id=1,
            message_id=2,
            user_id=3,
            model_id=1,
            model_name="test-model",
            prompt="hello",
            knowledge_ids="1",
            retrieve_count=2,
            input_token=1000,
            output_token=500,
            cost_ms=120,
        )
        db.commit()

        log = db.query(AiRequestLog).one()
        assert log.estimated_cost == Decimal("0.005000")
        assert log.currency == "CNY"
        assert log.knowledge_hit == 1
        assert log.question_type == "knowledge_grounded"
        assert "命中知识库" in (log.route_reason or "")


def test_dashboard_breaks_cost_down_by_actual_scene_and_model():
    with _db() as db:
        model = ModelConfig(
            id=1,
            provider="test",
            api_type="openai_compatible",
            model_name="report-model",
            api_url="https://example.com",
            api_key="secret",
            input_price_per_1k=Decimal("0.002"),
            output_price_per_1k=Decimal("0.006"),
            currency="CNY",
            timeout_second=30,
        )
        db.add(model)
        db.commit()
        AiRequestLogService.write_success(
            db,
            session_id=None,
            message_id=None,
            user_id=3,
            model_id=1,
            model_name="report-model",
            prompt="weekly report",
            knowledge_ids="",
            retrieve_count=0,
            input_token=1000,
            output_token=500,
            cost_ms=120,
            question_type="background_report",
            route_reason="场景分流：周期报告",
        )
        db.commit()

        stats = AdminDashboardService.stats(db)

        assert stats["costBreakdown"] == [
            {
                "scene": "background_report",
                "modelName": "report-model",
                "requestCount": 1,
                "inputToken": 1000,
                "outputToken": 500,
                "totalToken": 1500,
                "estimatedCost": 0.005,
                "currency": "CNY",
            }
        ]
