import { Flags } from '@oclif/core';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { styles } from '../../lib/styles.js';
import { shouldOutputJson, outputSuccessAsJson, createMetadata, } from '../../lib/prompt-json.js';
export default class SpecList extends PMOCommand {
    static description = 'List all specs';
    static examples = [
        '<%= config.bin %> <%= command.id %>',
        '<%= config.bin %> <%= command.id %> --status active',
        '<%= config.bin %> <%= command.id %> --type product',
    ];
    static flags = {
        ...pmoBaseFlags,
        status: Flags.string({
            char: 's',
            description: 'Filter by status',
            options: ['draft', 'active', 'implemented'],
        }),
        type: Flags.string({
            char: 't',
            description: 'Filter by type',
            options: ['product', 'platform', 'infra', 'integration'],
        }),
        search: Flags.string({
            description: 'Search in title/problem/solution',
        }),
    };
    async execute() {
        const { flags } = await this.parse(SpecList);
        // Check if JSON output mode is active
        const jsonMode = shouldOutputJson(flags);
        // List specs with filters
        const specs = await this.storage.listSpecs({
            status: flags.status,
            type: flags.type,
            search: flags.search,
        });
        // In JSON mode, output specs as structured data
        if (jsonMode) {
            outputSuccessAsJson({
                specs: specs.map(s => ({
                    id: s.id,
                    title: s.title,
                    status: s.status,
                    type: s.type,
                    tags: s.tags,
                    problem: s.problem,
                    createdAt: s.createdAt?.toISOString(),
                    updatedAt: s.updatedAt?.toISOString(),
                })),
                count: specs.length,
                filters: {
                    status: flags.status,
                    type: flags.type,
                    search: flags.search,
                },
            }, createMetadata('spec list', flags));
            return;
        }
        if (specs.length === 0) {
            this.log(styles.warning('\nNo specs found'));
            this.log(styles.muted('  Create your first spec: prlt spec create'));
            return;
        }
        // Display specs grouped by status
        this.log(styles.title('\n📄 Specs'));
        this.log(styles.muted('═'.repeat(60)));
        // Group by status
        const byStatus = {};
        for (const spec of specs) {
            if (!byStatus[spec.status])
                byStatus[spec.status] = [];
            byStatus[spec.status].push(spec);
        }
        for (const status of ['draft', 'active', 'implemented']) {
            const statusSpecs = byStatus[status];
            if (!statusSpecs || statusSpecs.length === 0)
                continue;
            const statusEmoji = status === 'draft' ? '📝' : status === 'active' ? '🟢' : '✅';
            this.log(styles.emphasis(`\n${statusEmoji} ${status.toUpperCase()} (${statusSpecs.length})`));
            for (const spec of statusSpecs) {
                const typeTag = spec.type ? ` (${spec.type})` : '';
                const tags = spec.tags && spec.tags.length > 0 ? ` [${spec.tags.join(', ')}]` : '';
                this.log(`  ${styles.code(spec.id)}: ${spec.title}${typeTag}${tags}`);
                if (spec.problem) {
                    const preview = spec.problem.length > 60
                        ? spec.problem.substring(0, 60) + '...'
                        : spec.problem;
                    this.log(styles.muted(`     ${preview}`));
                }
            }
        }
        this.log(styles.muted('\n' + '═'.repeat(60)));
        this.log(styles.emphasis(`Total: ${specs.length} spec${specs.length === 1 ? '' : 's'}`));
        this.log(styles.muted('\nCommands:'));
        this.log(styles.primary('  prlt spec create      ') + styles.muted('Create a new spec'));
        this.log(styles.primary('  prlt spec view <id>   ') + styles.muted('View spec details'));
        this.log(styles.primary('  prlt spec plan <id>   ') + styles.muted('Generate tickets from spec'));
    }
}
//# sourceMappingURL=list.js.map