// Qualification Engine Tests

import { qualificationEngine } from '@/core/iru/qualification/qualification-engine.service';
import { institutionalVerifier } from '@/core/iru/qualification/institutional-verifier.service';
import { capacityTierAssessor } from '@/core/iru/qualification/capacity-tier-assessor.service';
import { regulatoryComplianceChecker } from '@/core/iru/qualification/regulatory-compliance-checker.service';
import { jurisdictionalLawReviewer } from '@/core/iru/qualification/jurisdictional-law-reviewer.service';
import { technicalCapabilityAssessor } from '@/core/iru/qualification/technical-capability-assessor.service';
import prisma from '@/shared/database/prisma';

jest.mock('@/shared/database/prisma');
jest.mock('@/core/iru/qualification/institutional-verifier.service');
jest.mock('@/core/iru/qualification/capacity-tier-assessor.service');
jest.mock('@/core/iru/qualification/regulatory-compliance-checker.service');
jest.mock('@/core/iru/qualification/jurisdictional-law-reviewer.service');
jest.mock('@/core/iru/qualification/technical-capability-assessor.service');

describe('QualificationEngine', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('processQualification', () => {
    it('should process qualification successfully', async () => {
      const mockInquiry = {
        inquiryId: 'INQ-12345678',
        organizationName: 'Test Central Bank',
        institutionalType: 'CentralBank',
        jurisdiction: 'US',
        estimatedVolume: '1M',
        offering: {
          capacityTier: 1,
        },
      };

      (prisma.iruInquiry.findUnique as jest.Mock).mockResolvedValue(mockInquiry);
      (prisma.iruInquiry.update as jest.Mock).mockResolvedValue(mockInquiry);

      (institutionalVerifier.verify as jest.Mock).mockResolvedValue({
        verified: true,
        verifiedType: 'CentralBank',
        confidence: 0.9,
        riskScore: 20,
        details: {},
        issues: [],
      });

      (capacityTierAssessor.assess as jest.Mock).mockResolvedValue({
        requestedTier: 1,
        recommendedTier: 1,
        reasoning: 'Tier 1 for Central Bank',
        riskScore: 20,
        usageProfile: {},
      });

      (regulatoryComplianceChecker.check as jest.Mock).mockResolvedValue({
        compliant: true,
        riskScore: 20,
        checks: {},
        issues: [],
      });

      (jurisdictionalLawReviewer.review as jest.Mock).mockResolvedValue({
        approved: true,
        riskScore: 25,
        review: {},
        issues: [],
      });

      (technicalCapabilityAssessor.assess as jest.Mock).mockResolvedValue({
        capable: true,
        riskScore: 30,
        assessment: {},
        recommendations: [],
      });

      const result = await qualificationEngine.processQualification({
        inquiryId: 'INQ-12345678',
        preliminaryInfo: {},
      });

      expect(result.qualified).toBe(true);
      expect(result.capacityTier).toBe(1);
      expect(result.riskScore).toBeGreaterThan(0);
    });
  });
});
