// SolaceNet Seed Script
// Populates initial capability catalog

import { capabilityRegistryService } from '../src/core/solacenet/registry/capability-registry.service';
import { logger } from '../src/infrastructure/monitoring/logger';

async function seedCapabilities() {
  logger.info('Starting SolaceNet capability seed...');

  const capabilities = [
    // Core Platform Primitives
    {
      capabilityId: 'tenant-service',
      name: 'Tenant Service',
      description: 'Tenant, program, and environment management',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'iam-auth',
      name: 'IAM/Auth Service',
      description: 'OIDC, RBAC/ABAC, service-to-service authentication',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'kyc-kyb',
      name: 'KYC/KYB Orchestration',
      description: 'Workflow, document intake, verification',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'aml-monitoring',
      name: 'AML Monitoring',
      description: 'Transaction monitoring rules and scoring',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'ledger',
      name: 'Ledger Service',
      description: 'Double-entry, immutable journal',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'limits-velocity',
      name: 'Limits & Velocity Controls',
      description: 'Per user/account/merchant limits',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'fees-pricing',
      name: 'Fees & Pricing Engine',
      description: 'Fee schedules, interchange sharing, tiering',
      defaultState: 'enabled',
    },
    {
      capabilityId: 'risk-rules',
      name: 'Risk Rules Engine',
      description: 'Configurable fraud detection rules',
      defaultState: 'enabled',
    },

    // Payments & Merchant
    {
      capabilityId: 'payment-gateway',
      name: 'Payment Gateway',
      description: 'Pay-in intents, captures, refunds',
      defaultState: 'disabled',
      dependencies: ['ledger', 'limits-velocity', 'fees-pricing'],
    },
    {
      capabilityId: 'merchant-onboarding',
      name: 'Merchant Onboarding',
      description: 'KYB + underwriting',
      defaultState: 'disabled',
      dependencies: ['kyc-kyb'],
    },
    {
      capabilityId: 'merchant-processing',
      name: 'Merchant Processing',
      description: 'Authorization/capture/refund',
      defaultState: 'disabled',
      dependencies: ['payment-gateway'],
    },
    {
      capabilityId: 'disputes',
      name: 'Disputes/Chargebacks',
      description: 'Representment workflows',
      defaultState: 'disabled',
      dependencies: ['merchant-processing'],
    },
    {
      capabilityId: 'payouts',
      name: 'Payouts',
      description: 'Bank payout, push-to-card payout',
      defaultState: 'disabled',
      dependencies: ['payment-gateway', 'ledger'],
    },

    // Cards
    {
      capabilityId: 'card-issuing',
      name: 'Card Issuing',
      description: 'Virtual/physical card issuance',
      defaultState: 'disabled',
      dependencies: ['ledger', 'risk-rules'],
    },
    {
      capabilityId: 'card-lifecycle',
      name: 'Card Lifecycle',
      description: 'Create/activate/PIN/replace',
      defaultState: 'disabled',
      dependencies: ['card-issuing'],
    },
    {
      capabilityId: 'card-controls',
      name: 'Card Controls',
      description: 'Freeze, limits, MCC/merchant category blocks',
      defaultState: 'disabled',
      dependencies: ['card-issuing'],
    },
    {
      capabilityId: 'authorization-decisioning',
      name: 'Authorization Decisioning',
      description: 'Real-time rules + risk',
      defaultState: 'disabled',
      dependencies: ['card-issuing', 'risk-rules'],
    },

    // Wallets & Transfers
    {
      capabilityId: 'wallet-accounts',
      name: 'Wallet Accounts',
      description: 'Stored value with sub-ledgers',
      defaultState: 'disabled',
      dependencies: ['ledger'],
    },
    {
      capabilityId: 'p2p-transfers',
      name: 'P2P Transfers',
      description: 'Internal transfers',
      defaultState: 'disabled',
      dependencies: ['wallet-accounts'],
    },
    {
      capabilityId: 'bank-transfers',
      name: 'Bank Transfers',
      description: 'ACH/SEPA/FPS via connector',
      defaultState: 'disabled',
      dependencies: ['wallet-accounts'],
    },
    {
      capabilityId: 'account-funding',
      name: 'Account Funding',
      description: 'Bank transfer, card, cash',
      defaultState: 'disabled',
      dependencies: ['wallet-accounts'],
    },

    // Mobile Money
    {
      capabilityId: 'mobile-money-connector',
      name: 'Mobile Money Connector',
      description: 'Abstraction layer for mobile money providers',
      defaultState: 'disabled',
      dependencies: ['ledger'],
    },
    {
      capabilityId: 'mobile-money-cash-in',
      name: 'Mobile Money Cash-In',
      description: 'Cash-in orchestration',
      defaultState: 'disabled',
      dependencies: ['mobile-money-connector'],
    },
    {
      capabilityId: 'mobile-money-cash-out',
      name: 'Mobile Money Cash-Out',
      description: 'Cash-out orchestration',
      defaultState: 'disabled',
      dependencies: ['mobile-money-connector'],
    },
    {
      capabilityId: 'mobile-money-transfers',
      name: 'Mobile Money Transfers',
      description: 'Domestic transfers',
      defaultState: 'disabled',
      dependencies: ['mobile-money-connector'],
    },
  ];

  for (const cap of capabilities) {
    try {
      await capabilityRegistryService.createCapability({
        capabilityId: cap.capabilityId,
        name: cap.name,
        version: '1.0.0',
        description: cap.description,
        defaultState: cap.defaultState as any,
        dependencies: cap.dependencies || [],
      });
      logger.info(`Created capability: ${cap.capabilityId}`);
    } catch (error: any) {
      if (error.message?.includes('already exists')) {
        logger.info(`Capability ${cap.capabilityId} already exists, skipping`);
      } else {
        logger.error(`Failed to create capability ${cap.capabilityId}`, { error });
      }
    }
  }

  logger.info('SolaceNet capability seed completed!');
}

// Run if called directly
if (require.main === module) {
  seedCapabilities()
    .then(() => {
      logger.info('Seed script completed successfully');
      process.exit(0);
    })
    .catch((error) => {
      logger.error('Seed script failed', { error });
      process.exit(1);
    });
}

export { seedCapabilities };
