import { n as __toESM } from "../_runtime.mjs";
import { I as require_jsx_runtime } from "../_libs/@radix-ui/react-alert-dialog+[...].mjs";
import { t as PageHeader } from "./page-header-BE1EG5aP.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { i as CardTitle, n as CardContent, r as CardHeader, t as Card } from "./card-BXjpJ96D.mjs";
import { a as TableHead, n as TableBody, o as TableHeader, r as TableCell, s as TableRow, t as Table } from "./table-BAceZcga.mjs";
import { n as useItems, r as useLatestFacts } from "./data-hooks--e_NaUze.mjs";
import { t as Badge } from "./badge-D1Dupn2y.mjs";
import { i as riskDeficit, r as compliancePct } from "./calculations-JyW9rCxF.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/rankings-BogjdnzC.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
function Rankings() {
	const { data: aen = [] } = useLatestFacts("AEN");
	const { data: sse = [] } = useLatestFacts("SSE");
	const { data: items = [] } = useItems();
	const wMap = (0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.weightage ?? 0])), [items]);
	const rank = (rows) => rows.map((r) => ({
		...r,
		compliance: r.cumScope > 0 ? r.cumProg / r.cumScope * 100 : 0,
		wCompAvg: r.w > 0 ? r.wComp / r.w : 0
	})).sort((a, b) => b.wCompAvg - a.wCompAvg);
	const divRows = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of aen) {
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const c = compliancePct(r.cum_progress, r.cum_scope);
			if (!m.has(r.division)) m.set(r.division, {
				key: r.division,
				w: 0,
				wComp: 0,
				cumScope: 0,
				cumProg: 0,
				deficit: 0
			});
			const g = m.get(r.division);
			g.w += w;
			g.wComp += c * w;
			g.cumScope += r.cum_scope;
			g.cumProg += r.cum_progress;
			g.deficit += riskDeficit(c, w);
		}
		return rank(Array.from(m.values()));
	}, [aen, wMap]);
	const aenRows = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of aen) {
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const c = compliancePct(r.cum_progress, r.cum_scope);
			const key = `${r.division}|${r.aen}`;
			if (!m.has(key)) m.set(key, {
				key,
				division: r.division,
				aen: r.aen,
				w: 0,
				wComp: 0,
				cumScope: 0,
				cumProg: 0,
				deficit: 0
			});
			const g = m.get(key);
			g.w += w;
			g.wComp += c * w;
			g.cumScope += r.cum_scope;
			g.cumProg += r.cum_progress;
			g.deficit += riskDeficit(c, w);
		}
		return rank(Array.from(m.values()));
	}, [aen, wMap]);
	const sseRows = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of sse) {
			if (!r.sse) continue;
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const c = compliancePct(r.cum_progress, r.cum_scope);
			const key = `${r.division}|${r.aen}|${r.sse}`;
			if (!m.has(key)) m.set(key, {
				key,
				division: r.division,
				aen: r.aen,
				sse: r.sse,
				w: 0,
				wComp: 0,
				cumScope: 0,
				cumProg: 0,
				deficit: 0
			});
			const g = m.get(key);
			g.w += w;
			g.wComp += c * w;
			g.cumScope += r.cum_scope;
			g.cumProg += r.cum_progress;
			g.deficit += riskDeficit(c, w);
		}
		return rank(Array.from(m.values())).slice(0, 30);
	}, [sse, wMap]);
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1600px] mx-auto",
		children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
			title: "Rankings & Scorecards",
			subtitle: "Ranked by weighted compliance (higher is better)."
		}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
			className: "grid grid-cols-1 lg:grid-cols-3 gap-4",
			children: [
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(RankCard, {
					title: "Divisions",
					rows: divRows.map((r, i) => ({
						rank: i + 1,
						name: r.key,
						sub: null,
						comp: r.compliance,
						wcomp: r.wCompAvg,
						deficit: r.deficit
					}))
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(RankCard, {
					title: "AENs",
					rows: aenRows.map((r, i) => ({
						rank: i + 1,
						name: r.aen,
						sub: r.division,
						comp: r.compliance,
						wcomp: r.wCompAvg,
						deficit: r.deficit
					}))
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(RankCard, {
					title: "SSEs (top 30)",
					rows: sseRows.map((r, i) => ({
						rank: i + 1,
						name: r.sse,
						sub: `${r.division} · ${r.aen}`,
						comp: r.compliance,
						wcomp: r.wCompAvg,
						deficit: r.deficit
					}))
				})
			]
		})]
	});
}
function RankCard({ title, rows }) {
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: title }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, {
		className: "p-0",
		children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Table, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "#" }),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Name" }),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
				className: "text-right",
				children: "Comp."
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
				className: "text-right",
				children: "Weighted"
			})
		] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [rows.map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Badge, {
				variant: "outline",
				children: ["#", r.rank]
			}) }),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
				className: "font-medium",
				children: r.name
			}), r.sub && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
				className: "text-xs text-muted-foreground",
				children: r.sub
			})] }),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
				className: "text-right",
				children: [r.comp.toFixed(1), "%"]
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableCell, {
				className: "text-right",
				children: [r.wcomp.toFixed(1), "%"]
			})
		] }, r.rank + r.name)), rows.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
			colSpan: 4,
			className: "text-center text-sm text-muted-foreground py-6",
			children: "No data."
		}) })] })] })
	})] });
}
//#endregion
export { Rankings as component };
