from dataclasses import dataclass
from typing import List, Dict, Any, Optional
import os


@dataclass
class Config:
    """
    Centralized configuration for global/default settings.
    
    IMPORTANT: Farm-specific settings (distance_rings, ring_colors, wind_farm_name, 
    file paths, date ranges) are managed in wind_farms_config.json and set dynamically
    by batch_generate.py. The values below are only fallback defaults for backward
    compatibility and should NOT be configured here.
    """
    
    # Farm-specific settings (set by batch processing, DO NOT configure here)
    distance_rings: List[int] = None
    ring_colors: List[str] = None
    wind_farm_name: str = None
    analysis_start_date: Optional[str] = None
    analysis_end_date: Optional[str] = None
    timezone: Optional[str] = None

    # Lightning data source configuration
    # By default, lightning data is expected from API (JSON output).
    # When lightning_source_type is set to "csv", lightning_csv should
    # point to a CSV file that can be loaded instead.
    lightning_source_type: str = "api"
    lightning_json: Optional[str] = None
    lightning_csv: Optional[str] = None

    # Risk calculation parameters (global defaults)
    risk_params: Dict[str, float] = None
    
    # Histogram parameters (global defaults)
    histogram_params: Dict[str, Any] = None
    
    # Grouping parameters (global defaults, can be overridden per-farm)
    grouping_params: Dict[str, Any] = None

    # Centralized histogram layout configuration (global defaults)
    histogram_layout: Dict[str, Any] = None
    
    def __post_init__(self):
        """Set default values for global settings."""
        # Fallback defaults for farm-specific settings (only used if not set by batch processing)
        # DO NOT configure these - they come from wind_farms_config.json
        if self.distance_rings is None:
            self.distance_rings = [1000, 2000, 3000, 4000, 10000]
        
        if self.ring_colors is None:
            self.ring_colors = ['purple', 'red', 'orange', 'coral', 'green']
        
        if self.risk_params is None:
            self.risk_params = {
                'P_0': 1.0,
                'alpha': 0.5,
                'current_weight': 0.1
            }
        
        if self.histogram_params is None:
            self.histogram_params = {   
                'min_gap_minutes': 30,
                'min_events_per_period': 10,
                'max_periods': 8,
                'max_periods_per_figure': 6,
                'height_per_row': 200,
                'width': 800
            }
        
        if self.histogram_layout is None:
            self.histogram_layout = {
                'plot_width': 800,
                'plot_height_per_row': 250,
                'pdf_width_ratio': 0.95,
                'pdf_aspect_ratio': 0.75,
                'pdf_top_margin': 140,
                'pdf_left_margin': 40,
                'image_scale': 2,
                'image_engine': 'kaleido'
            }
        
        if self.grouping_params is None:
            self.grouping_params = {
                'max_distance_m': None,
                'distance_ring_index': 4,
                'min_group_size': 1,
                'max_group_size': 50
            }
# Global configuration instance
config = Config() 