"""Obračun za članak. Python 3.10+, bez spoljašnjih paketa."""
from csv import writer
from decimal import Decimal as D
from pathlib import Path

VOLUMES = (1_000, 10_000, 50_000, 100_000, 1_000_000)
EXACT_PRICES = {
    "mailkube (mesečno)": {1_000: "0", 10_000: "7", 50_000: "13", 100_000: "27", 1_000_000: "489"},
    "Mail Gazelle (dostavljeni cenovnik)": {1_000: "0", 10_000: "10", 50_000: "20", 100_000: "70"},
    "SendPulse (Free / PAYG)": {1_000: "0", 10_000: "0", 50_000: "49", 100_000: "89", 1_000_000: "780"},
    "Resend (Free / Pro)": {1_000: "0", 10_000: "20", 50_000: "20", 100_000: "65"},
    "Mailjet (Free / Essential / Premium)": {1_000: "0", 10_000: "19", 50_000: "40", 100_000: "112"},
    "Elastic Email (Free / API Starter)": {1_000: "0", 10_000: "19", 50_000: "19"},
    "Mailgun (Free / Basic / Foundation / Scale)": {1_000: "0", 10_000: "15", 50_000: "35", 100_000: "90"},
}


def prices(volume: int) -> dict[str, D | None]:
    thousand = D(volume) / 1_000
    cloudflare_overage = max(D(0), thousand - 3) * D("0.35")
    fixed = {
        "Amazon SES à la carte": thousand * D("0.10"),
        "Amazon SES Essentials": thousand * D("0.16"),
        "Zoho CPaaS (potrošena vrednost kredita)": thousand * D("0.25"),
        "Cloudflare (sa Workers Paid minimumom)": D(5) + cloudflare_overage,
        "Cloudflare (dodatak ako Workers Paid već plaćaš)": cloudflare_overage,
        "Plunk Cloud (Free / PAYG)": D(0) if volume <= 1_000 else thousand,
    }
    for provider, by_volume in EXACT_PRICES.items():
        value = by_volume.get(volume)
        fixed[provider] = D(value) if value is not None else None
    return fixed


def export(path: Path) -> None:
    by_volume = {volume: prices(volume) for volume in VOLUMES}
    with path.open("w", newline="", encoding="utf-8") as handle:
        csv = writer(handle)
        csv.writerow(["scenario_usd", *VOLUMES])
        for provider in by_volume[VOLUMES[0]]:
            series = [by_volume[volume][provider] for volume in VOLUMES]
            csv.writerow([provider, *("" if value is None else f"{value:.2f}" for value in series)])


if __name__ == "__main__":
    export(Path(__file__).with_name("model_troskova_usd.csv"))
