// Provisioning Validator Service
// Validates provisioning configuration

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

export interface ValidationRequest {
  resources: ResourceAllocation;
  configuration: IRUConfiguration;
  subscription: any;
}

export interface ValidationResult {
  valid: boolean;
  errors: string[];
  warnings: string[];
}

export class ProvisioningValidator {
  /**
   * Validate provisioning
   */
  async validate(request: ValidationRequest): Promise<ValidationResult> {
    const errors: string[] = [];
    const warnings: string[] = [];

    // Validate resources
    const resourceValidation = this.validateResources(request.resources);
    errors.push(...resourceValidation.errors);
    warnings.push(...resourceValidation.warnings);

    // Validate configuration
    const configValidation = this.validateConfiguration(request.configuration);
    errors.push(...configValidation.errors);
    warnings.push(...configValidation.warnings);

    // Validate subscription
    const subscriptionValidation = this.validateSubscription(request.subscription);
    errors.push(...subscriptionValidation.errors);
    warnings.push(...subscriptionValidation.warnings);

    return {
      valid: errors.length === 0,
      errors,
      warnings,
    };
  }

  /**
   * Validate resources
   */
  private validateResources(resources: ResourceAllocation): ValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    // Check containers
    if (resources.containers.length === 0) {
      errors.push('No containers allocated');
    }

    // Check required containers
    const requiredContainers = ['lxc-besu-sentry', 'lxc-firefly-core', 'lxc-firefly-db', 'lxc-monitoring'];
    const containerNames = resources.containers.map((c) => c.name);
    for (const required of requiredContainers) {
      if (!containerNames.includes(required)) {
        errors.push(`Missing required container: ${required}`);
      }
    }

    // Check network
    if (!resources.network.subnet || !resources.network.gateway) {
      errors.push('Network configuration incomplete');
    }

    // Check storage
    if (resources.storage.total < 100) {
      warnings.push('Storage allocation seems low');
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings,
    };
  }

  /**
   * Validate configuration
   */
  private validateConfiguration(config: IRUConfiguration): ValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    // Check containers configuration
    if (config.containers.length === 0) {
      errors.push('No container configurations');
    }

    // Check network configuration
    if (!config.network.subnet || !config.network.gateway) {
      errors.push('Network configuration incomplete');
    }

    // Check security configuration
    if (!config.security.tls?.enabled) {
      warnings.push('TLS not enabled - security risk');
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings,
    };
  }

  /**
   * Validate subscription
   */
  private validateSubscription(subscription: any): ValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    if (!subscription.subscriptionId) {
      errors.push('Subscription ID missing');
    }

    if (subscription.subscriptionStatus !== 'active') {
      errors.push(`Subscription status is ${subscription.subscriptionStatus}, must be active`);
    }

    if (!subscription.capacityTier || subscription.capacityTier < 1 || subscription.capacityTier > 5) {
      errors.push('Invalid capacity tier');
    }

    return {
      valid: errors.length === 0,
      errors,
      warnings,
    };
  }
}

export const provisioningValidator = new ProvisioningValidator();
