import { getSession } from "../lib/neo4j.js";
import { taskUpdate } from "./task-update.js";
export async function taskComplete(params) {
    const { taskId, accountId, note } = params;
    await taskUpdate({
        taskId,
        accountId,
        status: "completed",
        note: note ?? "Task completed.",
    });
    // Find tasks that were BLOCKS-ed by this one and are now potentially ready
    const session = getSession();
    try {
        const result = await session.run(`MATCH (completed:Task {taskId: $taskId, accountId: $accountId})-[:BLOCKS]->(candidate:Task {accountId: $accountId})
       WHERE candidate.status = 'pending'
       AND NOT EXISTS {
         MATCH (otherBlocker:Task {accountId: $accountId})-[:BLOCKS]->(candidate)
         WHERE otherBlocker.status IN ['pending', 'active']
         AND otherBlocker.taskId <> $taskId
       }
       RETURN candidate.taskId AS taskId, candidate.name AS name, candidate.priority AS priority`, { taskId, accountId });
        const unblockedTasks = result.records.map((rec) => ({
            taskId: rec.get("taskId"),
            name: rec.get("name"),
            priority: rec.get("priority"),
        }));
        return { unblockedTasks };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=task-complete.js.map