"""
Models for provider copy operations.

Data structures used by ProviderCopyOrchestrator to track source data,
copy results, and billing period information.
"""

from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional
from ..append.manifest_reader import S3FileInfo


@dataclass
class BillingPeriodInfo:
    """
    Information about a billing period discovered in source org.

    Contains all data needed to copy files from source to target org
    for a single billing period (month).
    """
    billing_period: str  # YYYYMMDD-YYYYMMDD format
    drop_id: str  # Source drop ID
    files: List[S3FileInfo]  # Files to copy
    manifest_data: Dict[str, Any]  # Original manifest data
    total_rows: int = 0
    total_size_bytes: int = 0


@dataclass
class ProviderDataInventory:
    """
    Complete inventory of provider data in source organization.

    Result of discovery phase - contains all billing periods and files
    that need to be copied to target organization.
    """
    source_org: str
    provider: str
    billing_periods: List[BillingPeriodInfo]
    total_files: int
    total_size_bytes: int

    def get_billing_period_names(self) -> List[str]:
        """Get list of billing period identifiers."""
        return [period.billing_period for period in self.billing_periods]

    def is_empty(self) -> bool:
        """Check if inventory contains any data."""
        return len(self.billing_periods) == 0


@dataclass
class ProviderCopyResult:
    """
    Result of copying a provider to target organization.

    Contains detailed outcome of copy operation including successes,
    failures, and metadata about what was copied.
    """
    source_org: str
    target_org: str
    provider: str
    status: str  # "success", "failed", "partial"
    billing_periods_copied: List[str] = field(default_factory=list)
    files_copied: int = 0
    files_failed: int = 0
    total_size_bytes: int = 0
    target_drop_id: str = ""
    copy_errors: List[str] = field(default_factory=list)
    manifest_updated: bool = False
    connection_status: str = ""  # "created", "validated", "failed"

    def is_successful(self) -> bool:
        """Check if copy was completely successful."""
        return self.status == "success"

    def is_failed(self) -> bool:
        """Check if copy failed completely."""
        return self.status == "failed"

    def is_partial(self) -> bool:
        """Check if copy was partially successful."""
        return self.status == "partial"

    def get_success_rate(self) -> float:
        """Calculate success rate as percentage."""
        total_files = self.files_copied + self.files_failed
        if total_files == 0:
            return 0.0
        return (self.files_copied / total_files) * 100.0
