"""Configuration models for usage allocation."""

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

import yaml


@dataclass
class UsageConfig:
    """Configuration for usage allocation processing.
    
    Minimal configuration following the established pattern.
    All other parameters are fixed/optimal.
    """
    dataset_name: str
    
    @classmethod
    def load_from_file(cls, config_path: str) -> "UsageConfig":
        """Load usage configuration from YAML file.
        
        Args:
            config_path: Path to usage configuration YAML file
            
        Returns:
            UsageConfig instance
            
        Raises:
            FileNotFoundError: If config file doesn't exist
            ValueError: If config format is invalid
        """
        config_file = Path(config_path)
        if not config_file.exists():
            raise FileNotFoundError(f"Configuration file not found: {config_path}")
        
        try:
            with open(config_file, 'r') as f:
                config_data = yaml.safe_load(f)
        except yaml.YAMLError as e:
            raise ValueError(f"Invalid YAML format in {config_path}: {e}")
        
        if not isinstance(config_data, dict):
            raise ValueError(f"Configuration must be a YAML dictionary in {config_path}")
        
        # 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 fields in {config_path}: {missing_fields}")
        
        return cls(**{field: config_data[field] for field in required_fields})
    
    def validate(self) -> None:
        """Validate configuration parameters.
        
        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")
        
        # Validate dataset name format (basic check)
        if "/" in self.dataset_name or "\\" in self.dataset_name:
            raise ValueError("dataset_name cannot contain path separators")


def load_usage_config(config_path: str) -> UsageConfig:
    """Load and validate usage configuration from file.
    
    Args:
        config_path: Path to usage configuration YAML file
        
    Returns:
        Validated UsageConfig instance
        
    Raises:
        FileNotFoundError: If config file doesn't exist
        ValueError: If config format or values are invalid
    """
    config = UsageConfig.load_from_file(config_path)
    config.validate()
    return config