"""Generate src/shipcalc/generated/rates_table.py from data/rates.csv.

Usage: python scripts/gen_rates.py data/rates.csv src/shipcalc/generated/rates_table.py
"""
import csv
import sys

HEADER = """# DO NOT EDIT. This file is generated by scripts/gen_rates.py from data/rates.csv.
# Edit the CSV and run `make gen` instead. CI fails if this file is out of date.
"""


def main(src: str, dest: str) -> None:
    with open(src, newline="") as fh:
        rows = list(csv.DictReader(fh))
    lines = [HEADER, "RATES = {"]
    for row in rows:
        lines.append(
            f'    "{row["region"]}": {{"currency": "{row["currency"]}", '
            f'"base_rate": {float(row["base_rate"])}, "per_kg": {float(row["per_kg"])}, '
            f'"free_threshold": {float(row["free_threshold"])}}},'
        )
    lines.append("}")
    with open(dest, "w") as fh:
        fh.write("\n".join(lines) + "\n")


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2])
