/* ============================================================================
   PRICING — data + math only. The screen (checkpoint 3) renders this.
   Visible price = tier.price × complexity.mult. Fixed prices are anchors.
   ============================================================================ */
const PRICING = {
  currency: "USD",
  tiers: [
    { id: "t1", n: 1, price: 4000, name: "UI", includes: ["UI"] },
    { id: "t2", n: 2, price: 10000, name: "UI + Backend", includes: ["UI", "Backend"] },
    { id: "t3", n: 3, price: 20000, name: "UI + Backend + IP rights", includes: ["UI", "Backend", "IP rights"] },
    { id: "t4", n: 4, price: 40000, name: "UI + Backend + IP + Deploy", includes: ["UI", "Backend", "IP rights", "Deployment"] },
    { id: "t5", n: 5, price: 120000, name: "T4 + Customization", includes: ["UI", "Backend", "IP rights", "Deployment", "Customization"] },
    { id: "t6", n: 6, price: 400000, name: "T4 + Customization + 12-mo Support", includes: ["UI", "Backend", "IP rights", "Deployment", "Customization", "12-month Support"] },
  ],
  backendOnly: { id: "be", price: 8000, name: "Backend only", includes: ["Backend"] },
  complexity: [
    { id: "s", label: "Small", range: "< 5k LOC", mult: 1.0 },
    { id: "m", label: "Medium", range: "5k–25k LOC", mult: 1.5 },
    { id: "l", label: "Large", range: "25k–100k LOC", mult: 2.5 },
    { id: "xl", label: "Custom", range: "> 100k LOC", mult: 4.0 },
  ],
};

function tierById(id) { return PRICING.tiers.find((t) => t.id === id) || PRICING.tiers[1]; }
function complexityById(id) { return PRICING.complexity.find((c) => c.id === id) || PRICING.complexity[1]; }

function tierPrice(tierId, complexityId) {
  return tierById(tierId).price * complexityById(complexityId).mult;
}

/* ROI math — conservative 18% incremental margin on revenue lift. */
function roiModel({ turnover, efficiencyGain, revenueLift, tierId, complexityId }) {
  const annualSavings = turnover * efficiencyGain;
  const annualRevenueLift = turnover * revenueLift;
  const annualMarginUplift = annualRevenueLift * 0.18;
  const annualBenefit = annualSavings + annualMarginUplift;
  const price = tierPrice(tierId, complexityId);
  const paybackMonths = annualBenefit > 0 ? Math.max(1, Math.round(price / (annualBenefit / 12))) : 0;
  const roi3yr = annualBenefit > 0 ? (annualBenefit * 3) / price : 0;
  return { annualSavings, annualRevenueLift, annualMarginUplift, annualBenefit, price, paybackMonths, roi3yr };
}

function roiSubline(roi3yr, paybackMonths) {
  if (roi3yr >= 5) return `You earn the build back in ${paybackMonths} months. Then it compounds.`;
  if (roi3yr >= 2) return "The build pays for itself inside the first year.";
  return "Choose a tier or complexity that fits your scale — the ROI scales with revenue.";
}

window.TALYA_PRICING = { PRICING, tierById, complexityById, tierPrice, roiModel, roiSubline };
