// Complete Compliance Test Suite

import { createTestSovereignBank, cleanDatabase } from '@/__tests__/utils/test-db';
import { testPrisma } from '@/__tests__/utils/test-db';
import { amlService } from '@/core/compliance/aml.service';
import { ComplianceRecordType } from '@/shared/types';

describe('Compliance - Complete Test Suite', () => {
  beforeEach(async () => {
    await cleanDatabase();
  });

  afterAll(async () => {
    await testPrisma.$disconnect();
  });

  describe('AML Screening', () => {
    it('should screen transactions for AML violations', async () => {
      const bank = await createTestSovereignBank();

      const result = await amlService.screenTransaction({
        transactionId: 'TEST-TX-001',
        sourceAccountId: 'test-account-1',
        destinationAccountId: 'test-account-2',
        amount: '10000.00',
        currencyCode: 'USD',
        transactionType: 'transfer',
      });

      expect(result).toBeDefined();
      expect(result.riskScore).toBeGreaterThanOrEqual(0);
      expect(result.riskScore).toBeLessThanOrEqual(100);
    });

    it('should flag high-risk transactions', async () => {
      const bank = await createTestSovereignBank();

      // Large amount that should trigger AML
      const result = await amlService.screenTransaction({
        transactionId: 'TEST-TX-002',
        sourceAccountId: 'test-account-1',
        destinationAccountId: 'test-account-2',
        amount: '1000000.00',
        currencyCode: 'USD',
        transactionType: 'transfer',
      });

      expect(result.riskScore).toBeGreaterThan(50);
    });
  });

  describe('Sanctions Checking', () => {
    it('should check entities against sanctions list', async () => {
      const bank = await createTestSovereignBank();

      const result = await amlService.checkSanctions({
        entityId: 'test-entity',
        entityType: 'account',
      });

      expect(result).toBeDefined();
      expect(result.isSanctioned).toBeDefined();
    });
  });

  describe('PEP Detection', () => {
    it('should detect politically exposed persons', async () => {
      const bank = await createTestSovereignBank();

      const result = await amlService.checkPEP({
        entityId: 'test-entity',
        entityType: 'account',
      });

      expect(result).toBeDefined();
      expect(result.isPEP).toBeDefined();
    });
  });

  describe('Risk Scoring', () => {
    it('should calculate comprehensive risk score', async () => {
      const bank = await createTestSovereignBank();

      const riskScore = await amlService.calculateRiskScore({
        transactionId: 'TEST-TX-003',
        sourceAccountId: 'test-account-1',
        destinationAccountId: 'test-account-2',
        amount: '50000.00',
        currencyCode: 'USD',
      });

      expect(riskScore).toBeGreaterThanOrEqual(0);
      expect(riskScore).toBeLessThanOrEqual(100);
    });
  });

  describe('Compliance Record Creation', () => {
    it('should create compliance record for flagged transactions', async () => {
      const bank = await createTestSovereignBank();

      const record = await amlService.createComplianceRecord({
        entityId: 'test-entity',
        entityType: 'transaction',
        recordType: ComplianceRecordType.AML_ALERT,
        riskScore: 75,
        details: {
          reason: 'High-value transaction',
        },
      });

      expect(record.id).toBeDefined();
      expect(record.status).toBeDefined();
    });
  });
});

