import uuid
from decimal import Decimal

from pydantic import BaseModel


class OrderStatusBreakdownOut(BaseModel):
    status: str
    count: int


class TopMerchantOut(BaseModel):
    merchant_id: uuid.UUID
    merchant_name: str
    order_count: int
    # Completed orders only — a cancelled or still-in-progress order
    # isn't real sales yet. order_count above is every order in the
    # window regardless of status, since "most orders coming in" is
    # the more useful "top" signal for a merchant on its own.
    total_sales: Decimal


class TopBranchOut(BaseModel):
    branch_id: uuid.UUID
    branch_name: str
    order_count: int
    total_sales: Decimal


class AdminAnalyticsOut(BaseModel):
    """System-performance snapshot for the admin console. period_days
    windows order_volume/order_status_breakdown/cancellation_rate/
    top_merchants; active_couriers/active_branches are a live
    right-now count, not windowed by period_days.

    revenue is Yeneb's own cut (service_fee on paid orders — couriers
    keep 100% of delivery_fee, see app.models.delivery.CourierEarning),
    not gross order value, and is superadmin-only: null for a regular
    admin."""

    period_days: int
    order_volume: int
    order_status_breakdown: list[OrderStatusBreakdownOut]
    cancellation_rate: float
    active_couriers: int
    active_branches: int
    top_merchants: list[TopMerchantOut]
    top_branches: list[TopBranchOut]
    revenue: Decimal | None = None
