import { int } from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
export async function taskList(params) {
    const { accountId, status, priority, relatedEntityId, limit = 50 } = params;
    const session = getSession();
    try {
        let query;
        const queryParams = { accountId, limit: int(limit) };
        if (relatedEntityId) {
            // Tasks related to a specific entity via any relationship
            query = `
        MATCH (t:Task {accountId: $accountId})-[]->(e)
        WHERE elementId(e) = $relatedEntityId
        ${status ? "AND t.status = $status" : ""}
        ${priority ? "AND t.priority = $priority" : ""}
        RETURN t
        ORDER BY t.createdAt DESC
        LIMIT $limit
      `;
            queryParams.relatedEntityId = relatedEntityId;
        }
        else {
            query = `
        MATCH (t:Task {accountId: $accountId})
        ${status || priority ? "WHERE" : ""}
        ${status ? "t.status = $status" : ""}
        ${status && priority ? "AND" : ""}
        ${priority ? "t.priority = $priority" : ""}
        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 DESC
        LIMIT $limit
      `;
        }
        if (status)
            queryParams.status = status;
        if (priority)
            queryParams.priority = priority;
        const result = await session.run(query, queryParams);
        return result.records.map((rec) => {
            const t = rec.get("t").properties;
            const summary = {
                taskId: t.taskId,
                name: t.name,
                status: t.status,
                priority: t.priority,
                createdAt: t.createdAt,
                updatedAt: t.updatedAt,
            };
            if (t.dueDate)
                summary.dueDate = t.dueDate;
            return summary;
        });
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=task-list.js.map