import neo4j from "neo4j-driver";
import { getSession } from "../lib/neo4j.js";
export async function waitlistList(params) {
    const { accountId, status, since, limit = 50 } = params;
    const session = getSession();
    try {
        const conditions = [
            "p.accountId = $accountId",
            "p.source = 'waitlist'",
        ];
        const queryParams = {
            accountId,
            limit: neo4j.int(limit),
        };
        if (status) {
            conditions.push("p.status = $status");
            queryParams.status = status;
        }
        if (since) {
            conditions.push("p.createdOn >= $since");
            queryParams.since = since;
        }
        const whereClause = conditions.join(" AND ");
        // Total count (all waitlist entries matching filters)
        const countResult = await session.run(`MATCH (p:Person) WHERE ${whereClause} RETURN count(p) AS total`, queryParams);
        const total = countResult.records[0].get("total").toNumber();
        // Count of entries needing review
        const reviewResult = await session.run(`MATCH (p:Person)
       WHERE p.accountId = $accountId AND p.source = 'waitlist' AND p.status = 'review'
       RETURN count(p) AS reviewCount`, { accountId });
        const reviewCount = reviewResult.records[0].get("reviewCount").toNumber();
        // Paginated list
        const listResult = await session.run(`MATCH (p:Person)
       WHERE ${whereClause}
       RETURN p, elementId(p) AS nodeId
       ORDER BY p.createdOn DESC
       LIMIT $limit`, queryParams);
        const entries = listResult.records.map((record) => {
            const p = record.get("p").properties;
            return {
                nodeId: record.get("nodeId"),
                givenName: p.givenName,
                familyName: p.familyName,
                email: p.email,
                status: p.status,
                createdOn: p.createdOn,
                extractedOn: p.extractedOn,
                sessionId: p.sessionId,
                discoveryNotes: p.discoveryNotes,
            };
        });
        return { entries, total, reviewCount };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=waitlist-list.js.map