import { randomUUID } from "node:crypto";
import { getSession } from "../lib/neo4j.js";
import { computeEmbedding } from "../lib/embeddings.js";
export async function taskCreate(params) {
    const { accountId, name, description, status = "pending", priority = "normal", dueDate, sessionKey, raisedBy, affects, } = params;
    const taskId = randomUUID();
    const now = new Date().toISOString();
    const props = {
        taskId,
        accountId,
        name,
        description,
        status,
        priority,
        createdAt: now,
        updatedAt: now,
    };
    if (dueDate)
        props.dueDate = dueDate;
    const embeddingText = `Task: ${name} — ${description} status: ${status} priority: ${priority}`;
    const embedding = await computeEmbedding(embeddingText);
    if (embedding.length > 0)
        props.embedding = embedding;
    const neo4jSession = getSession();
    try {
        if (sessionKey) {
            await neo4jSession.run(`CREATE (t:Task $props)
         WITH t
         MERGE (c:Conversation {sessionKey: $sessionKey})
         MERGE (t)-[:PART_OF]->(c)`, { props, sessionKey });
        }
        else {
            await neo4jSession.run(`CREATE (t:Task $props)`, { props });
        }
        // Create RAISED_BY relationship if a person was specified
        if (raisedBy) {
            await neo4jSession.run(`MATCH (t:Task {taskId: $taskId, accountId: $accountId})
         MATCH (p) WHERE elementId(p) = $raisedBy
         MERGE (t)-[:RAISED_BY]->(p)`, { taskId, accountId, raisedBy });
        }
        // Create AFFECTS relationships if entities were specified
        if (affects && affects.length > 0) {
            for (const entityId of affects) {
                await neo4jSession.run(`MATCH (t:Task {taskId: $taskId, accountId: $accountId})
           MATCH (e) WHERE elementId(e) = $entityId
           MERGE (t)-[:AFFECTS]->(e)`, { taskId, accountId, entityId });
            }
        }
    }
    finally {
        await neo4jSession.close();
    }
    return { taskId };
}
//# sourceMappingURL=task-create.js.map