import { randomUUID } from "node:crypto";
import { writeFile, unlink } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { getSession } from "../lib/neo4j.js";
import { memoryIngestExtract } from "./memory-ingest-extract.js";
/**
 * Extract a human-readable title from markdown content.
 * Priority: first H1 heading → first non-empty line → URL fallback.
 */
function extractTitle(content, url) {
    const h1Match = content.match(/^#\s+(.+)$/m);
    if (h1Match) {
        const title = h1Match[1].trim();
        return title.length > 200 ? title.slice(0, 200) : title;
    }
    const firstLine = content.split("\n").find((line) => line.trim().length > 0);
    if (firstLine) {
        const cleaned = firstLine.replace(/^[#*_>\-\s]+/, "").trim();
        if (cleaned.length > 0) {
            return cleaned.length > 200 ? cleaned.slice(0, 200) : cleaned;
        }
    }
    return url;
}
/**
 * Check if a KnowledgeDocument already exists for this URL.
 * Returns the existing document info or undefined.
 * Gracefully skips if Neo4j is unreachable.
 */
async function checkExistingDocument(url, accountId) {
    const session = getSession();
    try {
        const result = await session.run(`MATCH (d:KnowledgeDocument { sourceUrl: $url, accountId: $accountId })
       RETURN elementId(d) AS nodeId, d.name AS name, d.summary AS summary, d.createdAt AS createdAt
       LIMIT 1`, { url, accountId });
        if (result.records.length === 0)
            return undefined;
        const r = result.records[0];
        return {
            nodeId: r.get("nodeId"),
            name: r.get("name"),
            summary: r.get("summary"),
            ingestedAt: r.get("createdAt"),
        };
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        console.error(`[memory-ingest-web] dedup check failed (skipping): ${msg}`);
        return undefined;
    }
    finally {
        await session.close();
    }
}
export async function memoryIngestWeb(params) {
    const { accountId, url, content, scope } = params;
    const t0 = Date.now();
    const log = (stage, detail) => console.error(`[memory-ingest-web] [${url.slice(0, 60)}] ${stage}${detail ? ` — ${detail}` : ""} (${Date.now() - t0}ms)`);
    log("start", `${content.length} chars, scope=${scope ?? "public"}`);
    // Validate content
    const trimmed = content.trim();
    if (trimmed.length === 0) {
        throw new Error("Web content is empty. The page may not have readable text, " +
            "or WebFetch may have failed to extract content from this URL.");
    }
    // Check for existing document with this URL
    log("dedup-check");
    const existingDocument = await checkExistingDocument(url, accountId);
    if (existingDocument) {
        log("dedup-found", `existing: ${existingDocument.nodeId.slice(0, 12)}, ingested ${existingDocument.ingestedAt}`);
    }
    // Extract a human-readable title
    const title = extractTitle(trimmed, url);
    log("title", title.slice(0, 80));
    // Write content to a temp file for memoryIngestExtract
    const attachmentId = randomUUID();
    const tempPath = join(tmpdir(), `maxy-web-${attachmentId}.md`);
    try {
        log("temp-write", tempPath);
        await writeFile(tempPath, trimmed, "utf-8");
        // Delegate to the existing extraction + chunking pipeline
        log("extract-start");
        const extract = await memoryIngestExtract({
            storagePath: tempPath,
            filename: title,
            mimeType: "text/markdown",
            attachmentId,
        });
        log("extract-done", `${extract.sections.length} sections`);
        return {
            attachmentId,
            title,
            extract,
            existingDocument,
        };
    }
    finally {
        // Clean up temp file regardless of success or failure
        try {
            await unlink(tempPath);
        }
        catch {
            // Best-effort cleanup — temp dir is periodically cleaned by OS
        }
    }
}
//# sourceMappingURL=memory-ingest-web.js.map