"""
小程序任务相关 Pydantic 模型。

覆盖：任务列表项、任务详情、绩效概览、放弃请求等场景。
"""

from __future__ import annotations

from pydantic import Field

from app.schemas.base import CamelModel


class TaskListItem(CamelModel):
    """任务列表项（含客户信息 + RS 指数 + 爱心 icon）。"""

    id: int
    task_type: str
    status: str
    priority_score: float | None
    is_pinned: bool
    expires_at: str | None
    created_at: str
    # 客户信息（FDW 读取）
    member_id: int
    member_name: str | None
    member_phone: str | None
    # RS 指数 + 爱心 icon
    rs_score: float | None
    heart_icon: str  # 💖 / 🧡 / 💛 / 💙
    # 放弃原因（仅 abandoned 任务有值）
    abandon_reason: str | None = None


class AbandonRequest(CamelModel):
    """放弃任务请求（reason 必填）。"""

    reason: str = Field(..., min_length=1, description="放弃原因（必填）")


# ---------------------------------------------------------------------------
# RNS1.1 扩展模型
# ---------------------------------------------------------------------------


class PerformanceSummary(CamelModel):
    """绩效概览（附带在任务列表响应中）。"""

    total_hours: float
    total_income: float
    total_customers: int
    month_label: str
    tier_nodes: list[float]
    basic_hours: float
    bonus_hours: float
    current_tier: int
    next_tier_hours: float
    tier_completed: bool
    bonus_money: float
    income_trend: str
    income_trend_dir: str  # 'up' | 'down'
    prev_month: str
    current_tier_label: str


class TaskItem(CamelModel):
    """任务列表项（扩展版）。"""

    id: int
    customer_name: str
    customer_avatar: str
    task_type: str
    task_type_label: str
    deadline: str | None
    heart_score: float
    hobbies: list[str]
    is_pinned: bool
    has_note: bool
    status: str
    last_visit_days: int | None = None
    balance: float | None = None
    ai_suggestion: str | None = None
    expected_days: int | None = None
    ideal_interval_days: int | None = None
    # CHANGE 2026-03-27 | 近60天服务汇总（口径同 task-detail serviceSummary）
    recent60d_hours: float = 0.0
    recent60d_income: float = 0.0


class TaskListResponse(CamelModel):
    """TASK-1 响应。"""

    items: list[TaskItem]
    total: int
    page: int
    page_size: int
    performance: PerformanceSummary


class RetentionClue(CamelModel):
    """维客线索。"""

    tag: str
    tag_color: str
    emoji: str
    text: str
    source: str  # 'manual' | 'ai_consumption' | 'ai_note'
    desc: str | None = None


class ServiceRecord(CamelModel):
    """服务记录。"""

    table: str | None = None
    type: str
    type_class: str  # 'basic' | 'vip' | 'tip' | 'recharge' | 'incentive'
    record_type: str | None = None  # 'course' | 'recharge'
    duration: float
    duration_raw: float | None = None
    income: float
    is_estimate: bool | None = None
    drinks: str | None = None
    date: str


class AiAnalysis(CamelModel):
    """AI 分析结果。"""

    summary: str
    suggestions: list[str]


class NoteItem(CamelModel):
    """备注项。"""

    id: int
    content: str
    tag_type: str
    tag_label: str
    created_at: str
    score: int | None = None


class ServiceSummary(CamelModel):
    """服务记录摘要。"""

    total_hours: float
    total_income: float
    avg_income: float


class TaskDetailResponse(CamelModel):
    """TASK-2 响应。"""

    # 基础信息
    id: int
    customer_name: str
    customer_phone: str | None = None
    customer_avatar: str
    task_type: str
    task_type_label: str
    deadline: str | None
    heart_score: float
    hobbies: list[str]
    is_pinned: bool
    has_note: bool
    status: str
    customer_id: int
    balance: float | None = None
    # 扩展模块
    retention_clues: list[RetentionClue]
    talking_points: list[str]
    service_summary: ServiceSummary
    service_records: list[ServiceRecord]
    ai_analysis: AiAnalysis
    notes: list[NoteItem]
