#!/usr/bin/env python3
"""
Clean High Conductance Test - Full Progression
=============================================
Tests Period 1 -> Period 2 progression with high conductance GHB
to verify center cell responds correctly to boundary gradients.
"""

import os
from gspy_interface import run_modflow_step

def clean_high_conductance_test():
    """Test full progression with high conductance boundaries"""
    
    print("="*80)
    print("CLEAN HIGH CONDUCTANCE TEST")  
    print("Testing complete Period 1 -> Period 2 progression")
    print("GHB Conductance: 1,000,000 m²/day")
    print("="*80)
    
    # Clean up any existing state
    state_file = "modflow_state.pkl"
    if os.path.exists(state_file):
        os.remove(state_file)
        print(f"🗑️  Removed existing state file: {state_file}")
    
    # Period 1: Uniform boundaries (10.0m both sides)
    print("\n🔬 PERIOD 1: Uniform boundaries")
    print("Left=10.0m, Right=10.0m → Expected: 10.0m everywhere")
    result1 = run_modflow_step(1, 0.0, 10.0, 0.3)
    head1 = result1[0] if isinstance(result1, tuple) else result1
    print(f"Period 1 result: {head1:.6f} m")
    
    # Period 2: Gradient boundaries (10.0m to 0.0m)  
    print("\n🔬 PERIOD 2: Gradient boundaries")
    print("Left=10.0m, Right=0.0m → Expected: <10.0m at center")
    result2 = run_modflow_step(2, 0.0, 10.0, 0.3)
    head2 = result2[0] if isinstance(result2, tuple) else result2
    print(f"Period 2 result: {head2:.6f} m")
    
    # Analysis
    print(f"\n📊 ANALYSIS:")
    print(f"Period 1: {head1:.6f} m")
    print(f"Period 2: {head2:.6f} m") 
    print(f"Change: {head2-head1:+.6f} m")
    
    if abs(head1 - 10.0) < 0.001:
        print("✅ Period 1: Correct uniform behavior")
    else:
        print("❌ Period 1: Unexpected deviation from 10.0m")
        
    if head2 < 9.9:
        print("✅ Period 2: Center cell responds to gradient!")
        print("✅ HIGH CONDUCTANCE FIX SUCCESSFUL!")
        success = True
    else:
        print("❌ Period 2: Center cell still stuck near 10.0m")
        print("❌ Further investigation needed")
        success = False
        
    return success, head1, head2

if __name__ == "__main__":
    success, h1, h2 = clean_high_conductance_test()
    
    print(f"\n🎯 FINAL RESULT:")
    if success:
        print("✅ High conductance fix RESOLVED the center cell issue!")
    else:
        print("❌ Center cell issue persists - need different approach")