/**
 * Summary command for Night Watch CLI
 * Shows a "morning briefing" combining job runs, PRs, and queue status
 */
import path from 'path';
import chalk from 'chalk';
import { DEFAULT_SUMMARY_WINDOW_HOURS, createTable, dim, getSummaryData, header, info, loadConfig, } from '@night-watch/core';
/**
 * Format duration from seconds to human-readable string (e.g., "8m 32s")
 */
function formatDuration(seconds) {
    if (seconds === null)
        return '-';
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    if (mins === 0)
        return `${secs}s`;
    return `${mins}m ${secs}s`;
}
/**
 * Format CI status with color coding
 */
function formatCiStatus(status) {
    if (status === 'pass')
        return chalk.green('pass');
    if (status === 'fail')
        return chalk.red('fail');
    if (status === 'pending')
        return chalk.yellow('pending');
    return chalk.dim('unknown');
}
/**
 * Format review score with color coding
 */
function formatReviewScore(score) {
    if (score === null)
        return chalk.dim('-');
    if (score >= 80)
        return chalk.green(String(score));
    if (score >= 60)
        return chalk.yellow(String(score));
    return chalk.red(String(score));
}
/**
 * Format job status with color coding
 */
function formatJobStatus(status) {
    if (status === 'success')
        return chalk.green('success');
    if (status === 'failure')
        return chalk.red('failure');
    if (status === 'timeout')
        return chalk.yellow('timeout');
    if (status === 'rate_limited')
        return chalk.magenta('rate_limited');
    if (status === 'skipped')
        return chalk.dim('skipped');
    return chalk.dim(status);
}
/**
 * Extract project name from path
 */
function getProjectName(projectPath) {
    return path.basename(projectPath) || projectPath;
}
/**
 * Format provider key for display
 */
function formatProvider(providerKey) {
    return providerKey.split(':')[0] || providerKey;
}
/**
 * Summary command implementation
 */
export function summaryCommand(program) {
    program
        .command('summary')
        .description('Show a summary of recent Night Watch activity')
        .option('--hours <n>', 'Time window in hours (default: 12)', String(DEFAULT_SUMMARY_WINDOW_HOURS))
        .option('--json', 'Output summary as JSON')
        .action(async (options) => {
        try {
            const projectDir = process.cwd();
            const config = loadConfig(projectDir);
            const hours = parseInt(options.hours || String(DEFAULT_SUMMARY_WINDOW_HOURS), 10);
            if (isNaN(hours) || hours <= 0) {
                console.error('Error: --hours must be a positive integer');
                process.exit(1);
            }
            const data = await getSummaryData(projectDir, hours, config.branchPatterns);
            // Output as JSON if requested
            if (options.json) {
                console.log(JSON.stringify(data, null, 2));
                return;
            }
            // Print header
            console.log();
            console.log(chalk.bold.cyan(`Night Watch Summary (last ${data.windowHours}h)`));
            console.log(chalk.dim('─'.repeat(40)));
            console.log();
            // Jobs executed section
            if (data.jobRuns.length === 0) {
                info('No recent activity in this time window.');
                console.log();
            }
            else {
                // Job counts with colored indicators
                const countParts = [];
                if (data.counts.succeeded > 0) {
                    countParts.push(chalk.green(`✓ ${data.counts.succeeded} succeeded`));
                }
                if (data.counts.failed > 0) {
                    countParts.push(chalk.red(`✗ ${data.counts.failed} failed`));
                }
                if (data.counts.timedOut > 0) {
                    countParts.push(chalk.yellow(`⏱ ${data.counts.timedOut} timed out`));
                }
                if (data.counts.rateLimited > 0) {
                    countParts.push(chalk.magenta(`⏳ ${data.counts.rateLimited} rate limited`));
                }
                if (data.counts.skipped > 0) {
                    countParts.push(chalk.dim(`${data.counts.skipped} skipped`));
                }
                console.log(`Jobs Executed: ${data.counts.total}`);
                if (countParts.length > 0) {
                    console.log(`  ${countParts.join('   ')}`);
                }
                console.log();
                // Job runs table
                const table = createTable({
                    head: ['Job', 'Status', 'Project', 'Provider', 'Duration'],
                    colWidths: [12, 12, 20, 12, 12],
                });
                for (const run of data.jobRuns.slice(0, 10)) {
                    table.push([
                        run.jobType,
                        formatJobStatus(run.status),
                        getProjectName(run.projectPath),
                        formatProvider(run.providerKey),
                        formatDuration(run.durationSeconds),
                    ]);
                }
                console.log(table.toString());
                if (data.jobRuns.length > 10) {
                    dim(`  ... and ${data.jobRuns.length - 10} more`);
                }
                console.log();
            }
            // Open PRs section
            if (data.openPrs.length > 0) {
                header(`Open PRs (${data.openPrs.length})`);
                const prTable = createTable({
                    head: ['#', 'Title', 'CI', 'Score'],
                    colWidths: [6, 40, 10, 8],
                });
                for (const pr of data.openPrs) {
                    const title = pr.title.length > 37 ? pr.title.substring(0, 34) + '...' : pr.title;
                    prTable.push([
                        String(pr.number),
                        title,
                        formatCiStatus(pr.ciStatus),
                        formatReviewScore(pr.reviewScore),
                    ]);
                }
                console.log(prTable.toString());
                console.log();
            }
            // Queue section
            if (data.pendingQueueItems.length > 0) {
                const jobTypes = [...new Set(data.pendingQueueItems.map((item) => item.jobType))];
                const projectNames = [...new Set(data.pendingQueueItems.map((item) => item.projectName))];
                dim(`Queue: ${data.pendingQueueItems.length} pending (${jobTypes.join(', ')}) for ${projectNames.join(', ')}`);
                console.log();
            }
            // Action items or "all healthy" message
            if (data.actionItems.length > 0) {
                console.log(chalk.yellow('⚠ Action needed:'));
                for (const item of data.actionItems) {
                    console.log(`  • ${item}`);
                }
            }
            else {
                console.log(chalk.green('✓ No action needed — all jobs healthy.'));
            }
            console.log();
        }
        catch (error) {
            console.error(`Error getting summary: ${error instanceof Error ? error.message : String(error)}`);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=summary.js.map