import { builtinInstructionEntriesForModule } from "../../contracts/builtin-run-command-manifest.js";
import { generateDocument, generateAllDocuments } from "./runtime.js";
function parseOptions(raw) {
    return {
        dryRun: typeof raw.dryRun === "boolean" ? raw.dryRun : undefined,
        overwrite: typeof raw.overwrite === "boolean" ? raw.overwrite : undefined,
        overwriteAi: typeof raw.overwriteAi === "boolean" ? raw.overwriteAi : undefined,
        overwriteHuman: typeof raw.overwriteHuman === "boolean" ? raw.overwriteHuman : undefined,
        overwriteRepoRootReadme: typeof raw.overwriteRepoRootReadme === "boolean" ? raw.overwriteRepoRootReadme : undefined,
        strict: typeof raw.strict === "boolean" ? raw.strict : undefined,
        maxValidationAttempts: typeof raw.maxValidationAttempts === "number" ? raw.maxValidationAttempts : undefined,
        allowWithoutTemplate: typeof raw.allowWithoutTemplate === "boolean" ? raw.allowWithoutTemplate : undefined,
    };
}
export const documentationModule = {
    registration: {
        id: "documentation",
        version: "0.5.0",
        contractVersion: "1",
        stateSchema: 1,
        capabilities: ["documentation"],
        dependsOn: [],
        optionalPeers: [],
        enabledByDefault: true,
        config: {
            path: "src/modules/documentation/config.md",
            format: "md",
            description: "Documentation module configuration contract."
        },
        instructions: {
            directory: "src/modules/documentation/instructions",
            entries: builtinInstructionEntriesForModule("documentation")
        }
    },
    async onCommand(command, ctx) {
        const args = command.args ?? {};
        const rawOptions = typeof args.options === "object" && args.options !== null
            ? args.options
            : {};
        const options = parseOptions(rawOptions);
        const handlers = {
            "document-project": async () => {
                const batchResult = await generateAllDocuments({ options }, ctx);
                return {
                    ok: batchResult.ok,
                    code: batchResult.ok ? "documented-project" : "documentation-batch-failed",
                    message: batchResult.ok
                        ? `Generated ${batchResult.summary.succeeded} documents (${batchResult.summary.skipped} skipped)`
                        : `Batch failed: ${batchResult.summary.failed} of ${batchResult.summary.total} documents failed`,
                    data: {
                        summary: batchResult.summary,
                        results: batchResult.results.map((r) => ({
                            documentType: r.evidence.documentType,
                            ok: r.ok,
                            aiOutputPath: r.aiOutputPath,
                            humanOutputPath: r.humanOutputPath,
                            repoRootReadmePath: r.repoRootReadmePath,
                            filesWritten: r.evidence.filesWritten,
                            filesSkipped: r.evidence.filesSkipped
                        }))
                    }
                };
            },
            "generate-document": async () => {
                const result = await generateDocument({
                    documentType: typeof args.documentType === "string" ? args.documentType : undefined,
                    options
                }, ctx);
                return {
                    ok: result.ok,
                    code: result.ok ? "generated-document" : "generation-failed",
                    message: result.ok
                        ? `Generated document '${args.documentType ?? "unknown"}'`
                        : `Failed to generate document '${args.documentType ?? "unknown"}'`,
                    data: {
                        aiOutputPath: result.aiOutputPath,
                        humanOutputPath: result.humanOutputPath,
                        repoRootReadmePath: result.repoRootReadmePath,
                        evidence: result.evidence
                    }
                };
            }
        };
        const handler = handlers[command.name];
        if (handler)
            return handler();
        return {
            ok: false,
            code: "unsupported-command",
            message: `Documentation module does not support command '${command.name}'`
        };
    }
};
