import { specStore, knowledgeStore } from '../storage/index.js';
import { handleCreateBranch } from './git/branch-ops.js';
import { handleGeneratePr } from './git/pr-ops.js';
import { handleValidate } from './validate.js';
import { checkConstitutionCompliance } from './create-spec-tech.js';
export async function runImplementingActions(projectId, specId) {
    let autoBranch;
    let protectedBranchWarning = null;
    // Check if on a protected branch
    try {
        const { resolveProjectPath, git: gitCmd } = await import('./git/git-helpers.js');
        const projectPath = await resolveProjectPath(projectId);
        const { stdout } = await gitCmd(projectPath, ['rev-parse', '--abbrev-ref', 'HEAD']);
        const currentBranch = stdout.trim();
        const protectedBranches = ['main', 'master', 'develop', 'production'];
        if (protectedBranches.includes(currentBranch)) {
            protectedBranchWarning = `Currently on protected branch '${currentBranch}'. Consider switching to a feature branch.`;
        }
    }
    catch {
        // Not a git repo — skip check
    }
    // Auto-create branch
    const foundSpec = await specStore.getSpec(projectId, specId);
    if (foundSpec && !foundSpec.gitBranch) {
        const branchResult = await handleCreateBranch(projectId, specId);
        if (!branchResult.isError) {
            try {
                const branchData = JSON.parse(branchResult.content[0]?.text ?? '{}');
                autoBranch = branchData.branchName;
            }
            catch {
                // ignore parse error
            }
        }
    }
    return {
        autoBranch,
        protectedBranchWarning,
        suggestions: [
            'Run generate_execution_plan for a step-by-step implementation guide',
            'Run generate_tests to scaffold test files',
        ],
    };
}
export async function runDoneActions(projectId, specId, gitBranch) {
    let mergeWarning = null;
    let prSuggestion = null;
    let validationWarning = null;
    if (gitBranch) {
        // Check if branch has been merged
        try {
            const { resolveProjectPath, git: gitCmd } = await import('./git/git-helpers.js');
            const projectPath = await resolveProjectPath(projectId);
            const { stdout } = await gitCmd(projectPath, ['branch', '--merged', 'develop']);
            const mergedBranches = stdout.split('\n').map((b) => b.trim().replace('* ', ''));
            if (!mergedBranches.includes(gitBranch)) {
                mergeWarning = `Branch '${gitBranch}' has not been merged to develop yet. Consider merging before marking as done.`;
            }
        }
        catch {
            // Not a git repo or branch check failed — skip
        }
        // Auto-generate PR description (best-effort)
        try {
            const prResult = await handleGeneratePr(projectId, specId);
            if (!prResult.isError) {
                prSuggestion = JSON.parse(prResult.content[0]?.text ?? '{}');
            }
        }
        catch {
            // PR generation is best-effort
        }
    }
    // Auto-validate coverage (non-blocking)
    try {
        const valResult = await handleValidate({ specId, projectId });
        if (!valResult.isError && valResult.content[1]?.text) {
            const parsed = JSON.parse(valResult.content[1].text);
            const score = parsed.score ?? 100;
            if (score < 80) {
                validationWarning = `Coverage score is ${String(score)}%. Consider improving before marking done.`;
            }
        }
    }
    catch {
        // Validation is best-effort — continue with transition
    }
    return { mergeWarning, prSuggestion, validationWarning };
}
/**
 * Best-effort constitution compliance check for status transitions.
 * Returns warnings (never blocks the transition).
 */
export async function checkConstitutionOnTransition(projectId, title, tags) {
    try {
        const constitution = await knowledgeStore.getConstitution(projectId);
        if (!constitution || constitution.principles.length === 0) {
            return [];
        }
        return checkConstitutionCompliance(title, tags, constitution.principles);
    }
    catch {
        // Best-effort: if constitution can't be loaded, skip silently
        return [];
    }
}
//# sourceMappingURL=update-status-actions.js.map