"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 AppBackupDelete extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppBackupDelete);
        if (!flags['access-key'] || !flags['secret-key']) {
            this.error('S3 credentials missing. Pass --access-key/--secret-key or set FLUI_S3_ACCESS_KEY/FLUI_S3_SECRET_KEY.');
        }
        if (!flags.force) {
            const ok = await (0, prompts_1.confirmPrompt)(`Delete backup "${args.exportId}" from s3://${flags.bucket}?`);
            if (!ok) {
                console.log(chalk_1.default.dim('  Aborted.'));
                return;
            }
        }
        const destination = {
            bucket: flags.bucket,
            endpoint: flags.endpoint,
            region: flags.region,
            accessKeyId: flags['access-key'],
            secretAccessKey: flags['secret-key'],
        };
        const spinner = (0, ora_1.default)(`Deleting backup ${args.exportId}...`).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.deleteAppBackup(app.id, args.exportId, destination);
            spinner.succeed(`Deleted backup ${args.exportId}`);
        }
        catch (error) {
            spinner.fail('Backup deletion failed');
            const msg = error.response?.data?.message ?? error.message ?? String(error);
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            this.exit(1);
        }
    }
}
AppBackupDelete.description = 'Delete an S3 backup of an application. Removes all objects under the export key prefix.';
AppBackupDelete.examples = [
    '<%= config.bin %> <%= command.id %> my-app flui/<cluster>/<app>/20260510170000-abc123 -b my-bucket -e https://s3.fr-par.scw.cloud',
];
AppBackupDelete.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
    exportId: core_1.Args.string({
        description: 'Export id (S3 key prefix) returned by `flui app backup create`',
        required: true,
    }),
};
AppBackupDelete.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect)',
    }),
    bucket: core_1.Flags.string({
        char: 'b',
        description: 'S3 bucket where the backup lives',
        required: true,
    }),
    endpoint: core_1.Flags.string({
        char: 'e',
        description: 'S3 endpoint URL',
        required: true,
    }),
    region: core_1.Flags.string({
        char: 'r',
        description: 'S3 region',
        default: 'auto',
    }),
    'access-key': core_1.Flags.string({
        description: 'S3 access key id (defaults to FLUI_S3_ACCESS_KEY env)',
        env: 'FLUI_S3_ACCESS_KEY',
    }),
    'secret-key': core_1.Flags.string({
        description: 'S3 secret access key (defaults to FLUI_S3_SECRET_KEY env)',
        env: 'FLUI_S3_SECRET_KEY',
    }),
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Skip confirmation',
    }),
};
exports.default = AppBackupDelete;
