/**
 * Aggregate pipeline report.json files across all processed agents.
 *
 * Replaces the shell-based summary.sh — walks agent data directories,
 * collects report.json files, and produces a combined summary report.
 *
 * Usage:
 *   visionclaw summary-data <data-root>
 */
import fs from "node:fs";
import path from "node:path";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const DATE_PATTERN = /^\d{8}T\d{6}$/;
/** Find all report.json files under the data root. */
function findReports(baseDir) {
    const reports = [];
    for (const agentEntry of fs.readdirSync(baseDir, { withFileTypes: true })) {
        if (!agentEntry.isDirectory() || agentEntry.name.startsWith("."))
            continue;
        const agentDir = path.join(baseDir, agentEntry.name);
        for (const dateEntry of fs.readdirSync(agentDir, { withFileTypes: true })) {
            if (!dateEntry.isDirectory() || !DATE_PATTERN.test(dateEntry.name))
                continue;
            const reportPath = path.join(agentDir, dateEntry.name, "output", "report.json");
            if (fs.existsSync(reportPath)) {
                reports.push({
                    relativePath: `${agentEntry.name}/${dateEntry.name}`,
                    fullPath: reportPath,
                });
            }
        }
    }
    return reports.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
}
// ---------------------------------------------------------------------------
// CLI entry
// ---------------------------------------------------------------------------
export function runSummaryData(args) {
    const dataRoot = args.find((a) => !a.startsWith("-"));
    if (!dataRoot) {
        console.error("Usage: visionclaw summary-data <data-root>\n" +
            "\n" +
            "  <data-root>   Root directory containing agent data folders");
        process.exit(1);
    }
    if (!fs.existsSync(dataRoot) || !fs.statSync(dataRoot).isDirectory()) {
        console.error(`Not a directory: ${dataRoot}`);
        process.exit(1);
    }
    const reports = findReports(dataRoot);
    if (reports.length === 0) {
        console.log("No report.json files found. Run the pipeline first.");
        return;
    }
    // Aggregate
    const agents = {};
    const grandTotal = {
        total_records: 0,
        total_tool_calls: 0,
        total_records_no_pure_heartbeat: 0,
        total_tool_calls_no_pure_heartbeat: 0,
        files: {},
    };
    for (const { relativePath, fullPath } of reports) {
        const data = JSON.parse(fs.readFileSync(fullPath, "utf-8"));
        agents[relativePath] = {
            total_records: data.total_records,
            total_tool_calls: data.total_tool_calls,
            total_records_no_pure_heartbeat: data.total_records_no_pure_heartbeat,
            total_tool_calls_no_pure_heartbeat: data.total_tool_calls_no_pure_heartbeat,
            files: data.files,
        };
        grandTotal.total_records += data.total_records;
        grandTotal.total_tool_calls += data.total_tool_calls;
        grandTotal.total_records_no_pure_heartbeat += data.total_records_no_pure_heartbeat;
        grandTotal.total_tool_calls_no_pure_heartbeat += data.total_tool_calls_no_pure_heartbeat;
        for (const [fname, fdata] of Object.entries(data.files)) {
            const existing = grandTotal.files[fname];
            if (!existing) {
                grandTotal.files[fname] = { records: fdata.records, tool_calls: fdata.tool_calls };
            }
            else {
                existing.records += fdata.records;
                existing.tool_calls += fdata.tool_calls;
            }
        }
    }
    const summary = {
        generated_at: new Date().toISOString(),
        agent_count: Object.keys(agents).length,
        grand_total: grandTotal,
        agents,
    };
    // Write summary file
    const outPath = path.join(dataRoot, "summary-report.json");
    fs.writeFileSync(outPath, JSON.stringify(summary, null, 2) + "\n");
    // Print table
    console.log("");
    console.log("Agent Data Summary");
    console.log("=".repeat(90));
    console.log("Agent/Date".padEnd(40) +
        "Records".padStart(10) +
        "ToolCalls".padStart(10) +
        "Rec(noHB)".padStart(10) +
        "TC(noHB)".padStart(10));
    console.log("-".repeat(90));
    for (const [name, d] of Object.entries(agents).sort(([a], [b]) => a.localeCompare(b))) {
        console.log(name.padEnd(40) +
            String(d.total_records).padStart(10) +
            String(d.total_tool_calls).padStart(10) +
            String(d.total_records_no_pure_heartbeat).padStart(10) +
            String(d.total_tool_calls_no_pure_heartbeat).padStart(10));
    }
    console.log("-".repeat(90));
    console.log("TOTAL".padEnd(40) +
        String(grandTotal.total_records).padStart(10) +
        String(grandTotal.total_tool_calls).padStart(10) +
        String(grandTotal.total_records_no_pure_heartbeat).padStart(10) +
        String(grandTotal.total_tool_calls_no_pure_heartbeat).padStart(10));
    console.log("=".repeat(90));
    console.log("");
    console.log(`Written to: ${outPath}`);
}
//# sourceMappingURL=summary.js.map