"""Configuration models for discount package."""

from dataclasses import dataclass
from pathlib import Path
from typing import Optional
import yaml


@dataclass
class DiscountConfig:
    """Configuration for discount allocation processing."""
    
    dataset_name: str
    
    @classmethod
    def load_from_file(cls, config_path: str) -> "DiscountConfig":
        """Load discount configuration from YAML file.
        
        Args:
            config_path: Path to YAML configuration file
            
        Returns:
            DiscountConfig instance
            
        Raises:
            FileNotFoundError: If config file doesn't exist
            yaml.YAMLError: If YAML parsing fails
            ValueError: If required fields are missing
        """
        config_file = Path(config_path)
        if not config_file.exists():
            raise FileNotFoundError(f"Configuration file not found: {config_path}")
        
        with open(config_file, 'r') as f:
            config_data = yaml.safe_load(f)
        
        if not config_data:
            raise ValueError("Configuration file is empty")
        
        # Validate required fields
        required_fields = ['dataset_name']
        missing_fields = [field for field in required_fields if field not in config_data]
        if missing_fields:
            raise ValueError(f"Missing required configuration fields: {missing_fields}")
        
        return cls(**config_data)
    
    def validate(self) -> None:
        """Validate configuration values.
        
        Raises:
            ValueError: If configuration is invalid
        """
        if not self.dataset_name:
            raise ValueError("dataset_name cannot be empty")
        
        if not isinstance(self.dataset_name, str):
            raise ValueError("dataset_name must be a string")


def load_discount_config(config_path: str) -> DiscountConfig:
    """Convenience function to load and validate discount configuration.
    
    Args:
        config_path: Path to YAML configuration file
        
    Returns:
        Validated DiscountConfig instance
    """
    config = DiscountConfig.load_from_file(config_path)
    config.validate()
    return config