import { getSession } from "../lib/neo4j.js";
import { computeEmbedding } from "../lib/embeddings.js";
export async function taskUpdate(params) {
    const { taskId, accountId, status, priority, dueDate, note } = params;
    const session = getSession();
    try {
        // Fetch current task to build updated embedding and append note
        const existing = await session.run(`MATCH (t:Task {taskId: $taskId, accountId: $accountId})
       RETURN t.name AS name, t.description AS description, t.notes AS notes,
              t.status AS status, t.priority AS priority`, { taskId, accountId });
        if (existing.records.length === 0) {
            throw new Error(`Task not found: ${taskId}`);
        }
        const rec = existing.records[0];
        const currentName = rec.get("name");
        const currentDescription = rec.get("description");
        const currentNotes = rec.get("notes") ?? "";
        const currentStatus = rec.get("status");
        const currentPriority = rec.get("priority");
        const newStatus = status ?? currentStatus;
        const newPriority = priority ?? currentPriority;
        // Append note with timestamp (append-only — never overwrite)
        let newNotes = currentNotes;
        if (note) {
            const timestamp = new Date().toISOString().slice(0, 16).replace("T", " ");
            newNotes = currentNotes
                ? `${currentNotes}\n[${timestamp}] ${note}`
                : `[${timestamp}] ${note}`;
        }
        const setClauses = [
            "t.status = $status",
            "t.priority = $priority",
            "t.notes = $notes",
            "t.updatedAt = $updatedAt",
        ];
        const queryParams = {
            taskId,
            accountId,
            status: newStatus,
            priority: newPriority,
            notes: newNotes,
            updatedAt: new Date().toISOString(),
        };
        if (dueDate !== undefined) {
            setClauses.push("t.dueDate = $dueDate");
            queryParams.dueDate = dueDate;
        }
        // Re-embed with updated status/priority/notes
        const embeddingText = `Task: ${currentName} — ${currentDescription} status: ${newStatus} priority: ${newPriority}${newNotes ? " " + newNotes : ""}`;
        const embedding = await computeEmbedding(embeddingText);
        if (embedding.length > 0) {
            setClauses.push("t.embedding = $embedding");
            queryParams.embedding = embedding;
        }
        await session.run(`MATCH (t:Task {taskId: $taskId, accountId: $accountId})
       SET ${setClauses.join(", ")}`, queryParams);
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=task-update.js.map