import { i as writeAuditEntry, n as getActor } from "./audit-log-DNMY9mUZ.js";
import { o as enforceRbac } from "./rbac-msmBc_tK.js";
import { r as upsertDeal } from "./pipeline-writer-CIllfnZl.js";
import { z } from "zod";
//#region src/mcp/tools/update-deal.ts
const DATA_DIR = process.env["DXCRM_DATA_DIR"] ?? process.cwd();
async function handleUpdateDeal(input, dataDir = DATA_DIR) {
	const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
	const deal = {
		name: input.dealName,
		stage: input.stage ?? "lead",
		currency: "EUR",
		updated: today,
		...input.value !== void 0 ? { value: input.value } : {},
		...input.probability !== void 0 ? { probability: input.probability } : {},
		...input.closeDate !== void 0 ? { close_date: input.closeDate } : {},
		...input.notes !== void 0 ? { notes: input.notes } : {}
	};
	try {
		enforceRbac(dataDir, "update_deal");
		await upsertDeal(dataDir, input.slug, deal);
		writeAuditEntry(dataDir, {
			timestamp: (/* @__PURE__ */ new Date()).toISOString(),
			actor: getActor(),
			tool: "update_deal",
			slug: input.slug,
			summary: input.dealName
		});
		return { content: [{
			type: "text",
			text: JSON.stringify({
				success: true,
				deal
			}, null, 2)
		}] };
	} catch (err) {
		return { content: [{
			type: "text",
			text: JSON.stringify({
				success: false,
				error: err.message
			}, null, 2)
		}] };
	}
}
function registerUpdateDeal(server) {
	server.registerTool("update_deal", {
		title: "Update Deal",
		description: `Update or create a deal in the customer's pipeline. Upserts by deal name.
Use after pipeline discussions or when deal status changes.

Args:
  slug: Customer ID
  dealName: Deal name (used as unique key for upsert)
  stage: Deal stage ("lead" | "qualified" | "proposal" | "negotiation" | "won" | "lost")
  value: Deal value in euros
  probability: Win probability percentage (0-100)
  closeDate: Expected close date (YYYY-MM-DD)
  notes: Free-text notes about the deal

Returns: { success: boolean, deal: object }`,
		inputSchema: z.object({
			slug: z.string().describe("Customer slug (e.g. 'acme-corp')"),
			dealName: z.string().describe("Deal name (unique identifier for upsert)"),
			stage: z.enum([
				"lead",
				"qualified",
				"proposal",
				"negotiation",
				"won",
				"lost"
			]).optional().describe("Deal stage"),
			value: z.number().optional().describe("Deal value in euros"),
			probability: z.number().min(0).max(100).optional().describe("Win probability (0-100)"),
			closeDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional().describe("Expected close date (YYYY-MM-DD)"),
			notes: z.string().optional().describe("Free-text notes about the deal")
		})
	}, async ({ slug, dealName, stage, value, probability, closeDate, notes }) => handleUpdateDeal({
		slug,
		dealName,
		...stage !== void 0 ? { stage } : {},
		...value !== void 0 ? { value } : {},
		...probability !== void 0 ? { probability } : {},
		...closeDate !== void 0 ? { closeDate } : {},
		...notes !== void 0 ? { notes } : {}
	}));
}
//#endregion
export { registerUpdateDeal as n, handleUpdateDeal as t };

//# sourceMappingURL=update-deal-DSzr_Aau.js.map