// Technical Capability Assessor Service
// Assesses technical capabilities

import { PreliminaryInfo } from '../types/common.types';

export interface TechnicalCapabilityResult {
  capable: boolean;
  riskScore: number;
  assessment: {
    networkConnectivity: { capable: boolean; details: any };
    security: { capable: boolean; details: any };
    integration: { capable: boolean; details: any };
    support: { capable: boolean; details: any };
  };
  recommendations: string[];
}

export class TechnicalCapabilityAssessor {
  /**
   * Assess technical capability
   */
  async assess(preliminaryInfo: PreliminaryInfo): Promise<TechnicalCapabilityResult> {
    // Integrate with technical assessment tools
    // Framework in place - actual integration depends on available tools
    // For now, perform basic validation

    const recommendations: string[] = [];
    let capable = true;
    let riskScore = 30; // Default risk score

    // Network connectivity assessment
    const networkConnectivity = this.assessNetworkConnectivity(preliminaryInfo);
    if (!networkConnectivity.capable) {
      recommendations.push('Ensure secure network connectivity to DBIS infrastructure');
      riskScore += 20;
    }

    // Security assessment
    const security = this.assessSecurity(preliminaryInfo);
    if (!security.capable) {
      recommendations.push('Implement required security measures and key management');
      riskScore += 25;
    }

    // Integration assessment
    const integration = this.assessIntegration(preliminaryInfo);
    if (!integration.capable) {
      recommendations.push('Plan for integration with core banking systems');
      riskScore += 15;
    }

    // Support assessment
    const support = this.assessSupport(preliminaryInfo);
    if (!support.capable) {
      recommendations.push('Ensure technical support team is available');
      riskScore += 10;
    }

    if (riskScore > 70) {
      capable = false;
    }

    return {
      capable,
      riskScore: Math.min(100, riskScore),
      assessment: {
        networkConnectivity,
        security,
        integration,
        support,
      },
      recommendations,
    };
  }

  private assessNetworkConnectivity(preliminaryInfo: PreliminaryInfo): {
    capable: boolean;
    details: Record<string, unknown>;
  } {
    const hasNetworkInfo = preliminaryInfo?.networkConnectivity || preliminaryInfo?.technicalContact;

    return {
      capable: hasNetworkInfo,
      details: {
        assessed: true,
        hasNetworkInfo,
      },
    };
  }

  private assessSecurity(preliminaryInfo: PreliminaryInfo): {
    capable: boolean;
    details: Record<string, unknown>;
  } {
    const hasSecurityInfo =
      preliminaryInfo?.securityContact || preliminaryInfo?.securityPlan;

    return {
      capable: hasSecurityInfo,
      details: {
        assessed: true,
        hasSecurityInfo,
        keyManagement: preliminaryInfo?.keyManagement || 'not_specified',
      },
    };
  }

  private assessIntegration(preliminaryInfo: PreliminaryInfo): {
    capable: boolean;
    details: Record<string, unknown>;
  } {
    const hasIntegrationInfo =
      preliminaryInfo?.coreBankingSystem || preliminaryInfo?.integrationPlan;

    return {
      capable: true, // Assume capable, will be validated during technical onboarding
      details: {
        assessed: true,
        hasIntegrationInfo,
        coreBankingSystem: preliminaryInfo?.coreBankingSystem,
      },
    };
  }

  private assessSupport(preliminaryInfo: PreliminaryInfo): {
    capable: boolean;
    details: Record<string, unknown>;
  } {
    const hasSupportInfo = preliminaryInfo?.technicalContact || preliminaryInfo?.supportPlan;

    return {
      capable: hasSupportInfo,
      details: {
        assessed: true,
        hasSupportInfo,
        technicalContact: preliminaryInfo?.technicalContact,
      },
    };
  }
}

export const technicalCapabilityAssessor = new TechnicalCapabilityAssessor();
