import { appendPolicyTrace, parsePolicyApprovalFromEnv, resolveActorWithFallback } from "../core/policy.js";
import { generateProfileDrivenArtifacts, validateProfile } from "./profile-support.js";
const EXIT_VALIDATION_FAILURE = 1;
async function requireCliPolicyApproval(cwd, operationId, commandLabel, writeError) {
    const approval = parsePolicyApprovalFromEnv(process.env);
    if (!approval) {
        writeError(`workspace-kit ${commandLabel} (${operationId}) requires WORKSPACE_KIT_POLICY_APPROVAL with JSON {"confirmed":true,"rationale":"..."} (env lane). Sensitive workspace-kit run commands use JSON policyApproval instead — see docs/maintainers/POLICY-APPROVAL.md.`);
        await appendPolicyTrace(cwd, {
            timestamp: new Date().toISOString(),
            operationId,
            command: commandLabel,
            actor: await resolveActorWithFallback(cwd, {}, process.env),
            allowed: false,
            message: "missing WORKSPACE_KIT_POLICY_APPROVAL"
        });
        return null;
    }
    return { rationale: approval.rationale };
}
async function recordCliPolicySuccess(cwd, operationId, commandLabel, rationale, commandOk) {
    await appendPolicyTrace(cwd, {
        timestamp: new Date().toISOString(),
        operationId,
        command: commandLabel,
        actor: await resolveActorWithFallback(cwd, {}, process.env),
        allowed: true,
        rationale,
        commandOk
    });
}
/**
 * Legacy `workspace-kit init` behavior: validate profile and regenerate profile-driven artifacts only.
 */
export async function runRefreshContextCommand(cwd, writeLine, writeError) {
    const approval = await requireCliPolicyApproval(cwd, "cli.init", "refresh-context", writeError);
    if (!approval) {
        return EXIT_VALIDATION_FAILURE;
    }
    const { errors, profile } = await validateProfile(cwd);
    if (errors.length > 0 || !profile) {
        await recordCliPolicySuccess(cwd, "cli.init", "refresh-context", approval.rationale, false);
        writeError("workspace-kit refresh-context failed profile validation.");
        for (const error of errors) {
            writeError(`- ${error}`);
        }
        return EXIT_VALIDATION_FAILURE;
    }
    const artifacts = await generateProfileDrivenArtifacts(cwd, profile);
    writeLine("workspace-kit refresh-context regenerated profile-driven project context artifacts.");
    writeLine(`- ${artifacts.generatedJsonPath}`);
    writeLine(`- ${artifacts.generatedRulePath}`);
    await recordCliPolicySuccess(cwd, "cli.init", "refresh-context", approval.rationale, true);
    return 0;
}
