"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");
const prompts_1 = require("../../../lib/prompts");
class AppImageDelete extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppImageDelete);
        if (!flags.yes) {
            const ok = await (0, prompts_1.confirmPrompt)(`Permanently delete GHCR version ${args.versionId} for "${args.name}"?`, false);
            if (!ok) {
                console.log(chalk_1.default.dim('  Aborted.'));
                return;
            }
        }
        const spinner = (0, ora_1.default)(`Deleting version ${args.versionId}...`).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);
            await service.deleteImageVersion(app.id, args.versionId, {
                force: flags.force,
            });
            spinner.succeed(`Deleted GHCR version ${args.versionId}`);
        }
        catch (error) {
            spinner.fail('Delete failed');
            const msg = error.message ?? '';
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            if (/latest release/i.test(msg) && !flags.force) {
                console.log(chalk_1.default.dim('  Re-run with --force to override the latest-release guard.\n'));
            }
            if (/delete:packages/i.test(msg)) {
                console.log(chalk_1.default.dim('  Your GHCR PAT must be a classic token with read:packages + delete:packages scopes.\n'));
            }
            this.exit(1);
        }
    }
}
AppImageDelete.description = 'Delete a container image version from the registry (GHCR). ' +
    'Refuses to delete the currently deployed version. The latest-release guard can be overridden with --force.';
AppImageDelete.examples = [
    '<%= config.bin %> <%= command.id %> my-api 845914920',
    '<%= config.bin %> <%= command.id %> my-api 845914920 --force',
];
AppImageDelete.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
    versionId: core_1.Args.integer({
        description: 'GitHub Packages numeric version id (from `flui app versions --output json`)',
        required: true,
    }),
};
AppImageDelete.flags = {
    cluster: core_1.Flags.string({ char: 'c' }),
    force: core_1.Flags.boolean({
        description: 'Override the latest-release guard. The currently-deployed guard is never bypassable.',
    }),
    yes: core_1.Flags.boolean({
        char: 'y',
        description: 'Skip confirmation prompt',
    }),
};
exports.default = AppImageDelete;
