import { getSession } from "../lib/neo4j.js";
/**
 * Returns tasks that are safe to start: status=pending, zero unresolved
 * BLOCKS, and no AFFECTS conflict with currently active tasks.
 *
 * This is the product equivalent of the development lane compatibility
 * matrix — it answers "what can I work on next without conflict?"
 *
 *   Pending task is READY when:
 *     1. No incomplete task BLOCKS it (sequential dependency)
 *     2. No active task AFFECTS the same entity (conflict detection)
 */
export async function taskReady(accountId) {
    const session = getSession();
    try {
        const result = await session.run(`MATCH (t:Task {accountId: $accountId, status: 'pending'})
       WHERE NOT EXISTS {
         MATCH (blocker:Task {accountId: $accountId})-[:BLOCKS]->(t)
         WHERE blocker.status IN ['pending', 'active']
       }
       AND NOT EXISTS {
         MATCH (t)-[:AFFECTS]->(entity)<-[:AFFECTS]-(active:Task {accountId: $accountId})
         WHERE active.status = 'active'
       }
       RETURN t
       ORDER BY
         CASE t.priority
           WHEN 'urgent' THEN 0
           WHEN 'high' THEN 1
           WHEN 'normal' THEN 2
           WHEN 'low' THEN 3
           ELSE 4
         END,
         t.createdAt ASC
       LIMIT 50`, { accountId });
        return result.records.map((rec) => {
            const t = rec.get("t").properties;
            const task = {
                taskId: t.taskId,
                name: t.name,
                description: t.description,
                priority: t.priority,
                createdAt: t.createdAt,
            };
            if (t.dueDate)
                task.dueDate = t.dueDate;
            return task;
        });
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=task-ready.js.map