import { t as writeFileAtomic } from "./atomic-write-8yjqqLtS.js";
import { t as assertSafePathSegment } from "./safe-path-mpp0dKtO.js";
import path from "path";
import fs from "fs";
import matter from "gray-matter";
import { fromZodError } from "zod-validation-error";
import { z } from "zod";
//#region src/schemas/main-facts.ts
const MainFactsSchema = z.object({
	name: z.string().min(1),
	domain: z.string().optional(),
	email: z.string().optional(),
	phone: z.string().optional(),
	industry: z.string().optional(),
	relationship_stage: z.enum([
		"prospect",
		"active",
		"churned",
		"paused"
	]),
	deal_value: z.number().optional(),
	currency: z.string().default("EUR"),
	primary_contact: z.string().optional(),
	timezone: z.string().optional(),
	tags: z.array(z.string()).default([]),
	created: z.preprocess((v) => v instanceof Date ? v.toISOString().slice(0, 10) : v, z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD required")),
	updated: z.preprocess((v) => v instanceof Date ? v.toISOString().slice(0, 10) : v, z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD required"))
});
//#endregion
//#region src/fs/customer-dir.ts
function assertSafeSlug(slug) {
	assertSafePathSegment(slug, "customer slug");
}
function getCustomerDir(dataDir, slug) {
	assertSafeSlug(slug);
	return path.join(dataDir, "customers", slug);
}
function customerExists(dataDir, slug) {
	return fs.existsSync(getCustomerDir(dataDir, slug));
}
/** List all customer slugs (immediate subdirectories of customers/). */
function listCustomerSlugs(dataDir) {
	const dir = path.join(dataDir, "customers");
	if (!fs.existsSync(dir)) return [];
	return fs.readdirSync(dir).filter((s) => {
		try {
			return fs.statSync(path.join(dir, s)).isDirectory();
		} catch {
			return false;
		}
	});
}
async function ensureCustomerDir(dataDir, slug) {
	const customerDir = getCustomerDir(dataDir, slug);
	fs.mkdirSync(customerDir, { recursive: true });
	fs.mkdirSync(path.join(customerDir, "attachments"), { recursive: true });
	fs.mkdirSync(path.join(customerDir, "transcripts"), { recursive: true });
}
async function writeMainFacts(dataDir, slug, facts) {
	const filePath = path.join(getCustomerDir(dataDir, slug), "main_facts.md");
	const clean = Object.fromEntries(Object.entries(facts).filter(([, v]) => v !== void 0));
	writeFileAtomic(filePath, matter.stringify("", clean));
}
async function readMainFacts(dataDir, slug) {
	const filePath = path.join(getCustomerDir(dataDir, slug), "main_facts.md");
	if (!fs.existsSync(filePath)) throw new Error(`main_facts.md not found for customer '${slug}'`);
	const data = matter(fs.readFileSync(filePath, "utf-8")).data;
	for (const key of ["created", "updated"]) if (data[key] instanceof Date) data[key] = data[key].toISOString().slice(0, 10);
	const result = MainFactsSchema.safeParse(data);
	if (!result.success) throw new Error(fromZodError(result.error, {
		prefix: `Schema error in ${filePath}`,
		prefixSeparator: ":\n  - ",
		issueSeparator: "\n  - "
	}).message);
	return result.data;
}
//#endregion
export { readMainFacts as a, listCustomerSlugs as i, customerExists as n, writeMainFacts as o, ensureCustomerDir as r, MainFactsSchema as s, assertSafeSlug as t };

//# sourceMappingURL=customer-dir-CkMMXhb0.js.map