"""
Tests for Pressure Viscosity module
"""

import pytest
import numpy as np
from src.core.pressure_viscosity import PressureViscosity


class TestPressureViscosity:
    """Test suite for PressureViscosity"""
    
    def test_initialization(self):
        """Test initialization"""
        visc = PressureViscosity(temperature=2.0)
        assert visc.temperature == 2.0
        assert visc.alpha == 2.0e-9
    
    def test_surface_viscosity(self):
        """Test surface viscosity calculation"""
        visc = PressureViscosity(temperature=2.0)
        mu0 = visc.surface_viscosity()
        
        # Expected: 1.8e-3 Pa·s at 2°C
        assert mu0 == pytest.approx(1.8e-3, rel=0.05)
    
    def test_dynamic_viscosity(self):
        """Test pressure-dependent viscosity"""
        visc = PressureViscosity(temperature=2.0)
        
        # At surface
        mu_surface = visc.dynamic_viscosity(0.1)
        assert mu_surface == pytest.approx(1.8e-3, rel=0.05)
        
        # At Challenger Deep (110 MPa)
        mu_deep = visc.dynamic_viscosity(110.0)
        
        # Expected: 2.18e-3 Pa·s (22% increase)
        assert mu_deep == pytest.approx(2.18e-3, rel=0.05)
        assert mu_deep > mu_surface
    
    def test_kinematic_viscosity(self):
        """Test kinematic viscosity"""
        visc = PressureViscosity(temperature=2.0)
        
        nu = visc.kinematic_viscosity(110.0, density=1050)
        
        # ν = μ/ρ
        mu = visc.dynamic_viscosity(110.0)
        expected = mu / 1050
        
        assert nu == pytest.approx(expected, rel=0.01)
    
    def test_reynolds_number(self):
        """Test Reynolds number calculation"""
        visc = PressureViscosity(temperature=2.0)
        
        # Vehicle: 0.25 m/s, 0.05 m propeller
        Re = visc.reynolds_number(
            velocity=0.25,
            diameter=0.05,
            pressure=110.0,
            density=1050
        )
        
        # Should be in laminar regime (<1000)
        assert Re < 1000
        assert Re == pytest.approx(470, rel=0.1)
    
    def test_drag_coefficient(self):
        """Test drag coefficient calculation"""
        visc = PressureViscosity()
        
        # Stokes flow
        Cd = visc.drag_coefficient(10)
        assert Cd == pytest.approx(2.4, rel=0.1)
        
        # Transitional
        Cd = visc.drag_coefficient(100)
        assert 1.0 < Cd < 2.0
        
        # Turbulent
        Cd = visc.drag_coefficient(10000)
        assert Cd == pytest.approx(0.44, rel=0.1)
    
    def test_drag_force(self):
        """Test viscous drag force"""
        visc = PressureViscosity(temperature=2.0)
        
        force = visc.viscous_drag_force(
            velocity=0.25,
            area=0.01,
            pressure=110.0,
            density=1050
        )
        
        # Should be positive
        assert force > 0
    
    def test_propulsive_efficiency(self):
        """Test propulsive efficiency estimation"""
        visc = PressureViscosity(temperature=2.0)
        
        # At depth
        eff = visc.propulsive_efficiency(
            velocity=0.25,
            pressure=110.0,
            density=1050
        )
        
        assert 0 <= eff <= 1
    
    def test_validate_challenger_deep(self):
        """Test Challenger Deep validation"""
        visc = PressureViscosity(temperature=2.0)
        result = visc.validate_challenger_deep()
        
        assert 'viscosity' in result
        assert 'error_percent' in result
        assert result['error_percent'] < 5.0
        assert result['valid'] is True
