import { Args } from '@oclif/core';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { styles } from '../../lib/styles.js';
import { moveEpicFile, getRelativeEpicPath } from '../../lib/pmo/epic-files.js';
import { shouldOutputJson, outputErrorAsJson, createMetadata, } from '../../lib/prompt-json.js';
export default class EpicActivate extends PMOCommand {
    static description = 'Activate a draft or archived epic';
    static examples = [
        '<%= config.bin %> <%= command.id %> EPIC-004',
        '<%= config.bin %> <%= command.id %>',
    ];
    static args = {
        id: Args.string({
            description: 'Epic ID',
            required: false,
        }),
    };
    static flags = {
        ...pmoBaseFlags,
    };
    async execute() {
        const { args, flags } = await this.parse(EpicActivate);
        const projectId = await this.requireProject();
        // Check if JSON output mode is active
        const jsonMode = shouldOutputJson(flags);
        // Helper to handle errors in JSON mode
        const handleError = (code, message) => {
            if (jsonMode) {
                outputErrorAsJson(code, message, createMetadata('epic activate', flags));
                this.exit(1);
            }
            this.error(message);
        };
        let epicId = args.id;
        // If no ID provided, prompt for selection (show only non-active epics)
        if (!epicId) {
            const epics = await this.storage.listEpics(projectId);
            const activatable = epics.filter(e => e.status !== 'active');
            if (activatable.length === 0) {
                if (jsonMode) {
                    outputErrorAsJson('NO_EPICS_TO_ACTIVATE', 'No epics available to activate.', createMetadata('epic activate', flags));
                    return;
                }
                this.log(styles.muted('\nNo epics available to activate.'));
                return;
            }
            const selected = await this.selectFromList({
                message: 'Select epic to activate:',
                items: activatable,
                getName: (e) => `${e.id} ${e.title} (${e.status})`,
                getValue: (e) => e.id,
                getCommand: (e) => `prlt epic activate ${e.id} --json`,
                jsonMode: jsonMode ? { flags, commandName: 'epic activate' } : null,
            });
            if (!selected) {
                return;
            }
            epicId = selected;
        }
        const epic = await this.storage.getEpic(epicId);
        if (!epic) {
            return handleError('EPIC_NOT_FOUND', `Epic not found: ${epicId}`);
        }
        if (epic.status === 'active') {
            this.log(styles.muted(`Epic ${epicId} is already active.`));
            return;
        }
        // Warn if reactivating a complete epic
        if (epic.status === 'complete') {
            const tickets = await this.storage.getTicketsForEpic(projectId, epicId);
            const doneTickets = tickets.filter((t) => t.status === 'done').length;
            if (!jsonMode) {
                this.log(styles.warning(`\n⚠️  This epic was previously completed (${doneTickets}/${tickets.length} tickets done)`));
            }
            const jsonModeConfig = jsonMode ? { flags, commandName: 'epic activate' } : null;
            const { confirm } = await this.prompt([{
                    type: 'list',
                    name: 'confirm',
                    message: `This epic was previously completed (${doneTickets}/${tickets.length} tickets done). Reactivate this epic?`,
                    choices: [
                        { name: 'No', value: false, command: '' },
                        { name: 'Yes', value: true, command: `prlt epic activate ${epicId} --json` },
                    ],
                }], jsonModeConfig);
            if (!confirm) {
                this.log(styles.muted('Cancelled.'));
                return;
            }
        }
        this.log(`\nActivating: ${epicId} "${epic.title}"`);
        this.log(`Current status: ${epic.status}`);
        const previousStatus = epic.status;
        // Move the epic file to active status directory
        const moveResult = moveEpicFile(this.pmoPath, epicId, previousStatus, 'active', projectId);
        await this.storage.updateEpic(epicId, { status: 'active' });
        this.log(styles.success(`\n✅ Activated epic ${styles.emphasis(epicId)} "${epic.title}"`));
        this.log(styles.muted(`   Status: ${previousStatus} → active`));
        if (moveResult) {
            const relativePath = getRelativeEpicPath(this.pmoPath, epicId, 'active', projectId);
            this.log(styles.muted(`   File: ${relativePath}`));
        }
        this.log(styles.muted('\nNext steps:'));
        this.log(styles.muted(`  prlt epic view ${epicId}`));
        this.log(styles.muted(`  prlt ticket create --epic ${epicId} "First task"`));
    }
}
//# sourceMappingURL=activate.js.map