"""Data models for dataset identification and management."""

from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict, List, Optional


@dataclass
class BasicMetadata:
    """Basic metadata for a dataset (fast operations)."""
    dataset_id: str
    created_at: datetime
    description: str
    reference_data_version: str
    stages_completed: List[str]
    last_updated: datetime


@dataclass
class ProviderCostBreakdown:
    """Provider cost breakdown with monthly and yearly totals."""
    provider_costs: Dict[str, Dict[str, float]]  # {provider: {month: cost, yearly_total: total}}


@dataclass
class ProviderRowBreakdown:
    """Provider row count breakdown by month."""
    provider_rows: Dict[str, Dict[str, int]]  # {provider: {month: row_count}}


@dataclass
class SummaryInsights:
    """Summary insights derived from cost and row data."""
    top_providers_by_cost: List[str]
    top_providers_by_max_monthly_rows: List[str]
    date_ranges_by_provider: Dict[str, str]


@dataclass
class DetailedMetadata:
    """Detailed metadata for a dataset (computed on --refresh, cached)."""
    basic: BasicMetadata
    metadata_computed_at: datetime
    provider_cost_breakdown: ProviderCostBreakdown
    provider_row_breakdown: ProviderRowBreakdown
    summary_insights: SummaryInsights


@dataclass
class UploadEvent:
    """Records an upload event for tracking."""
    upload_id: str
    dataset_id: str
    organization: str
    uploaded_at: datetime
    upload_type: str  # "new" or "append"
    providers_uploaded: List[str]
    months_uploaded: List[str]
    date_range_folder: str
    drop_id: str  # Active drop ID for this upload


@dataclass
class DatasetContribution:
    """Represents a dataset's contribution to a billing drop."""
    dataset_id: str
    description: str
    upload_event: UploadEvent
    csv_billing_rows: int
    file_count: int


@dataclass
class DropComposition:
    """Composition analysis of a billing drop."""
    organization: str
    provider: str
    date_range: str
    active_drop_id: str
    contributing_datasets: List[DatasetContribution]
    total_csv_billing_rows: int
    total_files_processed: int


@dataclass
class DropInfo:
    """Basic information about a billing drop."""
    organization: str
    provider: str
    date_range: str
    active_drop_id: str
    last_upload: Optional[datetime] = None