"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 backup_client_1 = require("../../../lib/backup-client");
const context_banner_1 = require("../../../lib/context-banner");
const SCOPES = [
    'cluster_all',
    'namespaces',
    'applications',
    'label_selector',
];
const PROFILES = ['single', 'mirrored', 'custom'];
class BackupPolicyCreate extends core_1.Command {
    async run() {
        const { flags } = await this.parse(BackupPolicyCreate);
        (0, context_banner_1.printContextBanner)();
        const destinations = flags.destination.map((spec) => {
            const [id, role = 'primary', prio] = spec.split(':');
            return {
                destinationId: id,
                role: role,
                ...(prio ? { priority: Number.parseInt(prio, 10) } : {}),
            };
        });
        let scopeSelector;
        if (flags['scope-namespaces']) {
            scopeSelector = {
                namespaces: flags['scope-namespaces'].split(',').map((s) => s.trim()),
            };
        }
        else if (flags['scope-apps']) {
            scopeSelector = {
                applicationIds: flags['scope-apps'].split(',').map((s) => s.trim()),
            };
        }
        const client = backup_client_1.BackupClient.fromConfig();
        const spinner = (0, ora_1.default)('Creating policy...').start();
        try {
            const p = await client.createPolicy({
                name: flags.name,
                clusterId: flags.cluster,
                scope: flags.scope,
                profile: flags.profile,
                schedule: flags.schedule,
                retentionDays: flags['retention-days'],
                retentionMaxCopies: flags['retention-max-copies'],
                enabled: flags.enabled,
                destinations,
                scopeSelector,
            });
            spinner.succeed(`Created policy ${chalk_1.default.cyan(p.id)}`);
        }
        catch (err) {
            spinner.fail(`Create failed: ${err.message}`);
            this.exit(1);
        }
    }
}
BackupPolicyCreate.description = 'Create a backup policy';
BackupPolicyCreate.examples = [
    '<%= config.bin %> <%= command.id %> --name daily-all --cluster <id> --scope cluster_all --schedule "0 2 * * *" --retention-days 14 --destination <destId>',
    '<%= config.bin %> <%= command.id %> --name app-snap --cluster <id> --scope applications --scope-namespaces ns1,ns2 --destination <destId>',
];
BackupPolicyCreate.flags = {
    name: core_1.Flags.string({ required: true }),
    cluster: core_1.Flags.string({ required: true, description: 'Cluster ID' }),
    scope: core_1.Flags.string({ required: true, options: [...SCOPES] }),
    'scope-namespaces': core_1.Flags.string({
        description: 'Comma-separated namespaces (for scope=namespaces)',
    }),
    'scope-apps': core_1.Flags.string({
        description: 'Comma-separated application IDs (for scope=applications)',
    }),
    profile: core_1.Flags.string({
        options: [...PROFILES],
        default: 'single',
    }),
    schedule: core_1.Flags.string({
        description: 'Cron schedule (e.g. "0 2 * * *" for daily 02:00 UTC)',
    }),
    'retention-days': core_1.Flags.integer({ min: 1, default: 30 }),
    'retention-max-copies': core_1.Flags.integer({ min: 1 }),
    'include-pvcs': core_1.Flags.boolean(),
    'include-etcd-l1': core_1.Flags.boolean(),
    enabled: core_1.Flags.boolean({ default: true, allowNo: true }),
    destination: core_1.Flags.string({
        required: true,
        multiple: true,
        description: 'Destination spec: <destId>[:primary|replica[:priority]] (repeatable)',
    }),
};
exports.default = BackupPolicyCreate;
