// cli/commands/status.ts — planu status <specId> [--set implementing] (SPEC-124)
import { parseArgs } from 'node:util';
import { handleUpdateStatus } from '../../tools/update-status.js';
import { handleUpdateStatusBatch } from '../../tools/update-status/batch.js';
import { handlePlanStatus } from '../../tools/status-handler.js';
import { formatToolResult } from '../formatter.js';
import { detectProjectId } from '../project-detector.js';
import { red, green } from '../colors.js';
export const statusCommand = {
    name: 'status',
    description: 'Show project status or update the status of a spec',
    usage: 'planu status [--project-path . --project-id ID] | planu status <specId> --set <status> [--project-id ID] | planu status batch --set <status> SPEC-001 SPEC-002',
    async run(args, flags) {
        const { values, positionals } = parseStatusArgs(args);
        if (positionals.length === 0 && !values.set) {
            await runProjectStatus(values, flags);
            return;
        }
        const isBatch = positionals[0] === 'batch';
        const specId = positionals[0];
        if (!specId) {
            writeUsageError('Spec ID is required.');
            process.exitCode = 1;
            return;
        }
        const status = values.set;
        if (!status) {
            writeUsageError('--set <status> is required.');
            process.exitCode = 1;
            return;
        }
        if (isBatch) {
            await runBatchStatus(positionals.slice(1), values, status, flags);
            return;
        }
        await runSingleStatus(specId, values, status, flags);
    },
};
function parseStatusArgs(args) {
    const parsed = parseArgs({
        args,
        options: {
            set: { type: 'string', short: 's' },
            'project-id': { type: 'string' },
            'project-path': { type: 'string' },
            notes: { type: 'string', short: 'n' },
            'model-id': { type: 'string' },
            'model-tier-used': { type: 'string' },
            'context-hash': { type: 'string' },
            'handoff-path': { type: 'string' },
            'handoff-artifact-id': { type: 'string' },
            'reviewed-by': { type: 'string' },
            'arbitrated-by': { type: 'string' },
            reason: { type: 'string' },
            force: { type: 'boolean' },
            'force-status': { type: 'boolean' },
            'force-status-reason': { type: 'string' },
        },
        strict: false,
        allowPositionals: true,
    });
    return { values: parsed.values, positionals: parsed.positionals };
}
async function runProjectStatus(values, flags) {
    const result = await handlePlanStatus({
        projectPath: values['project-path'] ?? process.cwd(),
        projectId: values['project-id'],
    });
    if (result.isError) {
        process.stderr.write(`${red(formatToolResult(result, flags))}\n`);
        process.exitCode = 1;
        return;
    }
    const output = formatToolResult(result, flags);
    if (output) {
        process.stdout.write(`${output}\n`);
    }
}
function writeUsageError(message) {
    process.stderr.write(`${red('Error:')} ${message}\nUsage: ${statusCommand.usage}\n`);
}
async function runBatchStatus(specIds, values, status, flags) {
    const result = await handleUpdateStatusBatch({
        specIds,
        projectId: values['project-id'] ?? detectProjectId(),
        status: status,
        reviewNotes: values.notes,
    });
    writeToolResult(result, flags, false);
}
async function runSingleStatus(specId, values, status, flags) {
    const result = await handleUpdateStatus({
        specId,
        projectId: values['project-id'] ?? detectProjectId(),
        projectPath: values['project-path'],
        status: status,
        reviewNotes: values.notes,
        modelId: values['model-id'],
        modelTierUsed: values['model-tier-used'],
        contextHash: values['context-hash'],
        handoffPath: values['handoff-path'],
        handoffArtifactId: values['handoff-artifact-id'],
        reviewedBy: values['reviewed-by'],
        arbitratedBy: values['arbitrated-by'],
        reason: values.reason,
        force: values.force === true ? true : undefined,
        forceStatus: values['force-status'] === true ? true : undefined,
        forceStatusReason: values['force-status-reason'],
    });
    writeToolResult(result, flags, true);
}
function writeToolResult(result, flags, includeSuccessPrefix) {
    if (result.isError) {
        process.stderr.write(`${red(formatToolResult(result, flags))}\n`);
        process.exitCode = 1;
        return;
    }
    const output = formatToolResult(result, flags);
    if (flags?.quiet) {
        if (output) {
            process.stdout.write(`${output}\n`);
        }
        return;
    }
    process.stdout.write(includeSuccessPrefix ? `${green('Status updated!')} ${output}\n` : `${output}\n`);
}
//# sourceMappingURL=status.js.map