import { Flags, Args } from '@oclif/core';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { styles } from '../../lib/styles.js';
import { slugify } from '../../lib/pmo/utils.js';
import { shouldOutputJson } from '../../lib/prompt-json.js';
export default class RoadmapCreate extends PMOCommand {
    static description = 'Create a new roadmap';
    static examples = [
        '<%= config.bin %> <%= command.id %> "Public Roadmap"',
        '<%= config.bin %> <%= command.id %> --name "Internal Roadmap" --description "Full project list"',
        '<%= config.bin %> <%= command.id %> -i  # Interactive mode',
    ];
    static args = {
        name: Args.string({
            description: 'Roadmap name',
            required: false,
        }),
    };
    static flags = {
        ...pmoBaseFlags,
        name: Flags.string({
            char: 'n',
            description: 'Roadmap name',
        }),
        id: Flags.string({
            description: 'Custom roadmap ID (auto-generated from name if not provided)',
        }),
        description: Flags.string({
            char: 'd',
            description: 'Roadmap description',
        }),
        default: Flags.boolean({
            description: 'Set as the default roadmap',
            default: false,
        }),
        interactive: Flags.boolean({
            char: 'i',
            description: 'Interactive mode',
            default: false,
        }),
    };
    getPMOOptions() {
        return { promptIfMultiple: false };
    }
    async execute() {
        const { args, flags } = await this.parse(RoadmapCreate);
        const jsonMode = shouldOutputJson(flags);
        let roadmapData;
        if (flags.interactive || (!args.name && !flags.name)) {
            const jsonModeConfig = jsonMode ? { flags, commandName: 'roadmap create' } : null;
            // Prompt for name - in JSON mode, outputs prompt and exits; agent re-runs with --name flag
            const { name } = await this.prompt([{
                    type: 'input',
                    name: 'name',
                    message: 'Roadmap name:',
                    default: flags.name,
                    validate: (input) => (typeof input === 'string' && input.length > 0) || 'Name is required',
                }], jsonModeConfig);
            // Prompt for id
            const { id } = await this.prompt([{
                    type: 'input',
                    name: 'id',
                    message: 'Roadmap ID (leave blank to auto-generate):',
                    default: `roadmap-${slugify(name)}`,
                }], jsonModeConfig);
            // Prompt for description
            const { description } = await this.prompt([{
                    type: 'input',
                    name: 'description',
                    message: 'Description (optional):',
                    default: flags.description,
                }], jsonModeConfig);
            // Prompt for isDefault
            const { isDefault } = await this.prompt([{
                    type: 'list',
                    name: 'isDefault',
                    message: 'Set as default roadmap?',
                    choices: [
                        { name: 'No', value: 'false', command: `prlt roadmap create --name "${name}" --json` },
                        { name: 'Yes', value: 'true', command: `prlt roadmap create --name "${name}" --default --json` },
                    ],
                    default: flags.default ? 'true' : 'false',
                }], jsonModeConfig);
            roadmapData = {
                name,
                id: id || undefined,
                description: description || undefined,
                isDefault: isDefault === 'true',
            };
        }
        else {
            roadmapData = {
                name: args.name || flags.name,
                id: flags.id,
                description: flags.description,
                isDefault: flags.default,
            };
        }
        const roadmapId = roadmapData.id || `roadmap-${slugify(roadmapData.name)}`;
        // Check if roadmap already exists
        const existing = await this.storage.getRoadmap(roadmapId);
        if (existing) {
            this.error(`Roadmap "${roadmapId}" already exists.`);
        }
        // Create roadmap in database
        const roadmap = await this.storage.createRoadmap({
            id: roadmapId,
            name: roadmapData.name,
            description: roadmapData.description,
            isDefault: roadmapData.isDefault,
        });
        // In JSON mode, output roadmap data and return (skip interactive prompts)
        if (jsonMode) {
            this.log(JSON.stringify(roadmap, null, 2));
            return;
        }
        this.log(styles.success(`\nCreated roadmap "${styles.emphasis(roadmap.name)}"`));
        this.log(styles.muted(`  ID: ${roadmap.id}`));
        if (roadmap.description) {
            this.log(styles.muted(`  Description: ${roadmap.description}`));
        }
        if (roadmap.isDefault) {
            this.log(styles.muted(`  Default: Yes`));
        }
        // Prompt to add projects
        const projects = await this.storage.listProjects({ isArchived: false });
        if (projects.length > 0) {
            this.log('');
            const { addProjects } = await this.prompt([{
                    type: 'list',
                    name: 'addProjects',
                    message: 'Add projects to this roadmap now?',
                    choices: [
                        { name: 'Yes', value: true, command: `prlt roadmap add-project "${roadmap.id}" --json` },
                        { name: 'No, I\'ll add them later', value: false, command: '' },
                    ],
                }], null);
            if (addProjects) {
                const { selectedProjects } = await this.prompt([{
                        type: 'checkbox',
                        name: 'selectedProjects',
                        message: 'Select projects to include:',
                        choices: projects.map(p => ({
                            name: p.name,
                            value: p.id,
                            command: `prlt roadmap add-project "${roadmap.id}" "${p.id}" --json`,
                        })),
                    }], null);
                for (const projectId of selectedProjects) {
                    // eslint-disable-next-line no-await-in-loop -- Sequential updates
                    await this.storage.addProjectToRoadmap(roadmap.id, projectId);
                }
                if (selectedProjects.length > 0) {
                    this.log(styles.success(`Added ${selectedProjects.length} project(s) to roadmap`));
                }
            }
        }
        this.log(styles.muted(`\nNext steps:`));
        this.log(styles.muted(`  prlt roadmap view ${roadmap.id}`));
        this.log(styles.muted(`  prlt roadmap add-project ${roadmap.id}`));
        this.log(styles.muted(`  prlt roadmap generate ${roadmap.id}`));
    }
}
//# sourceMappingURL=create.js.map