import { loadLatestDispatchCollection, loadLatestDispatchManifest, collectDispatchOutputs, writeDispatchCollection } from "@shrey-junior/sj-core/core/dispatch.js";
import { loadActiveRoleHints, updateActiveRoleHints } from "@shrey-junior/sj-core/core/state.js";
import { PRODUCT_METADATA } from "@shrey-junior/sj-core/core/product.js";
import { renderDisplayPath } from "@shrey-junior/sj-core/utils/fs.js";
export async function runCollect(options) {
    const manifest = await loadLatestDispatchManifest(options.targetRoot);
    if (!manifest) {
        throw new Error("no dispatch manifest found in target repo");
    }
    const previousCollection = await loadLatestDispatchCollection(options.targetRoot, manifest.dispatchId);
    const collection = await collectDispatchOutputs(options.targetRoot, manifest);
    const paths = await writeDispatchCollection(options.targetRoot, manifest, collection);
    const activeRoleHints = await loadActiveRoleHints(options.targetRoot);
    if (activeRoleHints) {
        await updateActiveRoleHints(options.targetRoot, {
            activeRole: activeRoleHints.activeRole,
            authoritySource: activeRoleHints.authoritySource,
            projectType: activeRoleHints.projectType,
            supportingRoles: activeRoleHints.supportingRoles,
            nextFileToRead: ".agent/state/dispatch/latest-collect.json",
            nextSuggestedCommand: `${PRODUCT_METADATA.cli.primaryCommand} reconcile --target "${options.targetRoot}"`,
            nextAction: "Review the latest dispatch collection, then reconcile the role outputs before checkpointing or handoff."
        });
    }
    const lines = [
        `dispatch: ${manifest.dispatchId}`,
        `overall status: ${collection.overallStatus}`,
        `analysis basis: ${collection.parsingBasis}`,
        `completed roles: ${collection.completedRoles.join(", ") || "none"}`,
        `missing roles: ${collection.missingRoles.join(", ") || "none"}`,
        `heuristic fallback roles: ${collection.fallbackRoles.join(", ") || "none"}`,
        `malformed roles: ${collection.malformedRoles.join(", ") || "none"}`,
        `partial structured roles: ${collection.partialRoles.length > 0 ? collection.partialRoles.map((item) => `${item.role}(${item.missingFields.join(",")})`).join("; ") : "none"}`,
        ...collection.roleResults
            .filter((result) => result.parsingWarnings.length > 0)
            .map((result) => `- ${result.role} parse notes: ${result.parsingWarnings.join("; ")}`),
        `collection latest: ${renderDisplayPath(options.targetRoot, paths.latestPath)}`,
        `collection history: ${renderDisplayPath(options.targetRoot, paths.historyPath)}`,
        previousCollection ? "previous collection state replaced with latest snapshot" : "first collection snapshot recorded"
    ];
    options.logger.info(lines.join("\n"));
    return collection.overallStatus === "blocked" ? 1 : 0;
}
