"""
Physical constants and default parameters for CAVORA.
Centralized constants used throughout the package.

Author: Samir Baladi
"""

import numpy as np


class Constants:
    """
    Physical constants and default parameters for karst systems.
    All values from the research paper unless otherwise noted.
    """
    
    # ============================================================
    # Physical Constants
    # ============================================================
    
    GRAVITY = 9.81  # m/s²
    WATER_DENSITY = 1000  # kg/m³
    ROCK_DENSITY = 2700  # kg/m³ (typical limestone)
    ATMOSPHERIC_PRESSURE = 101325  # Pa
    GAS_CONSTANT = 8.314  # J/(mol·K)
    
    # ============================================================
    # Default VLF Parameters (from paper)
    # ============================================================
    
    # Baseline values
    V_BASELINE = 2.5  # m - typical vertical passage
    L_BASELINE = 3.0  # m - typical lateral conduit
    F_BASELINE = 0.08  # mm/yr - typical flowstone deposition
    
    # Non-linearity parameters
    MU_V = 0.15  # V non-linearity
    MU_L = 0.10  # L non-linearity
    MU_F = 0.05  # F non-linearity
    
    # Coupling coefficients
    K_VF = 0.23  # V-F coupling
    K_LF = -0.18  # L-F coupling
    K_FV = 0.31  # F-V coupling
    K_FL = -0.25  # F-L coupling
    
    # Natural frequency
    OMEGA_0 = 0.05  # rad/yr
    
    # ============================================================
    # Stability Thresholds
    # ============================================================
    
    STABILITY_THRESHOLDS = {
        'STABLE': 80,
        'CAUTION': 60,
        'UNSTABLE': 40,
        'HIGH_RISK': 20,
        'CRITICAL': 0
    }
    
    STABILITY_COLORS = {
        'STABLE': '🟢',
        'CAUTION': '🟡',
        'UNSTABLE': '🟠',
        'HIGH_RISK': '🔴',
        'CRITICAL': '⚫'
    }
    
    # ============================================================
    # CDZ Detection Thresholds (from paper)
    # ============================================================
    
    CDZ_DIVERGENCE_THRESHOLD = 1.5  # σ units
    CDZ_TRANSITION_THRESHOLD = 0.5  # m/season
    CDZ_CO2_THRESHOLD = 500  # ppm/hour
    
    # ============================================================
    # Hydrological Thresholds (from paper)
    # ============================================================
    
    DISCHARGE_THRESHOLDS = {
        'SAFE': 0.5,      # m³/s
        'CAUTION': 2.0,   # m³/s
        'HIGH_RISK': 5.0,  # m³/s
        'CRITICAL': 5.0    # m³/s (same as HIGH_RISK)
    }
    
    WATER_LEVEL_THRESHOLDS = {
        'NORMAL': 1.0,     # m
        'ELEVATED': 2.0,   # m
        'HIGH': 3.0,       # m
        'CRITICAL': 4.0    # m
    }
    
    # ============================================================
    # CO₂ Thresholds (ppm)
    # ============================================================
    
    CO2_THRESHOLDS = {
        'NORMAL': 1000,
        'ELEVATED': 2000,
        'HIGH': 5000,
        'DANGEROUS': 8500,
        'CRITICAL': 15000
    }
    
    # ============================================================
    # Time Conversions
    # ============================================================
    
    SECONDS_PER_MINUTE = 60
    MINUTES_PER_HOUR = 60
    HOURS_PER_DAY = 24
    DAYS_PER_YEAR = 365.25
    
    SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
    SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY
    SECONDS_PER_YEAR = SECONDS_PER_DAY * DAYS_PER_YEAR
    
    # ============================================================
    # Measurement Precisions (from paper)
    # ============================================================
    
    PRECISION_V = 0.05  # m - vertical measurement
    PRECISION_L = 0.10  # m - lateral measurement
    PRECISION_F = 0.5   # mm - flowstone measurement
    PRECISION_Q = 0.01  # m³/s - discharge measurement
    PRECISION_CO2 = 25  # ppm - CO₂ measurement
    PRECISION_TEMP = 0.1  # °C - temperature measurement
    PRECISION_HUMIDITY = 2  # % - relative humidity
    
    # ============================================================
    # Survey Team Parameters
    # ============================================================
    
    TEAM_SIZE_MIN = 3  # Minimum safe team size
    TEAM_SIZE_MAX = 6  # Maximum recommended team size
    MAX_SURVEY_HOURS = 8  # Maximum hours per shift
    REST_REQUIRED_HOURS = 12  # Hours rest required between shifts
    
    # ============================================================
    # Default File Paths
    # ============================================================
    
    DEFAULT_DATA_DIR = "data"
    DEFAULT_OUTPUT_DIR = "output"
    DEFAULT_LOG_DIR = "logs"
    DEFAULT_MODEL_DIR = "models"
    
    # ============================================================
    # Unit Conversions
    # ============================================================
    
    @classmethod
    def m_to_ft(cls, meters: float) -> float:
        """Convert meters to feet."""
        return meters * 3.28084
    
    @classmethod
    def ft_to_m(cls, feet: float) -> float:
        """Convert feet to meters."""
        return feet / 3.28084
    
    @classmethod
    def lps_to_cfs(cls, lps: float) -> float:
        """Convert liters per second to cubic feet per second."""
        return lps * 0.0353147
    
    @classmethod
    def cfs_to_lps(cls, cfs: float) -> float:
        """Convert cubic feet per second to liters per second."""
        return cfs / 0.0353147
    
    @classmethod
    def celsius_to_fahrenheit(cls, celsius: float) -> float:
        """Convert Celsius to Fahrenheit."""
        return celsius * 9/5 + 32
    
    @classmethod
    def fahrenheit_to_celsius(cls, fahrenheit: float) -> float:
        """Convert Fahrenheit to Celsius."""
        return (fahrenheit - 32) * 5/9


# Example usage
if __name__ == "__main__":
    print("="*60)
    print("CAVORA - Constants")
    print("="*60)
    
    print(f"\nVLF Baseline Values:")
    print(f"  V₀ = {Constants.V_BASELINE} m")
    print(f"  L₀ = {Constants.L_BASELINE} m")
    print(f"  F₀ = {Constants.F_BASELINE} mm/yr")
    
    print(f"\nCoupling Coefficients:")
    print(f"  k_vf = {Constants.K_VF}")
    print(f"  k_lf = {Constants.K_LF}")
    print(f"  k_fv = {Constants.K_FV}")
    print(f"  k_fl = {Constants.K_FL}")
    
    print(f"\nUnit Conversions:")
    print(f"  10 m = {Constants.m_to_ft(10):.2f} ft")
    print(f"  20°C = {Constants.celsius_to_fahrenheit(20):.1f}°F")
    
    print("\n" + "="*60)
