from pydantic import BaseModel
from decimal import Decimal
from typing import Optional


class DashboardStats(BaseModel):
    revenue_today: Decimal = Decimal("0")
    revenue_month: Decimal = Decimal("0")
    orders_today: int = 0
    orders_pending: int = 0
    customers_total: int = 0
    products_active: int = 0
    products_expiring: int = 0
    products_expired: int = 0
    # 环比变化率（与上一周期对比，百分比）
    revenue_today_change: float = 0.0
    revenue_month_change: float = 0.0
    orders_today_change: float = 0.0
    customers_new_today: int = 0
    refunds_pending: int = 0
    refunds_month_amount: Decimal = Decimal("0")
    contact_form_unread: int = 0


class RevenueTrendItem(BaseModel):
    date: str
    revenue: Decimal
    orders: int = 0
    refund_amount: Decimal = Decimal("0")


class OrderStatusItem(BaseModel):
    name: str
    value: int
    color: str


class TopProductItem(BaseModel):
    id: int
    name: str
    sku: str
    revenue: Decimal
    orders_count: int
    quantity: Decimal


class TopCustomerItem(BaseModel):
    id: int
    name: str
    email: str
    orders_count: int
    total_spent: Decimal


class CustomerGrowthItem(BaseModel):
    date: str
    new_count: int
    cumulative: int


class CategorySalesItem(BaseModel):
    category: str
    revenue: Decimal
    orders_count: int
    percentage: float


class InventoryAlertItem(BaseModel):
    id: int
    name: str
    sku: str
    stock_qty: Decimal
    low_stock_threshold: int
    status: str


class CouponStatsItem(BaseModel):
    id: int
    name: str
    code: Optional[str]
    type: str
    value: Decimal
    usage_limit: Optional[int]
    used_count: int
    usage_rate: float


class ReviewStatsResponse(BaseModel):
    avg_rating: float
    total: int
    approved: int
    pending: int
    distribution: dict  # {"1": n, "2": n, ...}


class ProductSalesItem(BaseModel):
    id: int
    name: str
    sku: str
    category: str
    base_price: Decimal
    revenue: Decimal
    quantity: Decimal
    orders_count: int
    avg_unit_price: Decimal
    stock_qty: Decimal


class ProductSalesPage(BaseModel):
    items: list[ProductSalesItem]
    total: int
    page: int
    page_size: int
