import { i as listCustomerSlugs } from "./customer-dir-CkMMXhb0.js";
import { t as computeCustomerHealth } from "./relationship-health-ZZNXR1RZ.js";
//#region src/core/churn.ts
function levelFromScore(score) {
	if (score >= 60) return "high";
	if (score >= 30) return "medium";
	return "low";
}
/**
* Assess one customer's churn risk. Risk is the inverse of relationship health,
* amplified by explicit risk flags (no-contact windows, silent champions) and
* declining/cold trends across the customer's contacts.
*/
function assessChurn(dataDir, slug, today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)) {
	const health = computeCustomerHealth(dataDir, slug, today);
	const signals = [];
	let risk = 100 - health.overallHealth;
	if (health.contacts.length === 0) {
		signals.push("No logged interactions — relationship never established");
		risk = Math.max(risk, 65);
	}
	const flagged = /* @__PURE__ */ new Set();
	for (const c of health.contacts) {
		for (const f of c.riskFlags) flagged.add(f);
		if (c.trend === "cold") signals.push(`${c.name} has gone cold (${c.daysSinceContact}d silent)`);
		else if (c.trend === "declining") signals.push(`${c.name}'s engagement is declining`);
	}
	if (flagged.has("NO_CONTACT_30D")) {
		signals.push("A key contact has had no contact in 30+ days");
		risk += 15;
	} else if (flagged.has("NO_CONTACT_14D")) {
		signals.push("A key contact has had no contact in 14+ days");
		risk += 5;
	}
	if (flagged.has("CHAMPION_SILENT")) {
		signals.push("Champion has gone silent — high-leverage risk");
		risk += 20;
	}
	const riskScore = Math.max(0, Math.min(100, Math.round(risk)));
	return {
		slug,
		riskScore,
		level: levelFromScore(riskScore),
		signals
	};
}
/** Assess every customer and return them ranked by churn risk (highest first). */
function scanChurn(dataDir, today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)) {
	return listCustomerSlugs(dataDir).map((slug) => assessChurn(dataDir, slug, today)).sort((a, b) => b.riskScore - a.riskScore);
}
//#endregion
export { assessChurn, scanChurn };

//# sourceMappingURL=churn-DN9WDGNM.js.map