import { existsSync } from "node:fs";
import { readdir } from "node:fs/promises";
import { resolve } from "node:path";
import { listViewModels, loadViewModel } from "./view-models.js";
import { loadRuntimeConfig } from "./runtime-config.js";
export async function runGenerateAllDocuments(args, ctx, generateOne) {
    const config = await loadRuntimeConfig(ctx.workspacePath);
    const workspaceViewsRoot = resolve(ctx.workspacePath, "src/modules/documentation/views");
    const useWorkspaceViews = existsSync(workspaceViewsRoot);
    const workItems = [];
    if (useWorkspaceViews) {
        const viewFiles = await listViewModels(ctx.workspacePath);
        for (const viewFile of viewFiles) {
            const view = await loadViewModel(ctx.workspacePath, viewFile);
            workItems.push({ documentType: view.target });
        }
    }
    else {
        const templatesDir = resolve(config.sourceRoot, config.templatesRoot);
        const listTemplateFiles = async (dir, baseDir) => {
            const entries = await readdir(dir, { withFileTypes: true });
            const files = [];
            for (const entry of entries) {
                const absPath = resolve(dir, entry.name);
                if (entry.isDirectory())
                    files.push(...(await listTemplateFiles(absPath, baseDir)));
                if (entry.isFile() && entry.name.endsWith(".md"))
                    files.push(absPath.slice(baseDir.length + 1).split("\\").join("/"));
            }
            return files;
        };
        for (const templateFile of (await listTemplateFiles(templatesDir, templatesDir)).sort()) {
            workItems.push({ documentType: templateFile });
        }
    }
    const results = [];
    let succeeded = 0;
    let failed = 0;
    let skipped = 0;
    const batchOptions = {
        ...args.options,
        overwriteAi: args.options?.overwriteAi ?? false,
        overwriteHuman: args.options?.overwriteHuman ?? true,
        strict: args.options?.strict ?? false
    };
    for (const item of workItems) {
        const result = await generateOne({ documentType: item.documentType, options: batchOptions }, ctx);
        results.push(result);
        if (!result.ok)
            failed += 1;
        else if (result.evidence.filesWritten.length > 0)
            succeeded += 1;
        else
            skipped += 1;
    }
    return {
        ok: failed === 0,
        results,
        summary: {
            total: workItems.length,
            succeeded,
            failed,
            skipped,
            timestamp: new Date().toISOString()
        }
    };
}
