// Input Data: response, domain, service, annual_cost, margin_percent, discount_percent. // Commercial thresholds come from the seller, not from anVendor. const data = JSON.parse(inputData.response); const checked = new Date().toISOString(); const result = (state, pricing, evidence) => ({state, pricing, evidence, checked_at: checked}); const review = message => result("needs_review", "Needs review", message); if (data.status === "pending") { return result("awaiting_result", "Pending", "The company analysis is still running."); } if (data.status !== "done") return review(data.error || "The analysis did not complete successfully."); const domain = String(inputData.domain || "").trim().toLowerCase(); const service = String(inputData.service || "").trim().toLowerCase(); if (!domain || data.company?.domain !== domain) return review("Company domain does not match the requested account."); const date = Date.parse(data.dataDate || ""); const age = Date.now() - date; if (data.stale !== false || data.partial !== false || !Number.isFinite(date) || age < 0 || age >= 30 * 24 * 60 * 60 * 1000) { return review("Review the analysis date, freshness and coverage before pricing."); } const match = Array.isArray(data.services) && data.services.find(item => item.domain === service); if (!match) return review("No finding for the selected comparison service; this does not establish non-use."); const spend = match.estimatedAnnualSpend; const positive = number => typeof number === "number" && Number.isFinite(number) && number > 0; if (!spend || !positive(spend.lowUsd) || !positive(spend.highUsd) || spend.highUsd < spend.lowUsd) { return review("The selected service has no usable annual spend estimate."); } if (spend.openEnded !== false) return review("Review the open-ended spend estimate before setting an anchor."); const numeric = value => value !== undefined && value !== null && String(value).trim() !== "" ? Number(value) : NaN; const annualCost = numeric(inputData.annual_cost); const margin = numeric(inputData.margin_percent) / 100; const discount = numeric(inputData.discount_percent) / 100; if (!Number.isFinite(annualCost) || annualCost <= 0 || !Number.isFinite(margin) || margin < 0 || margin >= 1 || !Number.isFinite(discount) || discount < 0 || discount >= 1) { return review("Provide positive annual delivery cost and margin/discount percentages from 0 to below 100."); } // Round the cost floor up and the comparison anchor down to the nearest cent. const floor = Math.ceil(annualCost / (1 - margin) * 100) / 100; const anchor = Math.floor(spend.lowUsd * (1 - discount) * 100) / 100; if (!Number.isFinite(floor) || !Number.isFinite(anchor)) return review("The assumptions produce an invalid amount."); const usd = amount => amount.toLocaleString("en-US", {style:"currency",currency:"USD",maximumFractionDigits:2}); const assumptions = `${service}: estimated annual spend ${usd(spend.lowUsd)}–${usd(spend.highUsd)}; observed ${data.dataDate}. ` + `Seller cost ${usd(annualCost)}; margin ${margin * 100}%; comparison discount ${discount * 100}%. `; if (floor > anchor) return review(assumptions + `Cost floor ${usd(floor)} exceeds comparison anchor ${usd(anchor)}; review scope or assumptions.`); return result("complete", `${usd(floor)}–${usd(anchor)} / year`, assumptions + "Pricing discussion scenario, not a verified budget or willingness to pay. Compare equivalent scope and first-year fees.");