"""
Data models for upload validation operations.

Models used by UploadValidationService to represent validation results,
stage discovery outcomes, and processing plans.
"""

from dataclasses import dataclass
from typing import Dict, List, Optional, Any
from enum import Enum


class ValidationStatus(Enum):
    """Status of validation operations."""
    SUCCESS = "success"
    FAILED = "failed"
    PARTIAL = "partial"


@dataclass
class ValidationResult:
    """Result of upload validation operations."""
    status: ValidationStatus
    error_message: Optional[str] = None
    validation_step: Optional[str] = None
    details: Dict[str, Any] = None
    
    def is_successful(self) -> bool:
        """Check if validation was successful."""
        return self.status == ValidationStatus.SUCCESS
    
    def is_failed(self) -> bool:
        """Check if validation failed completely."""
        return self.status == ValidationStatus.FAILED


@dataclass  
class StageDiscoveryResult:
    """Result of stage discovery for a provider."""
    provider: str
    stage: str
    available_months: List[str]
    discovery_method: str  # "discovered" or "override"
    error_message: Optional[str] = None
    
    def is_successful(self) -> bool:
        """Check if stage discovery was successful."""
        return len(self.available_months) > 0 and not self.error_message


@dataclass
class ConnectionValidationResult:
    """Result of connection validation for an organization."""
    organization: str
    providers: List[str]
    status: ValidationStatus
    missing_providers: List[str] = None
    auto_created_connections: List[str] = None
    error_message: Optional[str] = None
    
    def requires_connection_creation(self) -> bool:
        """Check if connection creation is needed."""
        return self.missing_providers and len(self.missing_providers) > 0


@dataclass
class ProviderPlan:
    """Processing plan for a single provider."""
    name: str
    stage: str
    months: List[str]
    file_count: int
    discovery_method: str


@dataclass  
class OrganizationPlan:
    """Processing plan for a single organization."""
    name: str
    status: str
    providers: List[ProviderPlan]
    error: Optional[str] = None
    
    def is_ready_for_processing(self) -> bool:
        """Check if organization is ready for processing."""
        return self.status == "ready"
    
    def should_skip(self) -> bool:
        """Check if organization should be skipped."""
        return not self.is_ready_for_processing()


@dataclass
class ProcessingPlan:
    """Complete processing plan for all organizations."""
    organizations: List[OrganizationPlan]
    total_files: int
    total_organizations: int
    
    def get_ready_organizations(self) -> List[OrganizationPlan]:
        """Get organizations ready for processing."""
        return [org for org in self.organizations if org.is_ready_for_processing()]
    
    def has_processable_organizations(self) -> bool:
        """Check if any organizations are ready for processing."""
        return len(self.get_ready_organizations()) > 0