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, i as TableFooter, 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 { t as Button } from "./button-BLZ6ednA.mjs";
import { h as Printer } from "../_libs/lucide-react.mjs";
import { a as SelectValue, i as SelectTrigger, n as SelectContent, r as SelectItem, t as Select } from "./select-CYB-gyWu.mjs";
import { o as zoneClass, r as compliancePct } from "./calculations-JyW9rCxF.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/aen-performance-Cpse0JhD.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
var printCss = `@media print {
  body * { visibility: hidden; }
  #print-area, #print-area * { visibility: visible; }
  #print-area { position: absolute; left: 0; top: 0; width: 100%; }
  .no-print { display: none !important; }
}`;
function AenPerf() {
	const { data: sseFacts = [] } = useLatestFacts("SSE");
	const { data: items = [] } = useItems();
	const wMap = (0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.weightage ?? 0])), [items]);
	(0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.name])), [items]);
	(0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.sort_order])), [items]);
	(0, import_react.useMemo)(() => new Map(items.map((i) => [i.sr_no, i.unit])), [items]);
	const [division, setDivision] = (0, import_react.useState)("all");
	const [aen, setAen] = (0, import_react.useState)("all");
	const divisions = (0, import_react.useMemo)(() => Array.from(new Set(sseFacts.map((r) => r.division))).sort(), [sseFacts]);
	const aens = (0, import_react.useMemo)(() => {
		const rows = division === "all" ? sseFacts : sseFacts.filter((r) => r.division === division);
		return Array.from(new Set(rows.map((r) => r.aen))).sort();
	}, [sseFacts, division]);
	const aenRanking = (0, import_react.useMemo)(() => {
		const rows = division === "all" ? sseFacts : sseFacts.filter((r) => r.division === division);
		const m = /* @__PURE__ */ new Map();
		for (const r of rows) {
			const w = wMap.get(r.item_sr_no) ?? r.weightage ?? 0;
			const key = `${r.division}|${r.aen}`;
			if (!m.has(key)) m.set(key, {
				division: r.division,
				aen: r.aen,
				wScope: 0,
				wProg: 0,
				scope: 0,
				prog: 0
			});
			const g = m.get(key);
			g.wScope += r.cum_scope * w;
			g.wProg += r.cum_progress * w;
			g.scope += r.cum_scope;
			g.prog += r.cum_progress;
		}
		return Array.from(m.values()).map((g) => ({
			...g,
			riskComp: g.wScope > 0 ? g.wProg / g.wScope * 100 : 0
		})).sort((a, b) => b.riskComp - a.riskComp).map((r, i) => ({
			...r,
			rank: i + 1
		}));
	}, [
		sseFacts,
		division,
		wMap
	]);
	const itemRows = (0, import_react.useMemo)(() => {
		if (aen === "all") return [];
		const rows = sseFacts.filter((r) => r.aen === aen && (division === "all" || r.division === division));
		const agg = /* @__PURE__ */ new Map();
		for (const r of rows) {
			if (!agg.has(r.item_sr_no)) agg.set(r.item_sr_no, {
				scope: 0,
				prog: 0,
				unit: r.unit
			});
			const g = agg.get(r.item_sr_no);
			g.scope += r.cum_scope;
			g.prog += r.cum_progress;
			if (!g.unit && r.unit) g.unit = r.unit;
		}
		return [...items].sort((a, b) => a.sort_order - b.sort_order).map((it) => {
			const g = agg.get(it.sr_no) ?? {
				scope: 0,
				prog: 0,
				unit: null
			};
			const w = it.weightage ?? 0;
			const c = compliancePct(g.prog, g.scope);
			return {
				sr: it.sr_no,
				name: it.name,
				unit: it.unit ?? g.unit,
				weight: w,
				scope: g.scope,
				prog: g.prog,
				compliance: c,
				riskComp: c * w
			};
		});
	}, [
		sseFacts,
		aen,
		division,
		items
	]);
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1600px] mx-auto",
		children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { children: printCss }),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
				title: "AEN Performance",
				subtitle: "Ranked by risk-weighted compliance"
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Card, {
				className: "mb-4 no-print",
				children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardContent, {
					className: "p-4 grid grid-cols-1 md:grid-cols-3 gap-3",
					children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", {
							className: "text-xs text-muted-foreground",
							children: "Division"
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Select, {
							value: division,
							onValueChange: (v) => {
								setDivision(v);
								setAen("all");
							},
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectTrigger, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectValue, {}) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SelectContent, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectItem, {
								value: "all",
								children: "All divisions"
							}), divisions.map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectItem, {
								value: d,
								children: d
							}, d))] })]
						})] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", {
							className: "text-xs text-muted-foreground",
							children: "AEN"
						}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Select, {
							value: aen,
							onValueChange: setAen,
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectTrigger, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectValue, {}) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(SelectContent, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectItem, {
								value: "all",
								children: "All AENs (show ranking)"
							}), aens.map((a) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SelectItem, {
								value: a,
								children: a
							}, a))] })]
						})] }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
							className: "flex items-end",
							children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Button, {
								onClick: () => window.print(),
								variant: "outline",
								className: "w-full",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Printer, { className: "h-4 w-4 mr-2" }), " Print / PDF"]
							})
						})
					]
				})
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				id: "print-area",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "hidden print:block mb-4",
					children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", {
						className: "text-xl font-bold",
						children: "AEN Performance Report"
					}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
						className: "text-sm",
						children: [
							"Division: ",
							division === "all" ? "All" : division,
							" · AEN: ",
							aen === "all" ? "All" : aen
						]
					})]
				}), aen === "all" ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardTitle, { children: [
					"AEN Ranking ",
					division !== "all" && `· ${division}`,
					" (",
					aenRanking.length,
					")"
				] }) }), /* @__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: "Rank" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Division" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "AEN" }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Cum. Scope"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Cum. Progress"
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
							className: "text-right",
							children: "Risk Compliance %"
						})
					] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [aenRanking.map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, {
						className: "cursor-pointer hover:bg-muted/50",
						onClick: () => {
							setDivision(r.division);
							setAen(r.aen);
						},
						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.jsx)(TableCell, { children: r.division }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "font-medium",
								children: r.aen
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.scope.toFixed(0)
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.prog.toFixed(0)
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", {
									className: `inline-block rounded px-2 py-0.5 text-xs ${zoneClass(r.riskComp)}`,
									children: [r.riskComp.toFixed(1), "%"]
								})
							})
						]
					}, `${r.division}-${r.aen}`)), aenRanking.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
						colSpan: 6,
						className: "text-center py-8 text-sm text-muted-foreground",
						children: "No data."
					}) })] })] })
				})] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CardTitle, { children: [
					"Item-wise Risk Compliance · ",
					aen,
					" (",
					itemRows.length,
					")"
				] }) }), /* @__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: "Sr" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Item" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, { children: "Unit" }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Weight"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Cum. Scope"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Cum. Progress"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Compliance %"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableHead, {
								className: "text-right",
								children: "Risk Comp."
							})
						] }) }),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableBody, { children: [itemRows.map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, { children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: r.sr }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "font-medium",
								children: r.name
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { children: r.unit }),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.weight.toFixed(2)
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.scope.toFixed(0)
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.prog.toFixed(0)
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", {
									className: `inline-block rounded px-2 py-0.5 text-xs ${zoneClass(r.compliance)}`,
									children: [r.compliance.toFixed(1), "%"]
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
								className: "text-right",
								children: r.riskComp.toFixed(1)
							})
						] }, r.sr)), itemRows.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableRow, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
							colSpan: 8,
							className: "text-center py-8 text-sm text-muted-foreground",
							children: "No items for this AEN."
						}) })] }),
						itemRows.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableFooter, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TableRow, {
							className: "font-semibold bg-muted/50",
							children: [
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
									colSpan: 4,
									children: "Total"
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
									className: "text-right",
									children: itemRows.reduce((s, r) => s + r.scope, 0).toFixed(0)
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, {
									className: "text-right",
									children: itemRows.reduce((s, r) => s + r.prog, 0).toFixed(0)
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TableCell, { colSpan: 2 })
							]
						}) })
					] })
				})] })]
			})
		]
	});
}
//#endregion
export { AenPerf as component };
