# @File: backend/schemas/whatsapp_analytics.py
# @Desc: Pydantic I/O models for the WhatsApp campaigns analytics API.
#        All aggregations are returned as plain dicts so the frontend
#        can freely add extra columns without a schema bump — we only
#        model the structurally stable top-level envelopes.
from __future__ import annotations

from typing import Any, Dict, List

from pydantic import BaseModel


class KpiBlock(BaseModel):
    total_campaigns: int
    total_recipients: int
    sent: int
    delivered: int
    read: int
    replied: int
    failed: int
    delivery_rate: float
    reply_rate: float
    failure_rate: float


class RangeBlock(BaseModel):
    start: str
    end: str
    granularity: str


class TrendPoint(BaseModel):
    bucket_ts: str
    sent: int
    delivered: int
    read: int
    replied: int
    failed: int
    opted_out: int
    delivery_rate: float
    reply_rate: float


class OverviewOut(BaseModel):
    kpis: KpiBlock
    trend: List[TrendPoint]
    range: RangeBlock


class TimeseriesOut(BaseModel):
    campaign: Dict[str, Any]
    range: RangeBlock
    series: List[TrendPoint]


class FunnelStep(BaseModel):
    label: str
    value: int
    rate: float


class FunnelOut(BaseModel):
    campaign: Dict[str, Any]
    steps: List[FunnelStep]
    failed: int


class CompareItem(BaseModel):
    id: int
    name: str
    status: str
    total_recipients: int
    sent: int
    delivered: int
    replied: int
    failed: int
    delivery_rate: float
    reply_rate: float
    failure_rate: float
    created_at: str | None = None
    completed_at: str | None = None


class CompareOut(BaseModel):
    campaigns: List[CompareItem]