from datetime import datetime

from pydantic import BaseModel


class MemberLevelResponse(BaseModel):
    id: int
    name: str
    level: int
    min_total_spent: int
    self_discount_rate: int
    points_multiplier: int
    points_cashback_rate: int = 0
    max_points_discount_rate: int
    can_apply_agent: bool
    commission_rate: int
    sort_order: int
    enabled: bool

    model_config = {"from_attributes": True}


class UserMembershipResponse(BaseModel):
    user_id: int
    member_level_id: int | None
    member_level_locked: bool
    agent_tier: str | None
    total_spent: int
    current_quarter_self_spent: int = 0
    current_quarter_spent: int = 0
    total_points: int
    available_points: int
    upgraded_at: datetime | None
    member_level: MemberLevelResponse | None
    commission_rate: int = 0  # 从 member_level 继承

    model_config = {"from_attributes": True}


class PointLogResponse(BaseModel):
    id: int
    user_id: int
    change_type: str
    delta: int
    balance_after: int
    order_id: int | None
    ref_no: str | None
    remark: str | None
    created_at: datetime

    model_config = {"from_attributes": True}


class PointsUseRequest(BaseModel):
    points: int


class SpendEventTypeConfigResponse(BaseModel):
    id: int
    code: str
    label: str
    description: str | None
    enabled: bool
    sort_order: int
    updated_at: datetime | None

    model_config = {"from_attributes": True}


class SpendEventTypeConfigUpdate(BaseModel):
    enabled: bool
