import { getSession } from "../lib/neo4j.js";
import { computeEmbedding, buildPersonEmbeddingText } from "../lib/embedding.js";
export async function waitlistReview(params) {
    const { accountId, email, nodeId, name, status, givenName, familyName } = params;
    if (!email && !nodeId && !name) {
        throw new Error("Either email, nodeId, or name is required to identify the entry");
    }
    const session = getSession();
    try {
        // Find the Person node — by email, nodeId, or name (case-insensitive partial match)
        let matchClause;
        if (email) {
            matchClause = "MATCH (p:Person {email: $email, accountId: $accountId, source: 'waitlist'})";
        }
        else if (nodeId) {
            matchClause = "MATCH (p:Person {accountId: $accountId, source: 'waitlist'}) WHERE elementId(p) = $nodeId";
        }
        else {
            matchClause = `MATCH (p:Person {accountId: $accountId, source: 'waitlist'})
       WHERE toLower(p.givenName) CONTAINS toLower($name)
          OR toLower(COALESCE(p.familyName, '')) CONTAINS toLower($name)`;
        }
        const findResult = await session.run(`${matchClause}
       RETURN p, elementId(p) AS nodeId`, { email, nodeId, name, accountId });
        if (findResult.records.length === 0) {
            throw new Error(email
                ? `No waitlist entry found for email: ${email}`
                : `No waitlist entry found for node ID: ${nodeId}`);
        }
        const record = findResult.records[0];
        const person = record.get("p").properties;
        const resolvedNodeId = record.get("nodeId");
        const previousStatus = person.status;
        const resolvedStatus = status ?? previousStatus;
        // Build SET clause
        const setProps = ["p.status = $status", "p.updatedAt = $now"];
        const queryParams = {
            email: person.email,
            accountId,
            nodeId: resolvedNodeId,
            status: resolvedStatus,
            now: new Date().toISOString(),
        };
        if (givenName) {
            setProps.push("p.givenName = $givenName");
            queryParams.givenName = givenName;
        }
        if (familyName) {
            setProps.push("p.familyName = $familyName");
            queryParams.familyName = familyName;
        }
        // Update
        const updateClause = email
            ? "MATCH (p:Person {email: $email, accountId: $accountId, source: 'waitlist'})"
            : "MATCH (p:Person {accountId: $accountId, source: 'waitlist'}) WHERE elementId(p) = $nodeId";
        await session.run(`${updateClause} SET ${setProps.join(", ")}`, queryParams);
        // Re-embed with discovery context preserved
        const currentGivenName = (givenName ?? person.givenName);
        const currentFamilyName = (familyName ?? person.familyName);
        const textForEmbedding = buildPersonEmbeddingText(currentGivenName, currentFamilyName ?? null, person.email, resolvedStatus, person.discoveryNotes ?? null);
        const embedding = await computeEmbedding(textForEmbedding);
        if (embedding.length > 0) {
            const embedClause = email
                ? "MATCH (p:Person {email: $email, accountId: $accountId})"
                : "MATCH (p:Person {accountId: $accountId}) WHERE elementId(p) = $nodeId";
            await session.run(`${embedClause} SET p.embedding = $embedding`, { email: person.email, accountId, nodeId: resolvedNodeId, embedding });
        }
        return {
            nodeId: resolvedNodeId,
            email: person.email,
            previousStatus,
            newStatus: resolvedStatus,
            extractedOn: person.extractedOn,
            discoveryNotes: person.discoveryNotes,
        };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=waitlist-review.js.map