"""
Comprehensive Fluid Properties Module
"""

import numpy as np
from typing import Dict, Tuple
from .equation_of_state import EquationOfState
from .acoustic_model import AcousticModel
from .pressure_viscosity import PressureViscosity


class FluidProperties:
    """
    Comprehensive fluid properties calculator
    
    Combines equation of state, acoustic, and viscosity models
    """
    
    def __init__(self,
                 temperature: float = 2.0,
                 salinity: float = 34.7,
                 depth: float = 0):
        """
        Initialize fluid properties
        
        Args:
            temperature: Water temperature in °C
            salinity: Salinity in PSU
            depth: Depth in meters
        """
        self.temperature = temperature
        self.salinity = salinity
        self.depth = depth
        
        # Initialize sub-models
        self.eos = EquationOfState(
            temperature=temperature,
            salinity=salinity,
            depth=depth
        )
        
        self.acoustic = AcousticModel(
            temperature=temperature,
            salinity=salinity,
            depth=depth
        )
        
        self.viscosity = PressureViscosity(
            temperature=temperature
        )
    
    @property
    def pressure(self) -> float:
        """Get pressure in MPa"""
        return self.eos.pressure
    
    @property
    def density(self) -> float:
        """Get density in kg/m³"""
        return self.eos.compute_density()
    
    @property
    def sound_speed(self) -> float:
        """Get sound speed in m/s"""
        return self.acoustic.sound_speed()
    
    @property
    def dynamic_viscosity(self) -> float:
        """Get dynamic viscosity in Pa·s"""
        return self.viscosity.dynamic_viscosity(self.pressure)
    
    @property
    def kinematic_viscosity(self) -> float:
        """Get kinematic viscosity in m²/s"""
        return self.viscosity.kinematic_viscosity(self.pressure, self.density)
    
    def all_properties(self) -> Dict[str, float]:
        """Get all fluid properties as dictionary"""
        return {
            'depth_m': self.depth,
            'pressure_mpa': self.pressure,
            'temperature_c': self.temperature,
            'salinity_psu': self.salinity,
            'density_kgm3': self.density,
            'density_increase_percent': self.eos.density_increase_percent(),
            'sound_velocity_ms': self.sound_speed,
            'sound_velocity_increase_percent': 
                (self.acoustic.refraction_index() - 1) * 100,
            'dynamic_viscosity_pas': self.dynamic_viscosity,
            'kinematic_viscosity_m2s': self.kinematic_viscosity,
            'viscosity_increase_percent':
                (self.dynamic_viscosity / self.viscosity.surface_viscosity() - 1) * 100
        }
    
    def summary(self) -> str:
        """Get formatted summary string"""
        props = self.all_properties()
        
        lines = [
            "=" * 50,
            f"Fluid Properties at {props['depth_m']:.0f}m depth",
            "=" * 50,
            f"Pressure:          {props['pressure_mpa']:.2f} MPa",
            f"Temperature:       {props['temperature_c']:.1f} °C",
            f"Salinity:          {props['salinity_psu']:.2f} PSU",
            f"Density:           {props['density_kgm3']:.1f} kg/m³",
            f"  (+{props['density_increase_percent']:.2f}% vs surface)",
            f"Sound Velocity:    {props['sound_velocity_ms']:.1f} m/s",
            f"  (+{props['sound_velocity_increase_percent']:.2f}% vs surface)",
            f"Dynamic Viscosity: {props['dynamic_viscosity_pas']:.2e} Pa·s",
            f"  (+{props['viscosity_increase_percent']:.2f}% vs surface)",
            "=" * 50
        ]
        
        return "\n".join(lines)
    
    def compare_trenches(self) -> Dict[str, Dict]:
        """Compare properties across major trenches"""
        trenches = {
            'Challenger Deep': {'depth': 10911, 'temp': 1.9, 'sal': 34.68},
            'Tonga Trench': {'depth': 10882, 'temp': 1.9, 'sal': 34.7},
            'Philippine Trench': {'depth': 10540, 'temp': 2.0, 'sal': 34.7},
            'Kermadec Trench': {'depth': 10047, 'temp': 2.0, 'sal': 34.7},
            'Japan Trench': {'depth': 9000, 'temp': 2.1, 'sal': 34.6}
        }
        
        results = {}
        for name, params in trenches.items():
            fp = FluidProperties(
                temperature=params['temp'],
                salinity=params['sal'],
                depth=params['depth']
            )
            results[name] = fp.all_properties()
        
        return results
    
    def __repr__(self) -> str:
        return (f"FluidProperties(T={self.temperature}°C, "
                f"S={self.salinity}PSU, depth={self.depth:.0f}m)")
