import { randomUUID } from "node:crypto";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { getSession } from "../lib/neo4j.js";
import { computeEmbedding } from "../lib/embedding.js";
import cronParser from "cron-parser";
const { parseExpression } = cronParser;
export async function scheduleEvent(params) {
    const { accountId, name, description, startDate, endDate, location, recurrence, timezone, sessionKey, action, } = params;
    // Validate action if provided — plugin directory must exist
    if (action) {
        if (!action.plugin || !action.tool) {
            throw new Error("Action requires both 'plugin' and 'tool' fields");
        }
        const platformRoot = process.env.PLATFORM_ROOT;
        if (!platformRoot) {
            throw new Error("PLATFORM_ROOT environment variable is required to validate action plugin");
        }
        const pluginsRoot = resolve(platformRoot, "plugins");
        const pluginDir = resolve(pluginsRoot, action.plugin);
        if (!pluginDir.startsWith(pluginsRoot + "/")) {
            throw new Error(`Invalid action plugin name: ${action.plugin}`);
        }
        if (!existsSync(pluginDir)) {
            throw new Error(`Action plugin directory not found: ${action.plugin}`);
        }
        const pluginMd = resolve(pluginDir, "PLUGIN.md");
        if (!existsSync(pluginMd)) {
            throw new Error(`Action plugin has no PLUGIN.md: ${action.plugin}`);
        }
    }
    // Validate cron expression if provided
    let nextRun;
    if (recurrence) {
        try {
            const interval = parseExpression(recurrence, {
                currentDate: new Date(startDate),
                tz: timezone,
            });
            nextRun = interval.next().toISOString();
        }
        catch (err) {
            throw new Error(`Invalid recurrence expression: "${recurrence}" — ${err instanceof Error ? err.message : String(err)}`);
        }
    }
    const eventId = randomUUID();
    const now = new Date().toISOString();
    const props = {
        eventId,
        accountId,
        name,
        startDate,
        eventStatus: "scheduled",
        createdAt: now,
        updatedAt: now,
    };
    if (description)
        props.description = description;
    if (endDate)
        props.endDate = endDate;
    if (location)
        props.location = location;
    if (recurrence)
        props.recurrence = recurrence;
    if (timezone)
        props.timezone = timezone;
    if (nextRun)
        props.nextRun = nextRun;
    if (action) {
        props.actionPlugin = action.plugin;
        props.actionTool = action.tool;
        if (action.args && Object.keys(action.args).length > 0) {
            props.actionArgs = JSON.stringify(action.args);
        }
    }
    // Compute embedding for memory-search discoverability
    const parts = [`Event: ${name}`];
    if (description)
        parts.push(description);
    parts.push(`starts: ${startDate}`);
    if (recurrence)
        parts.push(`recurs: ${recurrence}`);
    if (location)
        parts.push(`at: ${location}`);
    if (action)
        parts.push(`action: ${action.plugin}/${action.tool}`);
    const embedding = await computeEmbedding(parts.join(" — "));
    if (embedding.length > 0)
        props.embedding = embedding;
    const neo4jSession = getSession();
    try {
        if (sessionKey) {
            await neo4jSession.run(`CREATE (e:Event $props)
         WITH e
         MERGE (c:Conversation {sessionKey: $sessionKey})
         MERGE (e)-[:PART_OF]->(c)`, { props, sessionKey });
        }
        else {
            await neo4jSession.run(`CREATE (e:Event $props)`, { props });
        }
    }
    finally {
        await neo4jSession.close();
    }
    return { eventId, nextRun };
}
//# sourceMappingURL=schedule-event.js.map