"""Deterministic scenarios, Energy Atlas, 13 November 2026.

Run with python3 ptolemaida5-elegxoi-2026-11-13.py in a writable directory.
The outputs are sensitivities. Input market quotes require settlement verification.
"""

import csv
from pathlib import Path


def coal_variable_cost(eua):
    return (6.5 / 4.3) * 31 + 1.05 * eua + 12


def cgt_variable_cost(ttf, eua):
    return ttf / 0.5844 + 0.35 * eua + 6


def write_csv(output, name, header, rows):
    with (output / name).open("w", encoding="utf-8-sig", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(header)
        writer.writerows(rows)


def main():
    output = Path.cwd()
    eua = 86.55
    coal = coal_variable_cost(eua)
    cgt_other = cgt_variable_cost(0, eua)
    crf = 0.07 / (1 - 1.07 ** -25)
    annual_twh = 1.55
    scenarios = [(324.3, 1), (372.15, 1), (401.6, 1), (420, 1), (401.6, 0)]
    write_csv(
        output, "ptolemaida5-kostos-2026-11-13.csv",
        ["scenario_TTF_eur_MWhth", "scenario_EUA_eur_t", "perimeter_m_eur",
         "avoidable_share", "coal_variable_eur_MWhe", "CCGT_variable_eur_MWhe",
         "CCGT_capital_eur_MWhe", "CCGT_total_eur_MWhe", "break_even_TTF_eur_MWhth"],
        (
            [ttf, eua, amount, avoidable, f"{coal:.5f}", f"{gas:.5f}",
             f"{capital:.5f}", f"{gas + capital:.5f}", f"{(coal - cgt_other - capital) * 0.5844:.5f}"]
            for ttf in (30.98, 35, 43.10, 69.15, 85.59, 100)
            for gas in [cgt_variable_cost(ttf, eua)]
            for amount, avoidable in scenarios
            for capital in [amount * avoidable * crf / annual_twh]
        ),
    )
    kwh = 2942
    alpha = 0.70
    vat = 0.06
    write_csv(
        output, "ptolemaida5-lianiki-2026-11-13.csv",
        ["change_spot_after_minus_before_eur_MWh", "annual_consumption_kWh",
         "scenario_total_pass_through", "VAT", "change_energy_charge_eur"],
        ([spot, kwh, alpha, vat, f"{spot * (kwh / 1000) * alpha * (1 + vat):.5f}"]
         for spot in (-20, -10, -5, -2, 0, 2, 5, 10, 20)),
    )


if __name__ == "__main__":
    main()
