import math
import sys
sys.path.append('/home/www/claudev.cz/orb3')

from solar_simulation_astronomical import BifacialSolarSimulation
from datetime import datetime

sim = BifacialSolarSimulation()

# Test June 15, 10:00
date = datetime(2024, 6, 15)
solar_elev, solar_az = sim.get_solar_position(date, 10, 0)
print(f'June 15, 10:00: {solar_elev:.1f}°/{solar_az:.0f}°')
print()

# Compare 90° vs 70°
for tilt in [90, 70]:
    print(f"=== Panel {tilt}° (North-facing) ===")
    
    # Manual calculation to see what's happening
    front_cos = sim.calculate_angle_of_incidence(solar_elev, solar_az, tilt, 0)
    front_direct = sim.dni_clear_sky * front_cos if front_cos > 0 else 0
    
    # Diffuse
    diffuse_factor = 0.15
    sky_diffuse = sim.dni_clear_sky * diffuse_factor
    front_diffuse = sky_diffuse * (1 + math.cos(math.radians(tilt))) / 2
    back_diffuse = sky_diffuse * (1 - math.cos(math.radians(tilt))) / 2
    
    # Ground reflection
    ground_reflected = sim.dni_clear_sky * math.sin(math.radians(solar_elev)) * sim.ground_albedo
    front_ground = ground_reflected * (1 - math.cos(math.radians(tilt))) / 2
    back_ground = ground_reflected * (1 + math.cos(math.radians(tilt))) / 2
    
    # Bifacial effect
    angle_from_vertical = abs(90 - tilt)
    bifacial_factor = 1.0 - (angle_from_vertical / 90.0)
    effective_bifaciality = sim.bifaciality_factor * (0.3 + 0.7 * bifacial_factor)
    
    print(f"  Front cos_incidence: {front_cos:.4f}")
    print(f"  Front direct: {front_direct:.1f} W/m²")
    print(f"  Front diffuse: {front_diffuse:.1f} W/m²")
    print(f"  Back diffuse: {back_diffuse:.1f} W/m²")
    print(f"  Front ground: {front_ground:.1f} W/m²")
    print(f"  Back ground: {back_ground:.1f} W/m²")
    print(f"  Angle from vertical: {angle_from_vertical}°")
    print(f"  Bifacial factor: {bifacial_factor:.3f}")
    print(f"  Effective bifaciality: {effective_bifaciality:.3f}")
    
    front_total = front_direct + front_diffuse + front_ground
    back_total = (back_diffuse + back_ground) * effective_bifaciality
    total = front_total + back_total
    power = (total / 1000) * 700
    
    print(f"  Front total: {front_total:.1f} W/m²")
    print(f"  Back total: {back_total:.1f} W/m²")
    print(f"  Total: {total:.1f} W/m²")
    print(f"  Power: {power:.1f} W")
    print()

print("PROBLÉM: Pro nízké slunce (9.7°) by vertikální panel měl být lepší!")
print("Ale 70° má lepší cos_incidence pro přímé záření...")