import { t as writeFileAtomic } from "./atomic-write-8yjqqLtS.js";
import path from "path";
import fs from "fs";
import yaml from "js-yaml";
//#region src/core/quote-generator.ts
function quotesDir(dataDir) {
	return path.join(dataDir, ".agentic", "quotes");
}
function loadQuoteConfig(dataDir) {
	const p = path.join(dataDir, ".agentic", "quote-config.yaml");
	if (!fs.existsSync(p)) return {};
	try {
		return yaml.load(fs.readFileSync(p, "utf-8")) ?? {};
	} catch {
		return {};
	}
}
function nextQuoteNumber(dataDir) {
	const year = (/* @__PURE__ */ new Date()).getFullYear();
	const dir = quotesDir(dataDir);
	if (!fs.existsSync(dir)) {
		fs.mkdirSync(dir, { recursive: true });
		return `Q-${year}-001`;
	}
	const existing = fs.readdirSync(dir).filter((f) => f.endsWith(".json") && f.startsWith(`Q-${year}-`)).map((f) => parseInt(f.replace(`Q-${year}-`, "").replace(".json", ""), 10)).filter((n) => !isNaN(n));
	const max = existing.length > 0 ? Math.max(...existing) : 0;
	return `Q-${year}-${String(max + 1).padStart(3, "0")}`;
}
function addDaysToDate(isoDate, days) {
	const [year, month, day] = isoDate.slice(0, 10).split("-").map(Number);
	const d = new Date(Date.UTC(year, month - 1, day));
	d.setUTCDate(d.getUTCDate() + days);
	return d.toISOString().slice(0, 10);
}
function buildHtml(quote, config, customerName) {
	const lineRows = quote.lineItems.map((item) => `<tr><td>${item.description}</td><td style="text-align:right">${item.quantity}</td><td style="text-align:right">${item.unitPrice.toFixed(2)} ${quote.currency}</td><td style="text-align:right">${item.total.toFixed(2)} ${quote.currency}</td></tr>`).join("\n");
	return `<!DOCTYPE html>
<html lang="de">
<head><meta charset="UTF-8"><title>Angebot ${quote.quoteNumber}</title>
<style>body{font-family:Arial,sans-serif;max-width:800px;margin:40px auto;color:#222}table{width:100%;border-collapse:collapse}th,td{padding:8px 12px;border:1px solid #ddd}th{background:#f5f5f5}h1{color:#1a1a2e}.total{font-weight:bold;font-size:1.1em}</style>
</head>
<body>
<h1>Angebot ${quote.quoteNumber}</h1>
<p><strong>${config.companyName ?? ""}</strong><br>${config.companyAddress ?? ""}<br>${config.vatId ? `USt-IdNr.: ${config.vatId}` : ""}</p>
<hr>
<p><strong>An:</strong> ${customerName}</p>
<p><strong>Date:</strong> ${quote.createdAt.slice(0, 10)} &nbsp;&nbsp; <strong>Valid until:</strong> ${quote.validUntil}</p>
<h2>Leistungen</h2>
<table>
<thead><tr><th>Beschreibung</th><th style="text-align:right">Menge</th><th style="text-align:right">Einzelpreis</th><th style="text-align:right">Gesamt</th></tr></thead>
<tbody>${lineRows}</tbody>
</table>
<br>
<table style="width:300px;margin-left:auto">
<tr><td>Nettobetrag</td><td style="text-align:right">${quote.subtotal.toFixed(2)} ${quote.currency}</td></tr>
<tr><td>MwSt. (${quote.vatPercent}%)</td><td style="text-align:right">${quote.vat.toFixed(2)} ${quote.currency}</td></tr>
<tr class="total"><td><strong>Gesamtbetrag</strong></td><td style="text-align:right"><strong>${quote.total.toFixed(2)} ${quote.currency}</strong></td></tr>
</table>
<br><p>${config.paymentTerms ?? ""}</p>
<hr><small>${config.footerText ?? ""}</small>
</body></html>`;
}
function readCustomerName(dataDir, slug) {
	const p = path.join(dataDir, "customers", slug, "main_facts.md");
	if (!fs.existsSync(p)) return slug;
	const content = fs.readFileSync(p, "utf-8");
	return /^name:\s*(.+)$/m.exec(content)?.[1]?.trim() ?? slug;
}
function readQuote(dataDir, quoteNumber) {
	const p = path.join(quotesDir(dataDir), `${quoteNumber}.json`);
	if (!fs.existsSync(p)) return null;
	try {
		return JSON.parse(fs.readFileSync(p, "utf-8"));
	} catch {
		return null;
	}
}
function listQuotes(dataDir, slug) {
	const dir = quotesDir(dataDir);
	if (!fs.existsSync(dir)) return [];
	return fs.readdirSync(dir).filter((f) => f.endsWith(".json")).flatMap((f) => {
		try {
			const q = JSON.parse(fs.readFileSync(path.join(dir, f), "utf-8"));
			return slug === void 0 || q.slug === slug ? [q] : [];
		} catch {
			return [];
		}
	});
}
async function generateQuote(dataDir, input) {
	const config = loadQuoteConfig(dataDir);
	const vatPercent = input.vatPercent ?? 19;
	const validUntilDays = input.validUntilDays ?? 30;
	const currency = input.currency ?? config.currency ?? "EUR";
	const items = input.lineItems.map((item) => ({
		description: item.description,
		quantity: item.quantity,
		unitPrice: item.unitPrice,
		total: item.quantity * item.unitPrice
	}));
	const subtotal = items.reduce((sum, i) => sum + i.total, 0);
	const vat = Math.round(subtotal * (vatPercent / 100) * 100) / 100;
	const total = Math.round((subtotal + vat) * 100) / 100;
	const quoteNumber = nextQuoteNumber(dataDir);
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const validUntil = addDaysToDate(now.slice(0, 10), validUntilDays);
	const dir = quotesDir(dataDir);
	fs.mkdirSync(dir, { recursive: true });
	const htmlPath = path.join(dir, `${quoteNumber}.html`);
	const quote = {
		quoteNumber,
		slug: input.slug,
		dealName: input.dealName,
		lineItems: items,
		subtotal,
		vatPercent,
		vat,
		total,
		currency,
		createdAt: now,
		validUntilDays,
		validUntil,
		status: "draft",
		htmlPath
	};
	writeFileAtomic(path.join(dir, `${quoteNumber}.json`), JSON.stringify(quote, null, 2));
	writeFileAtomic(htmlPath, buildHtml(quote, config, readCustomerName(dataDir, input.slug)));
	return quote;
}
//#endregion
export { listQuotes as n, readQuote as r, generateQuote as t };

//# sourceMappingURL=quote-generator-CTdR8eEI.js.map