import { a as readMainFacts, i as listCustomerSlugs } from "./customer-dir-CkMMXhb0.js";
import { a as summarizeLogs } from "./logger-Dyl4VcLO.js";
import path from "path";
import fs from "fs";
//#region src/core/doctor.ts
/** Recursively collect files whose name matches the atomic-write temp pattern. */
function findOrphanedTempFiles(dir, depth = 0) {
	if (depth > 3 || !fs.existsSync(dir)) return [];
	const out = [];
	let entries;
	try {
		entries = fs.readdirSync(dir);
	} catch {
		return [];
	}
	for (const entry of entries) {
		const full = path.join(dir, entry);
		let isDir = false;
		try {
			isDir = fs.statSync(full).isDirectory();
		} catch {
			continue;
		}
		if (isDir) out.push(...findOrphanedTempFiles(full, depth + 1));
		else if (/\.\d+\.[0-9a-f]+\.tmp$/.test(entry)) out.push(full);
	}
	return out;
}
/** Delete orphaned atomic-write temp files; returns the paths removed. */
function cleanupTempFiles(dataDir) {
	const temps = [...findOrphanedTempFiles(path.join(dataDir, ".agentic")), ...findOrphanedTempFiles(path.join(dataDir, "customers"))];
	const removed = [];
	for (const f of temps) try {
		fs.rmSync(f, { force: true });
		removed.push(f);
	} catch {}
	return removed;
}
async function runDiagnostics(dataDir) {
	const checks = [];
	const agenticDir = path.join(dataDir, ".agentic");
	const customersDir = path.join(dataDir, "customers");
	if (!fs.existsSync(agenticDir) && !fs.existsSync(customersDir)) checks.push({
		name: "data directory",
		status: "fail",
		detail: `Neither .agentic/ nor customers/ found under ${dataDir} — run 'dxcrm init'`
	});
	else checks.push({
		name: "data directory",
		status: "ok",
		detail: dataDir
	});
	const slugs = listCustomerSlugs(dataDir);
	const invalid = [];
	for (const slug of slugs) try {
		await readMainFacts(dataDir, slug);
	} catch {
		invalid.push(slug);
	}
	checks.push({
		name: "customer data",
		status: invalid.length > 0 ? "fail" : "ok",
		detail: invalid.length > 0 ? `${invalid.length} of ${slugs.length} invalid: ${invalid.slice(0, 5).join(", ")}` : `${slugs.length} customer(s) valid`
	});
	const temps = [...findOrphanedTempFiles(agenticDir), ...findOrphanedTempFiles(customersDir)];
	checks.push({
		name: "temp files",
		status: temps.length > 0 ? "warn" : "ok",
		detail: temps.length > 0 ? `${temps.length} orphaned temp file(s) from interrupted writes — safe to delete` : "no orphaned temp files"
	});
	const summary = summarizeLogs(dataDir);
	const errorCount = summary.byLevel.error;
	checks.push({
		name: "logs",
		status: errorCount > 0 ? "warn" : "ok",
		detail: errorCount > 0 ? `${errorCount} error entr${errorCount === 1 ? "y" : "ies"} in the log (dxcrm logs --level error)` : `${summary.total} log entr${summary.total === 1 ? "y" : "ies"}, no errors`
	});
	const backupLogPath = path.join(agenticDir, "backup-log.json");
	if (fs.existsSync(backupLogPath)) try {
		const entries = JSON.parse(fs.readFileSync(backupLogPath, "utf-8"));
		const last = entries[entries.length - 1]?.createdAt;
		const ageDays = last ? Math.floor((Date.now() - new Date(last).getTime()) / 864e5) : Infinity;
		checks.push({
			name: "backups",
			status: ageDays > 7 ? "warn" : "ok",
			detail: last ? `last backup ${ageDays}d ago` : "no backups recorded"
		});
	} catch {
		checks.push({
			name: "backups",
			status: "warn",
			detail: "backup log unreadable"
		});
	}
	return {
		ok: !checks.some((c) => c.status === "fail"),
		checks
	};
}
//#endregion
export { runDiagnostics as n, cleanupTempFiles as t };

//# sourceMappingURL=doctor-BFeelnq8.js.map