// Configuration Generator Service
// Generates IRU configuration

import { ResourceAllocation } from './resource-allocator.service';

export interface ConfigurationRequest {
  subscription: any;
  resources: ResourceAllocation;
  deploymentConfig?: any;
}

export interface IRUConfiguration {
  containers: Array<{
    name: string;
    config: any;
  }>;
  network: any;
  security: any;
  monitoring: any;
}

export class ConfigurationGenerator {
  /**
   * Generate IRU configuration
   */
  async generate(request: ConfigurationRequest): Promise<IRUConfiguration> {
    const { subscription, resources, deploymentConfig } = request;

    // Generate container configurations
    const containers = resources.containers.map((container) => ({
      name: container.name,
      config: this.generateContainerConfig(container, subscription, deploymentConfig),
    }));

    // Generate network configuration
    const network = this.generateNetworkConfig(resources.network, subscription);

    // Generate security configuration
    const security = this.generateSecurityConfig(subscription);

    // Generate monitoring configuration
    const monitoring = this.generateMonitoringConfig(subscription);

    return {
      containers,
      network,
      security,
      monitoring,
    };
  }

  /**
   * Generate container configuration
   */
  private generateContainerConfig(container: any, subscription: any, deploymentConfig?: any): any {
    const baseConfig: any = {
      hostname: container.name,
      resources: {
        cpu: container.resources.cpu,
        memory: container.resources.memory,
        disk: container.resources.disk,
      },
      network: {
        ip: container.network.ip,
        gateway: container.network.gateway || '10.0.0.1',
      },
    };

    // Container-specific configurations
    switch (container.type) {
      case 'besu-sentry':
        baseConfig.besu = {
          networkId: 138,
          rpcEnabled: true,
          wsEnabled: true,
        };
        break;
      case 'firefly-core':
        baseConfig.firefly = {
          namespace: subscription.subscriptionId,
          database: {
            type: 'postgres',
            host: 'lxc-firefly-db',
          },
        };
        break;
      case 'firefly-db':
        baseConfig.postgres = {
          database: `firefly_${subscription.subscriptionId}`,
          user: 'firefly',
        };
        break;
      case 'monitoring':
        baseConfig.monitoring = {
          prometheus: true,
          grafana: true,
        };
        break;
    }

    return baseConfig;
  }

  /**
   * Generate network configuration
   */
  private generateNetworkConfig(network: any, subscription: any): any {
    return {
      vlan: network.vlan,
      subnet: network.subnet,
      gateway: network.gateway,
      dns: ['8.8.8.8', '8.8.4.4'],
      firewall: {
        enabled: true,
        rules: this.generateFirewallRules(subscription),
      },
    };
  }

  /**
   * Generate security configuration
   */
  private generateSecurityConfig(subscription: any): any {
    return {
      tls: {
        enabled: true,
        certAuthority: 'DBIS-CA',
      },
      keyManagement: {
        provider: 'external',
        location: 'secure',
      },
      accessControl: {
        enabled: true,
        method: 'keycloak',
      },
    };
  }

  /**
   * Generate monitoring configuration
   */
  private generateMonitoringConfig(subscription: any): any {
    return {
      metrics: {
        enabled: true,
        interval: 30,
      },
      logging: {
        enabled: true,
        level: 'info',
      },
      alerts: {
        enabled: true,
        channels: ['email', 'portal'],
      },
    };
  }

  /**
   * Generate firewall rules
   */
  private generateFirewallRules(subscription: any): any[] {
    return [
      {
        direction: 'in',
        action: 'ACCEPT',
        protocol: 'tcp',
        port: 8545, // Besu RPC
        source: '0.0.0.0/0',
      },
      {
        direction: 'in',
        action: 'ACCEPT',
        protocol: 'tcp',
        port: 5000, // FireFly API
        source: '0.0.0.0/0',
      },
      {
        direction: 'in',
        action: 'DROP',
        protocol: 'all',
        source: '0.0.0.0/0',
      },
    ];
  }
}

export const configurationGenerator = new ConfigurationGenerator();
