import { readdir, readFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { getSession } from "../lib/neo4j.js";
import { ATTACHMENTS_ROOT, } from "../lib/attachments.js";
export async function memoryListAttachments(accountId) {
    const t0 = Date.now();
    const log = (stage, detail) => console.error(`[memory-list-attachments] [${accountId.slice(0, 8)}] ${stage}${detail ? ` — ${detail}` : ""} (${Date.now() - t0}ms)`);
    log("start");
    const accountDir = resolve(ATTACHMENTS_ROOT, accountId);
    if (!existsSync(accountDir)) {
        log("empty", "no attachments directory");
        return { entries: [], totalFiles: 0, ingestedCount: 0, skippedCount: 0 };
    }
    // Read all attachment subdirectories
    const subdirs = await readdir(accountDir);
    const entries = [];
    let skippedCount = 0;
    for (const subdir of subdirs) {
        const metaPath = resolve(accountDir, subdir, `${subdir}.meta.json`);
        if (!existsSync(metaPath)) {
            skippedCount++;
            continue;
        }
        try {
            const raw = await readFile(metaPath, "utf-8");
            const meta = JSON.parse(raw);
            entries.push({
                attachmentId: meta.attachmentId,
                filename: meta.filename,
                mimeType: meta.mimeType,
                sizeBytes: meta.sizeBytes,
                storedAt: meta.storedAt,
                ingested: false,
                softDeleted: false,
            });
        }
        catch {
            log("skip", `corrupt meta.json for ${subdir}`);
            skippedCount++;
        }
    }
    if (entries.length === 0) {
        log("empty", "no valid meta.json files found");
        return { entries, totalFiles: 0, ingestedCount: 0, skippedCount };
    }
    // Cross-reference with Neo4j for ingestion status
    const session = getSession();
    try {
        const attachmentIds = entries.map((e) => e.attachmentId);
        const result = await session.run(`MATCH (k:KnowledgeDocument { accountId: $accountId })
       WHERE k.attachmentId IN $attachmentIds
       RETURN k.attachmentId AS aid, k.deletedAt AS deletedAt`, { accountId, attachmentIds });
        const ingestionMap = new Map();
        for (const record of result.records) {
            const aid = record.get("aid");
            const deletedAt = record.get("deletedAt");
            ingestionMap.set(aid, {
                ingested: true,
                softDeleted: deletedAt != null,
            });
        }
        let ingestedCount = 0;
        for (const entry of entries) {
            const status = ingestionMap.get(entry.attachmentId);
            if (status) {
                entry.ingested = status.ingested;
                entry.softDeleted = status.softDeleted;
                ingestedCount++;
            }
        }
        // Sort by storedAt descending (newest first)
        entries.sort((a, b) => new Date(b.storedAt).getTime() - new Date(a.storedAt).getTime());
        log("done", `${entries.length} files, ${ingestedCount} ingested, ${skippedCount} skipped`);
        return {
            entries,
            totalFiles: entries.length,
            ingestedCount,
            skippedCount,
        };
    }
    finally {
        await session.close();
    }
}
//# sourceMappingURL=memory-list-attachments.js.map