import math
from datetime import datetime
from astral import LocationInfo
from astral.sun import elevation, azimuth
import pytz

def calculate_angle_of_incidence(solar_elevation, solar_azimuth, panel_tilt, panel_azimuth):
    """Calculate angle of incidence between sun and panel surface"""
    if solar_elevation <= 0:
        return 0
    
    # Convert to radians
    se_rad = math.radians(solar_elevation)
    sa_rad = math.radians(solar_azimuth)
    pt_rad = math.radians(panel_tilt)
    pa_rad = math.radians(panel_azimuth)
    
    # Angle of incidence formula
    cos_incidence = (
        math.sin(se_rad) * math.cos(pt_rad) +
        math.cos(se_rad) * math.sin(pt_rad) * math.cos(sa_rad - pa_rad)
    )
    
    print(f"  Panel {panel_tilt}°: cos_incidence = {cos_incidence:.4f}, angle = {math.degrees(math.acos(abs(cos_incidence))):.1f}°")
    
    # For bifacial panels, allow light from angles > 90° but reduce efficiency
    if cos_incidence >= 0:
        return cos_incidence
    else:
        # Light from "behind" the panel surface - reduced efficiency
        return abs(cos_incidence) * 0.3  # 30% efficiency for oblique back-lighting

# Test with October data: 14:00, elevation 51.1°, azimuth 350°
solar_elev = 51.1
solar_az = 350.0
panel_azimuth = 0  # North-facing

print(f"Sun position: elevation {solar_elev}°, azimuth {solar_az}°")
print(f"Panel azimuth: {panel_azimuth}° (North)")
print()

# Test different tilts
tilts = [20, 30, 45, 60, 70]

print("Angle of incidence calculations:")
for tilt in tilts:
    cos_inc = calculate_angle_of_incidence(solar_elev, solar_az, tilt, panel_azimuth)

print()
print("Expected behavior:")
print("- At 51° sun elevation, optimal panel tilt should be around 90° - 51° = 39°")
print("- Therefore 30° tilt should be better than 20° tilt")
print("- And 45° should be even better than 30°")