"use strict";
/**
 * Migrate Command
 *
 * CLI command to migrate from v1 (JSON) to v2 (YAML) config format.
 *
 * Usage:
 *   ccs migrate           - Run migration
 *   ccs migrate --dry-run - Preview migration without changes
 *   ccs migrate --rollback <path> - Restore from backup
 *   ccs migrate --list-backups    - List available backups
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.printMigrateHelp = exports.handleMigrateCommand = void 0;
const migration_manager_1 = require("../config/migration-manager");
const ui_1 = require("../utils/ui");
const config_loader_facade_1 = require("../config/config-loader-facade");
async function handleMigrateCommand(args) {
    await (0, ui_1.initUI)();
    // Handle --list-backups
    if (args.includes('--list-backups')) {
        listBackups();
        return;
    }
    // Handle --rollback
    if (args.includes('--rollback')) {
        const rollbackIndex = args.indexOf('--rollback');
        const backupPath = args[rollbackIndex + 1];
        if (!backupPath) {
            console.error((0, ui_1.fail)('Error: --rollback requires backup path'));
            console.log((0, ui_1.info)('Usage: ccs migrate --rollback <backup-path>'));
            console.log((0, ui_1.info)('Use --list-backups to see available backups'));
            process.exit(1);
        }
        await handleRollback(backupPath);
        return;
    }
    // Check if already migrated
    if ((0, config_loader_facade_1.hasUnifiedConfig)() && !(0, migration_manager_1.needsMigration)()) {
        console.log((0, ui_1.info)('Already using unified config format (config.yaml)'));
        return;
    }
    // Check if migration is needed
    if (!(0, migration_manager_1.needsMigration)()) {
        console.log((0, ui_1.info)('No migration needed - no legacy config found'));
        return;
    }
    // Handle --dry-run
    const dryRun = args.includes('--dry-run');
    if (dryRun) {
        console.log((0, ui_1.info)('Dry run - no changes will be made'));
        console.log('');
    }
    const result = await (0, migration_manager_1.migrate)(dryRun);
    if (result.success) {
        console.log('');
        if (dryRun) {
            console.log((0, ui_1.infoBox)('Dry run - migration preview (no changes made)'));
        }
        else {
            console.log((0, ui_1.infoBox)('Migrated to unified config (config.yaml)', 'SUCCESS'));
        }
        if (result.backupPath && !dryRun) {
            console.log(`  Backup: ${result.backupPath}`);
        }
        console.log(`  Items:  ${result.migratedFiles.length} migrated`);
        if (result.warnings.length > 0) {
            for (const warning of result.warnings) {
                console.log((0, ui_1.warn)(warning));
            }
        }
        if (dryRun) {
            console.log((0, ui_1.dim)('  Run without --dry-run to apply changes'));
        }
        else {
            console.log(`  Rollback: ccs migrate --rollback ${result.backupPath}`);
        }
        console.log('');
    }
    else {
        console.error((0, ui_1.fail)(`Migration failed: ${result.error}`));
        if (result.migratedFiles.length > 0) {
            console.log('');
            console.log('    Partially migrated:');
            result.migratedFiles.forEach((f) => console.log(`      - ${f}`));
        }
        process.exit(1);
    }
}
exports.handleMigrateCommand = handleMigrateCommand;
async function handleRollback(backupPath) {
    console.log((0, ui_1.info)(`Rolling back from: ${backupPath}`));
    console.log('');
    const success = await (0, migration_manager_1.rollback)(backupPath);
    if (success) {
        console.log((0, ui_1.ok)('Rollback complete'));
        console.log((0, ui_1.info)('Legacy config restored'));
    }
    else {
        console.error((0, ui_1.fail)('Rollback failed'));
        process.exit(1);
    }
}
function listBackups() {
    const backups = (0, migration_manager_1.getBackupDirectories)();
    if (backups.length === 0) {
        console.log((0, ui_1.info)('No backup directories found'));
        return;
    }
    console.log((0, ui_1.info)('Available backups (most recent first):'));
    console.log('');
    backups.forEach((backup, index) => {
        console.log(`    ${index + 1}. ${backup}`);
    });
    console.log('');
    console.log((0, ui_1.info)('To rollback: ccs migrate --rollback <backup-path>'));
}
/**
 * Print help for migrate command.
 */
function printMigrateHelp() {
    console.log('Usage: ccs migrate [options]');
    console.log('');
    console.log('Migrate from legacy JSON config to unified YAML format.');
    console.log('');
    console.log('Options:');
    console.log('  --dry-run        Preview migration without making changes');
    console.log('  --rollback PATH  Restore from backup directory');
    console.log('  --list-backups   List available backup directories');
    console.log('  --help           Show this help message');
    console.log('');
    console.log('Examples:');
    console.log('  ccs migrate                      # Run migration');
    console.log('  ccs migrate --dry-run            # Preview changes');
    console.log('  ccs migrate --list-backups       # List backups');
    console.log('  ccs migrate --rollback ~/.ccs/backup-v1-2025-01-15');
}
exports.printMigrateHelp = printMigrateHelp;
//# sourceMappingURL=migrate-command.js.map