/*
   Travel Visa Signature — Savings Calculator (V2)
   Live "how much you could save" estimator: spend sliders + live totals + CTA.
*/
const { useState, useMemo } = React;

const TIERS = [
  { key: "gs",    rate: 0.10, name: "Good Sam affiliated campgrounds", where: "on reservations at Good Sam affiliated campgrounds", ref: 3,    max: 10000, step: 100, default: 1500 },
  { key: "cw",    rate: 0.05, name: "Family of brands",                where: "at Camping World & Good Sam family of brands",       ref: 5,    max: 10000, step: 100, default: 2000 },
  { key: "groc",  rate: 0.03, name: "Groceries",                       where: "at grocery stores",                                  ref: 4,    max: 10000, step: 100, default: 4000 },
  { key: "gas",   rate: 0.03, name: "Gas and EV charging stations",    where: "at gas and EV charging stations",                    ref: 4,    max: 10000, step: 100, default: 3000 },
  { key: "othcg", rate: 0.03, name: "Other campgrounds",               where: "at other campgrounds",                               ref: null, max: 10000, step: 100, default: 1500 },
];

const ELITE_VALUE        = 149;
const ELITE_BENEFITS_AVG = 1000;

const fmt = (n) => "$" + Math.round(n).toLocaleString();

function TravelSavingsCalculator() {
  const [spend, setSpend] = useState(() =>
    Object.fromEntries(TIERS.map((t) => [t.key, t.default]))
  );

  const totals = useMemo(() => {
    const lines        = TIERS.map((t) => ({ ...t, spend: spend[t.key], earned: spend[t.key] * t.rate }));
    const rewardsEarned = lines.reduce((s, t) => s + t.earned, 0);
    const pointsTotal   = Math.round(rewardsEarned * 100);
    const displayTotal  = rewardsEarned + ELITE_VALUE + ELITE_BENEFITS_AVG;
    return { lines, rewardsEarned, pointsTotal, displayTotal };
  }, [spend]);

  return (
    <div className="calc-card">
      <div className="calc-inputs">
        {totals.lines.map((t) => (
          <div className="calc-row" key={t.key}>
            <label htmlFor={"calc-" + t.key} className="calc-row-title">
              <strong>{Math.round(t.rate * 100)}% back in points</strong>
              <span className="calc-row-where">{t.where}{t.ref ? <sup>{t.ref}</sup> : null}</span>
            </label>
            <div className="calc-input-row">
              <input
                id={"calc-" + t.key}
                type="range"
                min="0"
                max={t.max}
                step={t.step}
                value={t.spend}
                onChange={(e) => setSpend((s) => ({ ...s, [t.key]: Number(e.target.value) }))}
                aria-label={Math.round(t.rate * 100) + "% back " + t.where + " — annual spend"}
                style={{ "--progress": ((t.spend / t.max) * 100) + "%" }}
              />
              <output className="calc-spend-value" htmlFor={"calc-" + t.key}>
                {fmt(t.spend)}<span className="calc-unit"> / yr</span>
              </output>
            </div>
          </div>
        ))}
      </div>

      <aside className="calc-summary" aria-live="polite">
        <div className="calc-headline">
          <div className="calc-headline-label">Your estimated rewards:</div>
          <div className="calc-headline-value">
            {fmt(totals.displayTotal)}
            <span className="calc-headline-unit"> / year</span>
          </div>
          <div className="calc-headline-points">
            = {totals.pointsTotal.toLocaleString()} points + savings
          </div>
        </div>

        <div className="calc-tier-block">
          <ul className="calc-tier-breakdown">
            {totals.lines.map((t) => (
              <li key={t.key}>
                <span className="calc-tier-name">{t.name}</span>
                <span className="calc-tier-earn">{fmt(t.earned)}</span>
              </li>
            ))}
          </ul>
        </div>

        <div className="calc-tier-block">
          <ul className="calc-tier-breakdown">
            <li>
              <span className="calc-tier-name">Elite Membership included</span>
              <span className="calc-tier-earn">${ELITE_VALUE}</span>
            </li>
            <li>
              <span className="calc-tier-name">Average Elite member benefits</span>
              <span className="calc-tier-earn">${ELITE_BENEFITS_AVG.toLocaleString()}+</span>
            </li>
          </ul>
        </div>

        <a href="#" className="btn btn-primary apply-now">Apply now</a>
        <p className="calc-foot">Actual rewards vary by redemption choice.</p>
      </aside>
    </div>
  );
}

window.TravelSavingsCalculator = TravelSavingsCalculator;
