"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 nest_app_1 = require("../../lib/nest-app");
const prompts_1 = require("../../lib/prompts");
const hetzner_dns_service_1 = require("../../../../src/modules/providers/services/hetzner-dns.service");
const dns_provider_interface_1 = require("../../../../src/modules/providers/interfaces/dns-provider.interface");
class DnsCleanup extends core_1.Command {
    async run() {
        const { flags } = await this.parse(DnsCleanup);
        let spinner = (0, ora_1.default)('Looking up zone...').start();
        try {
            const app = await (0, nest_app_1.getNestApp)();
            const hetznerDns = app.get(hetzner_dns_service_1.HetznerDnsService);
            const zone = await hetznerDns.getZoneByName(flags.domain);
            if (!zone) {
                spinner.fail(`Zone not found: ${flags.domain}`);
                this.exit(1);
            }
            spinner.succeed(`Zone found: ${zone.zoneId}`);
            spinner = (0, ora_1.default)('Fetching A records...').start();
            const records = await hetznerDns.listRecords(zone.zoneId);
            const aRecords = records.filter((r) => r.type === dns_provider_interface_1.DnsRecordType.A);
            const rrsetNames = [...new Set(aRecords.map((r) => r.name))];
            spinner.succeed(`Found ${aRecords.length} A value(s) across ${rrsetNames.length} RRSet(s)`);
            console.log(chalk_1.default.yellow(`\n  Zone:           ${flags.domain}`));
            console.log(chalk_1.default.yellow(`  A RRSets:       ${rrsetNames.length}`));
            console.log(chalk_1.default.yellow(`  Total A values: ${aRecords.length}\n`));
            for (const name of rrsetNames) {
                const values = aRecords
                    .filter((r) => r.name === name)
                    .map((r) => r.value);
                console.log(`  ${chalk_1.default.bold(name)} → ${values.join(', ')}`);
            }
            if (flags['dry-run']) {
                console.log(chalk_1.default.green('\n  Dry run complete. No records deleted.\n'));
                return;
            }
            if (rrsetNames.length === 0) {
                console.log(chalk_1.default.green('\n  No A records found. Nothing to do.\n'));
                return;
            }
            if (!flags.force) {
                console.log('');
                console.log(chalk_1.default.yellow(`  Type 'yes' to delete all ${rrsetNames.length} A RRSet(s) from ${flags.domain}:`));
                const confirmed = await (0, prompts_1.confirmByTypingPrompt)(chalk_1.default.yellow('Confirm'), 'yes');
                if (!confirmed) {
                    console.log(chalk_1.default.green('\n  Deletion cancelled.\n'));
                    return;
                }
            }
            spinner = (0, ora_1.default)('Deleting A records...').start();
            const deleted = await hetznerDns.purgeARecords(zone.zoneId);
            spinner.succeed(`Deleted ${deleted} A RRSet(s)`);
            console.log(chalk_1.default.green(`\n  Cleaned up ${deleted} A RRSet(s) from ${flags.domain}\n`));
        }
        catch (error) {
            spinner.fail('DNS cleanup failed');
            console.log(chalk_1.default.red(`\n  ${error instanceof Error ? error.message : String(error)}\n`));
            this.exit(1);
        }
        finally {
            await (0, nest_app_1.closeNestApp)();
        }
    }
}
DnsCleanup.description = 'Delete all A records from a Hetzner DNS zone';
DnsCleanup.examples = [
    '<%= config.bin %> <%= command.id %> --domain example.com',
    '<%= config.bin %> <%= command.id %> --domain example.com --dry-run',
    '<%= config.bin %> <%= command.id %> --domain example.com --force',
];
DnsCleanup.flags = {
    domain: core_1.Flags.string({
        required: true,
        description: 'Hetzner DNS zone domain name',
    }),
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Skip confirmation prompt',
        default: false,
    }),
    'dry-run': core_1.Flags.boolean({
        description: 'Preview without deleting',
        default: false,
    }),
};
exports.default = DnsCleanup;
