"""Critical Load R04 v1.0 reproducibility model.

Recomputes the public Peak Water Capacity Screen (PWCS) and project
scenario values from model_inputs.csv using Python's standard library.
This is a screening model, not a utility will-serve determination.
"""
import csv
from pathlib import Path

ROOT = Path(__file__).resolve().parent

def read_inputs():
    with (ROOT / "model_inputs.csv").open(newline="", encoding="utf-8") as f:
        rows = list(csv.DictReader(f))
    return {r["parameter"]: float(r["value"]) for r in rows}

def direct_water_mgd(project_mw, gpd_per_mw):
    return project_mw * gpd_per_mw / 1_000_000.0

def pwcs(future_capacity_mgd, allocation_share, current_dc_peak_mgd, peak_gpd_per_mw):
    budget = allocation_share * future_capacity_mgd - current_dc_peak_mgd
    intensity_mgd_per_gw = peak_gpd_per_mw / 1000.0
    return max(0.0, budget / intensity_mgd_per_gw)

def main():
    x = read_inputs()
    budget = x["allocation_share"] * x["future_permitted_capacity_mgd"] - x["current_dc_peak_mgd"]
    print(f"Incremental potable peak budget: {budget:.2f} MGD")
    for label, key in [
        ("Low", "low_peak_gpd_per_mw"),
        ("Medium", "medium_peak_gpd_per_mw"),
        ("High", "high_peak_gpd_per_mw"),
        ("Facility-high sensitivity", "facility_high_peak_gpd_per_mw"),
    ]:
        val = pwcs(x["future_permitted_capacity_mgd"], x["allocation_share"], x["current_dc_peak_mgd"], x[key])
        print(f"{label}: {val:.3f} GW screened additional IT load")

    print("\n+1 GW direct-water check:")
    for label, avg_key, peak_key in [
        ("Low", "low_avg_gpd_per_mw", "low_peak_gpd_per_mw"),
        ("Medium", "medium_avg_gpd_per_mw", "medium_peak_gpd_per_mw"),
        ("High", "high_avg_gpd_per_mw", "high_peak_gpd_per_mw"),
    ]:
        avg = direct_water_mgd(1000, x[avg_key])
        peak = direct_water_mgd(1000, x[peak_key])
        cons = peak * x["consumptive_factor"]
        print(f"{label}: avg={avg:.2f} MGD, peak={peak:.2f} MGD, peak consumption sensitivity={cons:.3f} MGD")

if __name__ == "__main__":
    main()
