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 ProjectList extends PMOCommand {
    static description = 'List all projects in the PMO';
    static examples = [
        '<%= config.bin %> <%= command.id %>',
        '<%= config.bin %> <%= command.id %> --archived',
        '<%= config.bin %> <%= command.id %> --all',
    ];
    static flags = {
        ...pmoBaseFlags,
        archived: Flags.boolean({
            char: 'a',
            description: 'Show only archived projects',
            default: false,
        }),
        all: Flags.boolean({
            description: 'Show all projects (including archived)',
            default: false,
        }),
    };
    getPMOOptions() {
        return { promptIfMultiple: false };
    }
    async execute() {
        const { flags } = await this.parse(ProjectList);
        // Check if JSON output mode is active
        const jsonMode = shouldOutputJson(flags);
        // Determine filter based on flags
        let isArchived;
        if (flags.archived) {
            isArchived = true;
        }
        else if (!flags.all) {
            isArchived = false; // Default: show only non-archived
        }
        // If --all, isArchived is undefined (show both)
        const projects = await this.storage.listProjects({ isArchived });
        // Get phases for display
        const phases = await this.storage.listPhases();
        const phaseMap = new Map(phases.map(p => [p.id, p]));
        // Get ticket counts using listProjectSummaries
        const summaries = await this.storage.listProjectSummaries();
        const ticketCountMap = new Map(summaries.map(s => [s.id, s.ticketCount]));
        // JSON mode: output structured data
        if (jsonMode) {
            const projectData = projects.map(p => ({
                id: p.id,
                name: p.name,
                description: p.description,
                isArchived: p.isArchived,
                isDefault: p.id === 'default',
                ticketCount: ticketCountMap.get(p.id) ?? 0,
                phase: p.phaseId ? phaseMap.get(p.phaseId)?.name : undefined,
                phaseId: p.phaseId,
            }));
            outputSuccessAsJson({ projects: projectData }, createMetadata('project list', flags));
            return;
        }
        if (projects.length === 0) {
            if (flags.archived) {
                this.log(styles.muted('No archived projects found.'));
            }
            else if (flags.all) {
                this.log(styles.muted('No projects found. Create one with: prlt project create'));
            }
            else {
                this.log(styles.muted('No active projects found. Create one with: prlt project create'));
                // Check if there are archived projects
                const archivedProjects = await this.storage.listProjects({ isArchived: true });
                if (archivedProjects.length > 0) {
                    this.log(styles.muted(`(${archivedProjects.length} archived project${archivedProjects.length > 1 ? 's' : ''} hidden. Use --archived to view)`));
                }
            }
            return;
        }
        const title = flags.archived ? 'Archived Projects' : flags.all ? 'All Projects' : 'Projects';
        this.log(styles.title(`\n${title}\n`));
        for (const project of projects) {
            const isDefault = project.id === 'default';
            const markers = [];
            if (isDefault)
                markers.push(styles.success('default'));
            if (project.isArchived)
                markers.push(styles.muted('archived'));
            const markerStr = markers.length > 0 ? ` (${markers.join(', ')})` : '';
            this.log(`  ${styles.emphasis(project.name)}${markerStr}`);
            this.log(styles.muted(`    ID: ${project.id}`));
            // Show ticket count
            const ticketCount = ticketCountMap.get(project.id) ?? 0;
            this.log(styles.muted(`    Tickets: ${ticketCount}`));
            // Show phase if set
            if (project.phaseId) {
                const phase = phaseMap.get(project.phaseId);
                if (phase) {
                    this.log(styles.muted(`    Phase: ${phase.name}`));
                }
            }
            if (project.description) {
                this.log(styles.muted(`    ${project.description}`));
            }
            this.log('');
        }
        // Show hint about archived projects when viewing active
        if (!flags.archived && !flags.all) {
            const archivedProjects = await this.storage.listProjects({ isArchived: true });
            if (archivedProjects.length > 0) {
                this.log(styles.muted(`(${archivedProjects.length} archived project${archivedProjects.length > 1 ? 's' : ''} hidden. Use --archived to view)`));
            }
        }
    }
}
//# sourceMappingURL=list.js.map