"""Reproduce the Outbound Index plan-fit thresholds. Python 3, standard library.

python3 outbound-plan-fit.py > outbound-plan-fit.csv
python3 outbound-plan-fit.py --json > outboundPlanFit.json
The CSV and the website JSON use the same enumeration.
"""
import argparse
import csv
import json
import sys

CHECKED = "2026-12-05"
BUDGET_USD = 110
GRID_MAX = 30_000
SOURCES = {
    "Instantly": {
        "pricing": "https://instantly.ai/pricing",
        "counting": "https://help.instantly.ai/en/articles/7920548-outbound-plans-comparison",
    },
    "Smartlead": {
        "pricing": "https://www.smartlead.ai/pricing",
        "counting": "https://helpcenter.smartlead.ai/en/articles/392-managing-lead-credits-and-email-credits-quota-in-smartlead",
    },
}
# Month-to-month standalone campaign subscriptions. No extensions or bundles.
PLANS = [
    ("Instantly", "Growth", 47, 1_000, 5_000),
    ("Instantly", "Hypergrowth", 107, 25_000, 125_000),
    ("Smartlead", "Base", 39, 2_000, 6_000),
    ("Smartlead", "Pro", 94, 30_000, 90_000),
]
EMAILS = [2, 3, 4, 5]
PRIOR_BATCHES = [0, 1]


def benchmark():
    rows = []
    for emails in EMAILS:
        for prior in PRIOR_BATCHES:
            for vendor, plan, usd, slots, sends in PLANS:
                if usd > BUDGET_USD:
                    continue
                maximum = max(
                    n for n in range(1, GRID_MAX + 1)
                    if n * (1 + prior) <= slots and n * emails <= sends
                )
                following = maximum + 1
                exceeded = []
                if following * (1 + prior) > slots:
                    exceeded.append("contacts")
                if following * emails > sends:
                    exceeded.append("emails")
                assert exceeded, "Extend the grid to locate the plan boundary."
                rows.append({
                    "vendor": vendor, "plan": plan, "monthly_usd": usd,
                    "contact_cap": slots, "email_cap": sends,
                    "emails_per_new_contact": emails, "prior_batches_retained": prior,
                    "max_new_contacts": maximum,
                    "first_excluded_new_contacts": following,
                    "exceeded_at_next_contact": " and ".join(exceeded),
                    "checked": CHECKED, "pricing_source": SOURCES[vendor]["pricing"],
                    "counting_source": SOURCES[vendor]["counting"],
                })
    return rows


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--json", action="store_true", help="emit the site's data")
    args = parser.parse_args()
    rows = benchmark()
    if args.json:
        print(json.dumps({
            "budget": BUDGET_USD, "checked": CHECKED, "gridMax": GRID_MAX,
            "emails": EMAILS, "priorBatches": PRIOR_BATCHES, "sources": SOURCES,
            "plans": PLANS, "rows": rows,
        }, indent=2))
    else:
        writer = csv.DictWriter(sys.stdout, fieldnames=rows[0].keys(), lineterminator="\n")
        writer.writeheader()
        writer.writerows(rows)
