"""代理系统 Pydantic schemas"""
from datetime import datetime
from pydantic import BaseModel


# ── Agent Tier ──
class AgentTierResponse(BaseModel):
    id: int
    name: str
    code: str
    cost_rate: int
    min_member_level: int
    min_total_spend: int
    monthly_sales_required: int = 0
    monthly_active_customers_required: int = 0
    max_refund_rate_bps: int = 5000
    max_monthly_subsidy_cents: int = 10000
    min_balance_for_subsidy_cents: int = 12800
    observe_days: int = 7
    freeze_days: int = 7
    auto_downgrade_enabled: bool = True
    max_coupon_discount_rate: int
    max_monthly_coupon_count: int
    sort_order: int
    enabled: bool
    description: str | None
    created_at: datetime

    model_config = {"from_attributes": True}


class AgentTierCreate(BaseModel):
    name: str
    code: str
    cost_rate: int
    min_member_level: int = 0
    min_total_spend: int = 0
    monthly_sales_required: int = 0
    monthly_active_customers_required: int = 0
    max_refund_rate_bps: int = 5000
    max_monthly_subsidy_cents: int = 10000
    min_balance_for_subsidy_cents: int = 12800
    observe_days: int = 7
    freeze_days: int = 7
    auto_downgrade_enabled: bool = True
    max_coupon_discount_rate: int = 5000
    max_monthly_coupon_count: int = 10
    sort_order: int = 0
    enabled: bool = True
    description: str | None = None


class AgentTierUpdate(BaseModel):
    name: str | None = None
    code: str | None = None
    cost_rate: int | None = None
    min_member_level: int | None = None
    min_total_spend: int | None = None
    monthly_sales_required: int | None = None
    monthly_active_customers_required: int | None = None
    max_refund_rate_bps: int | None = None
    max_monthly_subsidy_cents: int | None = None
    min_balance_for_subsidy_cents: int | None = None
    observe_days: int | None = None
    freeze_days: int | None = None
    auto_downgrade_enabled: bool | None = None
    max_coupon_discount_rate: int | None = None
    max_monthly_coupon_count: int | None = None
    sort_order: int | None = None
    enabled: bool | None = None
    description: str | None = None


# ── User Agent ──
class UserAgentResponse(BaseModel):
    id: int
    user_id: int
    agent_tier_id: int | None
    status: str
    invite_code: str | None
    total_sales: int
    total_profit: int
    total_subsidy: int
    agent_balance: int = 0
    agent_frozen_balance: int = 0
    approved_at: datetime | None
    observe_started_at: datetime | None = None
    observe_until: datetime | None = None
    risk_locked_at: datetime | None = None
    risk_reason: str | None = None
    last_evaluated_at: datetime | None = None
    last_evaluation_snapshot: dict | None = None
    reject_reason: str | None
    remark: str | None
    review_message: str | None = None
    created_at: datetime
    agent_tier: AgentTierResponse | None = None
    user: "UserBrief | None" = None
    apply_tier: str | None = None
    apply_reason: str | None = None
    apply_channels: str | None = None

    model_config = {"from_attributes": True}


class AgentApplyRequest(BaseModel):
    tier: str  # yunqi / yunxiang / yunce / yunling / yunzun / yunguan
    reason: str
    channels: str


class UserBrief(BaseModel):
    id: int
    username: str
    email: str | None
    phone: str | None
    nickname: str | None
    balance: int
    created_at: datetime

    model_config = {"from_attributes": True}


class AgentReviewRequest(BaseModel):
    action: str  # "approve" or "reject"
    agent_tier_id: int | None = None
    reject_reason: str | None = None
    remark: str | None = None


class AgentTierAssignRequest(BaseModel):
    agent_tier_id: int


class AgentManualActionRequest(BaseModel):
    action: str
    reason: str
    agent_tier_id: int | None = None


# ── Agent Customer ──
class AgentCustomerResponse(BaseModel):
    id: int
    agent_user_id: int
    customer_user_id: int
    bind_source: str
    bound_at: datetime
    first_order_id: int | None
    status: str
    customer: UserBrief | None = None

    model_config = {"from_attributes": True}


# ── Agent Settlement ──
class AgentSettlementResponse(BaseModel):
    id: int
    order_id: int
    order_no: str
    settlement_type: str
    user_id: int
    customer_user_id: int | None
    standard_amount: int
    customer_pay_amount: int
    agent_cost_rate: int | None
    agent_cost_amount: int | None
    delta_amount: int
    commission_rate: int | None
    status: str
    frozen_until: datetime | None = None
    points_amount: int = 0
    point_log_id: int | None = None
    settled_at: datetime | None
    reversed_at: datetime | None = None
    remark: str | None
    created_at: datetime

    model_config = {"from_attributes": True}


# ── Agent Center ──
class AgentOverviewResponse(BaseModel):
    agent: UserAgentResponse | None
    customer_count: int
    pending_profit: int
    total_sales: int
    total_profit: int
    invite_code: str | None
    invite_code_edit_enabled: bool = False
    invite_code_change_count: int = 0
    invite_code_change_limit: int = 3
    invite_code_change_remaining: int = 3
    member_commission_rate: int  # 会员返佣比例（非代理时展示）


# ── Affiliate ──
class AffiliateInfoResponse(BaseModel):
    aff_code: str
    agent_name: str | None  # nickname or username
    agent_tier: str | None


class AffiliateClickRequest(BaseModel):
    aff_code: str
    ref: str | None = None
    coupon: str | None = None
    utm_source: str | None = None
    utm_medium: str | None = None
    utm_campaign: str | None = None
    utm_content: str | None = None
    utm_term: str | None = None
    landing_url: str | None = None
