/**
 * Integration Test: Onboarding Validation Use Case Orchestration
 * 
 * Tests the orchestration logic of validation-related Use Cases:
 * - CompleteDriverOnboardingUseCase: Validates driver data before creation
 * 
 * Validates that Use Cases correctly interact with their Ports (Repositories)
 * Uses In-Memory adapters for fast, deterministic testing
 * 
 * Focus: Business logic orchestration, NOT UI rendering
 */

import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { CompleteDriverOnboardingUseCase } from '../../../core/racing/application/use-cases/CompleteDriverOnboardingUseCase';
import { Logger } from '../../../core/shared/domain/Logger';

describe('Onboarding Validation Use Case Orchestration', () => {
  let driverRepository: InMemoryDriverRepository;
  let completeDriverOnboardingUseCase: CompleteDriverOnboardingUseCase;
  let mockLogger: Logger;

  beforeAll(() => {
    mockLogger = {
      info: () => {},
      debug: () => {},
      warn: () => {},
      error: () => {},
    } as unknown as Logger;

    driverRepository = new InMemoryDriverRepository(mockLogger);
    completeDriverOnboardingUseCase = new CompleteDriverOnboardingUseCase(
      driverRepository,
      mockLogger
    );
  });

  beforeEach(async () => {
    await driverRepository.clear();
  });

  describe('CompleteDriverOnboardingUseCase - Validation Scenarios', () => {
    it('should validate that driver does not already exist', async () => {
      // Scenario: Duplicate driver validation
      // Given: A driver already exists
      const userId = 'duplicate-user';
      await completeDriverOnboardingUseCase.execute({
        userId,
        firstName: 'First',
        lastName: 'Last',
        displayName: 'FirstLast',
        country: 'US',
      });

      // When: Attempting to onboard again
      const result = await completeDriverOnboardingUseCase.execute({
        userId,
        firstName: 'Second',
        lastName: 'Attempt',
        displayName: 'SecondAttempt',
        country: 'US',
      });

      // Then: Validation should fail
      expect(result.isErr()).toBe(true);
      expect(result.unwrapErr().code).toBe('DRIVER_ALREADY_EXISTS');
    });
  });
});
