"""admin 比价记录 debug 页 schema(独立于 C 端 app/schemas/compare_record:admin 看任意用户、
无 debug_trace 权限闸、展示全量 debug 字段)。phone/nickname 由 queries 瞬态挂在 ORM 实例上。"""
from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, ConfigDict


class AdminComparisonListItem(BaseModel):
    """列表项:概要 + debug 维度概览(不含大字段 raw_payload/llm_calls/明细数组)。"""

    model_config = ConfigDict(from_attributes=True)

    id: int
    user_id: int
    phone: str | None = None        # join User 瞬态(非 DB 列)
    nickname: str | None = None     # join User 瞬态
    business_type: str
    trace_id: str
    # admin 是 debug 工具,无条件下发 trace_url(不看 user.debug_trace_enabled)
    trace_url: str | None = None
    status: str
    information: str | None = None
    store_name: str | None = None
    product_names: str | None = None   # 下单商品名派生串(顿号分隔;「商品」列展示 + 商品搜索)
    source_platform_name: str | None = None
    best_platform_name: str | None = None
    source_price_cents: int | None = None
    best_price_cents: int | None = None
    saved_amount_cents: int | None = None
    ordered: bool = False
    # debug 概览
    total_ms: int | None = None
    step_count: int | None = None
    llm_call_count: int | None = None
    retry_count: int | None = None
    input_tokens: int | None = None      # Σ usage.prompt_tokens(server 派生)
    output_tokens: int | None = None     # Σ usage.completion_tokens(server 派生)
    # 本次比价 LLM 总成本(元,按当时价冻结);旧记录/未回填为 None → 前端「成本」列回退估算。见 services/llm_cost.py。
    llm_cost_yuan: float | None = None
    device_model: str | None = None
    device_model_name: str | None = None
    rom_vendor: str | None = None
    rom_name: str | None = None
    rom_version: int | None = None
    android_version: str | None = None
    app_version: str | None = None
    ad_revenue_yuan: float = 0.0   # 本次比价看的信息流广告预估收益(元),queries 瞬态挂 ORM 实例上
    created_at: datetime


class AdminComparisonSummary(BaseModel):
    """比价记录页概览；主耗时指标仅统计 status=success。"""

    started: int
    completed: int
    success: int
    success_rate: float | None = None
    avg_token_cost: float | None = None
    lower_price_rate: float | None = None
    avg_duration_ms: int | None = None
    p5_duration_ms: int | None = None
    p50_duration_ms: int | None = None
    p95_duration_ms: int | None = None
    p99_duration_ms: int | None = None
    cancelled: int
    cancelled_rate: float | None = None
    cancelled_p5_ms: int | None = None
    cancelled_p50_ms: int | None = None
    cancelled_p95_ms: int | None = None


class AdminComparisonDetail(AdminComparisonListItem):
    """详情:概要 + 全量明细(逐平台对比 / LLM 每次调用 / 原始 payload)。"""

    source_platform_id: str | None = None
    source_package: str | None = None
    best_platform_id: str | None = None
    best_deeplink: str | None = None
    is_source_best: bool | None = None
    total_dish_count: int | None = None
    skipped_dish_count: int | None = None
    device_id: str | None = None
    items: list = []
    comparison_results: list = []   # 逐平台对比(价格/rank/coupon/打烊...)
    skipped_dish_names: list = []
    # 全量环境
    device_manufacturer: str | None = None
    android_sdk: int | None = None
    app_version_code: int | None = None
    source_app_version: str | None = None
    longitude: float | None = None
    latitude: float | None = None
    # 每次 LLM 调用明细 [{scene,model,input_messages,output,usage,latency_ms,error}]
    llm_calls: list | None = None
    # 原始上报全量;「卡在哪一步」从 raw_payload.platform_results[*].status 读
    # (store_not_found/items_not_found/below_minimum/unsupported = 卡在 找店/加菜/起送/读价)。
    raw_payload: dict | None = None
    # 算成本所用单价快照 {mode, prices:{model:{...}}}(llm_cost_yuan 继承自列表项)。见 services/llm_cost.py。
    llm_price_snapshot: dict | None = None
