// Provision Admin Vault for Sankofa Admin Portal
// Creates the admin vault using the provisioning service

import { adminVaultProvisioningService } from '../src/core/iru/provisioning/admin-vault-provisioning.service';

async function main() {
  const args = process.argv.slice(2);
  
  // Parse arguments
  let orgName = 'Sankofa Admin';
  let vaultName = 'sankofa-admin';
  let adminLevel: 'super_admin' | 'admin' | 'operator' = 'super_admin';
  
  for (let i = 0; i < args.length; i++) {
    if (args[i] === '--org' && args[i + 1]) {
      orgName = args[i + 1];
      i++;
    } else if (args[i] === '--name' && args[i + 1]) {
      vaultName = args[i + 1];
      i++;
    } else if (args[i] === '--level' && args[i + 1]) {
      adminLevel = args[i + 1] as 'super_admin' | 'admin' | 'operator';
      i++;
    }
  }

  console.log('Provisioning Admin Vault...');
  console.log(`  Organization: ${orgName}`);
  console.log(`  Vault Name: ${vaultName}`);
  console.log(`  Admin Level: ${adminLevel}`);
  console.log('');

  try {
    const result = await adminVaultProvisioningService.provisionAdminVault({
      organizationName: orgName,
      vaultName: vaultName,
      adminLevel: adminLevel,
      features: {
        auditLogging: true,
        backupEnabled: true,
        encryptionLevel: 'enhanced',
      },
    });

    console.log('✅ Admin vault provisioned successfully!');
    console.log('');
    console.log('Vault Details:');
    console.log(`  Vault ID: ${result.vaultId}`);
    console.log(`  Vault Path: ${result.vaultPath}`);
    console.log(`  API Endpoint: ${result.apiEndpoint}`);
    console.log(`  Role ID: ${result.roleId}`);
    console.log(`  Secret ID: ${result.secretId}`);
    console.log('');
    console.log('⚠️  IMPORTANT: Store these credentials securely!');
    console.log('');
    console.log('Next steps:');
    console.log('1. Run migration script: ./scripts/migrate-secrets-to-admin-vault.sh');
    console.log('2. Store credentials in secure location');
    console.log('3. Update applications to use admin vault');
  } catch (error) {
    console.error('❌ Failed to provision admin vault:', error);
    process.exit(1);
  }
}

main();
