/**
 * Cross-reference management between BRAIN memory entries and tasks.
 *
 * Provides linking/unlinking and query functions for relationships
 * between decisions, patterns, learnings and CLEO tasks.
 *
 * @task T5156
 * @epic T5149
 */
import { getBrainAccessor } from '../store/brain-accessor.js';
import { taskExistsInTasksDb } from '../store/cross-db-cleanup.js';
import { getDb } from '../store/sqlite.js';
/**
 * Link a memory entry to a task.
 *
 * @task T5156
 */
export async function linkMemoryToTask(projectRoot, memoryType, memoryId, taskId, linkType) {
    if (!memoryId || !taskId) {
        throw new Error('memoryId and taskId are required');
    }
    // Write-guard: reject stale task IDs before creating cross-db reference
    const tasksDb = await getDb(projectRoot);
    if (!(await taskExistsInTasksDb(taskId, tasksDb))) {
        throw new Error(`Write-guard: task ${taskId} does not exist in tasks.db — refusing to create brain link`);
    }
    const accessor = await getBrainAccessor(projectRoot);
    // Check if link already exists
    const existingLinks = await accessor.getLinksForMemory(memoryType, memoryId);
    const duplicate = existingLinks.find((l) => l.taskId === taskId && l.linkType === linkType);
    if (duplicate) {
        return duplicate;
    }
    await accessor.addLink({
        memoryType,
        memoryId,
        taskId,
        linkType,
    });
    // Return the created link
    const links = await accessor.getLinksForMemory(memoryType, memoryId);
    return links.find((l) => l.taskId === taskId && l.linkType === linkType);
}
/**
 * Remove a link between a memory entry and a task.
 *
 * @task T5156
 */
export async function unlinkMemoryFromTask(projectRoot, memoryType, memoryId, taskId, linkType) {
    const accessor = await getBrainAccessor(projectRoot);
    await accessor.removeLink(memoryType, memoryId, taskId, linkType);
}
/**
 * Get all memory entries linked to a specific task.
 *
 * @task T5156
 */
export async function getTaskLinks(projectRoot, taskId) {
    const accessor = await getBrainAccessor(projectRoot);
    return accessor.getLinksForTask(taskId);
}
/**
 * Get all tasks linked to a specific memory entry.
 *
 * @task T5156
 */
export async function getMemoryLinks(projectRoot, memoryType, memoryId) {
    const accessor = await getBrainAccessor(projectRoot);
    return accessor.getLinksForMemory(memoryType, memoryId);
}
/**
 * Batch create multiple links at once.
 *
 * @task T5156
 */
export async function bulkLink(projectRoot, links) {
    const accessor = await getBrainAccessor(projectRoot);
    const tasksDb = await getDb(projectRoot);
    let created = 0;
    let skipped = 0;
    for (const link of links) {
        // Write-guard: reject stale task IDs before creating cross-db reference
        if (!(await taskExistsInTasksDb(link.taskId, tasksDb))) {
            skipped++;
            continue;
        }
        // Check for duplicate
        const existing = await accessor.getLinksForMemory(link.memoryType, link.memoryId);
        const duplicate = existing.find((l) => l.taskId === link.taskId && l.linkType === link.linkType);
        if (duplicate) {
            skipped++;
            continue;
        }
        await accessor.addLink({
            memoryType: link.memoryType,
            memoryId: link.memoryId,
            taskId: link.taskId,
            linkType: link.linkType,
        });
        created++;
    }
    return { created, skipped };
}
/**
 * Get all decisions linked to a task.
 * Convenience method that fetches full decision rows.
 *
 * @task T5156
 */
export async function getLinkedDecisions(projectRoot, taskId) {
    const accessor = await getBrainAccessor(projectRoot);
    const links = await accessor.getLinksForTask(taskId);
    const decisionLinks = links.filter((l) => l.memoryType === 'decision');
    const decisions = [];
    for (const link of decisionLinks) {
        const decision = await accessor.getDecision(link.memoryId);
        if (decision) {
            decisions.push(decision);
        }
    }
    return decisions;
}
/**
 * Get all patterns linked to a task.
 * Convenience method that fetches full pattern rows.
 *
 * @task T5156
 */
export async function getLinkedPatterns(projectRoot, taskId) {
    const accessor = await getBrainAccessor(projectRoot);
    const links = await accessor.getLinksForTask(taskId);
    const patternLinks = links.filter((l) => l.memoryType === 'pattern');
    const patterns = [];
    for (const link of patternLinks) {
        const pattern = await accessor.getPattern(link.memoryId);
        if (pattern) {
            patterns.push(pattern);
        }
    }
    return patterns;
}
/**
 * Get all learnings linked to a task.
 * Convenience method that fetches full learning rows.
 *
 * @task T5156
 */
export async function getLinkedLearnings(projectRoot, taskId) {
    const accessor = await getBrainAccessor(projectRoot);
    const links = await accessor.getLinksForTask(taskId);
    const learningLinks = links.filter((l) => l.memoryType === 'learning');
    const learnings = [];
    for (const link of learningLinks) {
        const learning = await accessor.getLearning(link.memoryId);
        if (learning) {
            learnings.push(learning);
        }
    }
    return learnings;
}
//# sourceMappingURL=brain-links.js.map