"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const cli_app_service_1 = require("../../lib/services/cli-app.service");
const resolve_cluster_1 = require("../../lib/resolve-cluster");
class AppCrash extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppCrash);
        const verb = flags.dismiss ? 'Dismissing' : 'Fetching';
        const spinner = (0, ora_1.default)(`${verb} crash ${args.id}...`).start();
        try {
            const { id: clusterId } = await (0, resolve_cluster_1.resolveCluster)(flags.cluster);
            const service = await cli_app_service_1.CliAppService.create(clusterId);
            const app = await service.getAppByName(args.name);
            const d = flags.dismiss
                ? await service.dismissCrash(app.id, args.id)
                : await service.getCrash(app.id, args.id);
            spinner.stop();
            if (flags.output === 'json') {
                console.log(JSON.stringify(d, null, 2));
                return;
            }
            if (flags.dismiss) {
                console.log(chalk_1.default.green(`\n  ✔ Crash ${d.id} marked as resolved.\n`));
                return;
            }
            console.log(chalk_1.default.cyan(`\n  Crash ${d.id}\n`));
            console.log(`  ${chalk_1.default.dim('title:')}      ${d.title}`);
            console.log(`  ${chalk_1.default.dim('category:')}   ${d.category}`);
            console.log(`  ${chalk_1.default.dim('severity:')}   ${this.colorSeverity(d.severity)}`);
            console.log(`  ${chalk_1.default.dim('pod:')}        ${d.podName}`);
            if (d.containerName) {
                console.log(`  ${chalk_1.default.dim('container:')}  ${d.containerName}`);
            }
            console.log(`  ${chalk_1.default.dim('detected:')}   ${new Date(d.createdAt).toLocaleString()}`);
            console.log(`  ${chalk_1.default.dim('resolved:')}   ${d.resolvedAt ? new Date(d.resolvedAt).toLocaleString() : chalk_1.default.yellow('open')}`);
            if (d.explanation) {
                console.log(`\n  ${chalk_1.default.bold('Reason')}`);
                console.log(`  ${d.explanation}`);
            }
            const action = d.suggestedAction;
            if (action?.summary) {
                console.log(`\n  ${chalk_1.default.bold('Suggested action')}`);
                console.log(`  ${action.summary}`);
                if (Array.isArray(action.steps)) {
                    for (const step of action.steps)
                        console.log(`    • ${step}`);
                }
            }
            console.log('');
        }
        catch (error) {
            spinner.fail(`${verb} failed`);
            console.log(chalk_1.default.red(`\n  Error: ${error.message}\n`));
            this.exit(1);
        }
    }
    colorSeverity(severity) {
        switch (severity) {
            case 'critical':
                return chalk_1.default.red(severity);
            case 'warning':
                return chalk_1.default.yellow(severity);
            default:
                return chalk_1.default.dim(severity);
        }
    }
}
AppCrash.description = 'Show a single crash diagnosis. Use --dismiss to mark it resolved.';
AppCrash.examples = [
    '<%= config.bin %> <%= command.id %> my-api 5d3f...',
    '<%= config.bin %> <%= command.id %> my-api 5d3f... --dismiss',
    '<%= config.bin %> <%= command.id %> my-api 5d3f... --output json',
];
AppCrash.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
    id: core_1.Args.string({
        description: 'Crash diagnosis ID',
        required: true,
    }),
};
AppCrash.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect when only one cluster exists)',
    }),
    dismiss: core_1.Flags.boolean({
        description: 'Mark this crash diagnosis as resolved',
        default: false,
    }),
    output: core_1.Flags.string({
        char: 'o',
        description: 'Output format',
        options: ['table', 'json'],
        default: 'table',
    }),
};
exports.default = AppCrash;
