/**
 * Integration Test: Profile Use Cases Orchestration
 * 
 * Tests the orchestration logic of profile-related Use Cases:
 * - GetProfileOverviewUseCase: Retrieves driver profile overview
 * - UpdateDriverProfileUseCase: Updates driver profile information
 * - GetDriverLiveriesUseCase: Retrieves driver liveries
 * - GetLeagueMembershipsUseCase: Retrieves driver league memberships (via league)
 * - GetPendingSponsorshipRequestsUseCase: Retrieves pending sponsorship requests
 * 
 * Adheres to Clean Architecture:
 * - Tests Core Use Cases directly
 * - Uses In-Memory adapters for repositories
 * - Follows Given/When/Then pattern
 * 
 * Focus: Business logic orchestration, NOT UI rendering
 */

import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryTeamRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamRepository';
import { InMemoryTeamMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamMembershipRepository';
import { InMemorySocialGraphRepository } from '../../../adapters/social/persistence/inmemory/InMemorySocialAndFeed';
import { InMemoryDriverExtendedProfileProvider } from '../../../adapters/racing/ports/InMemoryDriverExtendedProfileProvider';
import { InMemoryDriverStatsRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverStatsRepository';
import { InMemoryLiveryRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLiveryRepository';
import { InMemoryLeagueRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueRepository';
import { InMemoryLeagueMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueMembershipRepository';
import { InMemorySponsorshipRequestRepository } from '../../../adapters/racing/persistence/inmemory/InMemorySponsorshipRequestRepository';
import { InMemorySponsorRepository } from '../../../adapters/racing/persistence/inmemory/InMemorySponsorRepository';

import { GetProfileOverviewUseCase } from '../../../core/racing/application/use-cases/GetProfileOverviewUseCase';
import { UpdateDriverProfileUseCase } from '../../../core/racing/application/use-cases/UpdateDriverProfileUseCase';
import { DriverStatsUseCase } from '../../../core/racing/application/use-cases/DriverStatsUseCase';
import { RankingUseCase } from '../../../core/racing/application/use-cases/RankingUseCase';
import { GetDriverLiveriesUseCase } from '../../../core/racing/application/use-cases/GetDriverLiveriesUseCase';
import { GetLeagueMembershipsUseCase } from '../../../core/racing/application/use-cases/GetLeagueMembershipsUseCase';
import { GetPendingSponsorshipRequestsUseCase } from '../../../core/racing/application/use-cases/GetPendingSponsorshipRequestsUseCase';

import { Driver } from '../../../core/racing/domain/entities/Driver';
import { Team } from '../../../core/racing/domain/entities/Team';
import { League } from '../../../core/racing/domain/entities/League';
import { LeagueMembership } from '../../../core/racing/domain/entities/LeagueMembership';
import { DriverLivery } from '../../../core/racing/domain/entities/DriverLivery';
import { SponsorshipRequest } from '../../../core/racing/domain/entities/SponsorshipRequest';
import { Sponsor } from '../../../core/racing/domain/entities/sponsor/Sponsor';
import { Money } from '../../../core/racing/domain/value-objects/Money';
import { Logger } from '../../../core/shared/domain/Logger';

describe('Profile Use Cases Orchestration', () => {
  let driverRepository: InMemoryDriverRepository;
  let teamRepository: InMemoryTeamRepository;
  let teamMembershipRepository: InMemoryTeamMembershipRepository;
  let socialRepository: InMemorySocialGraphRepository;
  let driverExtendedProfileProvider: InMemoryDriverExtendedProfileProvider;
  let driverStatsRepository: InMemoryDriverStatsRepository;
  let liveryRepository: InMemoryLiveryRepository;
  let leagueRepository: InMemoryLeagueRepository;
  let leagueMembershipRepository: InMemoryLeagueMembershipRepository;
  let sponsorshipRequestRepository: InMemorySponsorshipRequestRepository;
  let sponsorRepository: InMemorySponsorRepository;

  let driverStatsUseCase: DriverStatsUseCase;
  let rankingUseCase: RankingUseCase;
  let getProfileOverviewUseCase: GetProfileOverviewUseCase;
  let updateDriverProfileUseCase: UpdateDriverProfileUseCase;
  let getDriverLiveriesUseCase: GetDriverLiveriesUseCase;
  let getLeagueMembershipsUseCase: GetLeagueMembershipsUseCase;
  let getPendingSponsorshipRequestsUseCase: GetPendingSponsorshipRequestsUseCase;

  let mockLogger: Logger;

  beforeAll(() => {
    mockLogger = {
      info: () => {},
      debug: () => {},
      warn: () => {},
      error: () => {},
    } as unknown as Logger;
    
    driverRepository = new InMemoryDriverRepository(mockLogger);
    teamRepository = new InMemoryTeamRepository(mockLogger);
    teamMembershipRepository = new InMemoryTeamMembershipRepository(mockLogger);
    socialRepository = new InMemorySocialGraphRepository(mockLogger);
    driverExtendedProfileProvider = new InMemoryDriverExtendedProfileProvider(mockLogger);
    driverStatsRepository = new InMemoryDriverStatsRepository(mockLogger);
    liveryRepository = new InMemoryLiveryRepository(mockLogger);
    leagueRepository = new InMemoryLeagueRepository(mockLogger);
    leagueMembershipRepository = new InMemoryLeagueMembershipRepository(mockLogger);
    sponsorshipRequestRepository = new InMemorySponsorshipRequestRepository(mockLogger);
    sponsorRepository = new InMemorySponsorRepository(mockLogger);
    
    driverStatsUseCase = new DriverStatsUseCase(
      {} as any,
      {} as any,
      driverStatsRepository,
      mockLogger
    );
    
    rankingUseCase = new RankingUseCase(
      {} as any,
      {} as any,
      driverStatsRepository,
      mockLogger
    );

    getProfileOverviewUseCase = new GetProfileOverviewUseCase(
      driverRepository,
      teamRepository,
      teamMembershipRepository,
      socialRepository,
      driverExtendedProfileProvider,
      driverStatsUseCase,
      rankingUseCase
    );
    
    updateDriverProfileUseCase = new UpdateDriverProfileUseCase(driverRepository, mockLogger);
    getDriverLiveriesUseCase = new GetDriverLiveriesUseCase(liveryRepository, mockLogger);
    getLeagueMembershipsUseCase = new GetLeagueMembershipsUseCase(leagueMembershipRepository, driverRepository, leagueRepository);
    getPendingSponsorshipRequestsUseCase = new GetPendingSponsorshipRequestsUseCase(sponsorshipRequestRepository, sponsorRepository);
  });

  beforeEach(() => {
    driverRepository.clear();
    teamRepository.clear();
    teamMembershipRepository.clear();
    socialRepository.clear();
    driverExtendedProfileProvider.clear();
    driverStatsRepository.clear();
    liveryRepository.clear();
    leagueRepository.clear();
    leagueMembershipRepository.clear();
    sponsorshipRequestRepository.clear();
    sponsorRepository.clear();
  });

  describe('GetProfileOverviewUseCase', () => {
    it('should retrieve complete driver profile overview', async () => {
      // Given: A driver exists with stats, team, and friends
      const driverId = 'd1';
      const driver = Driver.create({ id: driverId, iracingId: '1', name: 'John Doe', country: 'US' });
      await driverRepository.create(driver);

      await driverStatsRepository.saveDriverStats(driverId, {
        rating: 2000,
        totalRaces: 10,
        wins: 2,
        podiums: 5,
        overallRank: 1,
        safetyRating: 4.5,
        sportsmanshipRating: 95,
        dnfs: 0,
        avgFinish: 3.5,
        bestFinish: 1,
        worstFinish: 10,
        consistency: 85,
        experienceLevel: 'pro'
      });

      const team = Team.create({ id: 't1', name: 'Team 1', tag: 'T1', description: 'Desc', ownerId: 'other', leagues: [] });
      await teamRepository.create(team);
      await teamMembershipRepository.saveMembership({
        teamId: 't1',
        driverId: driverId,
        role: 'driver',
        status: 'active',
        joinedAt: new Date()
      });

      socialRepository.seed({
        drivers: [driver, Driver.create({ id: 'f1', iracingId: '2', name: 'Friend 1', country: 'UK' })],
        friendships: [{ driverId: driverId, friendId: 'f1' }],
        feedEvents: []
      });

      // When: GetProfileOverviewUseCase.execute() is called
      const result = await getProfileOverviewUseCase.execute({ driverId });

      // Then: The result should contain all profile sections
      expect(result.isOk()).toBe(true);
      const overview = result.unwrap();
      expect(overview.driverInfo.driver.id).toBe(driverId);
      expect(overview.stats?.rating).toBe(2000);
      expect(overview.teamMemberships).toHaveLength(1);
      expect(overview.socialSummary.friendsCount).toBe(1);
    });
  });

  describe('UpdateDriverProfileUseCase', () => {
    it('should update driver bio and country', async () => {
      // Given: A driver exists
      const driverId = 'd2';
      const driver = Driver.create({ id: driverId, iracingId: '2', name: 'Update Driver', country: 'US' });
      await driverRepository.create(driver);

      // When: UpdateDriverProfileUseCase.execute() is called
      const result = await updateDriverProfileUseCase.execute({
        driverId,
        bio: 'New bio',
        country: 'DE',
      });

      // Then: The driver should be updated
      expect(result.isOk()).toBe(true);
      const updatedDriver = await driverRepository.findById(driverId);
      expect(updatedDriver?.bio?.toString()).toBe('New bio');
      expect(updatedDriver?.country.toString()).toBe('DE');
    });
  });

  describe('GetDriverLiveriesUseCase', () => {
    it('should retrieve driver liveries', async () => {
      // Given: A driver has liveries
      const driverId = 'd3';
      const livery = DriverLivery.create({
        id: 'l1',
        driverId,
        gameId: 'iracing',
        carId: 'porsche_911_gt3_r',
        uploadedImageUrl: 'https://example.com/livery.png'
      });
      await liveryRepository.createDriverLivery(livery);

      // When: GetDriverLiveriesUseCase.execute() is called
      const result = await getDriverLiveriesUseCase.execute({ driverId });

      // Then: It should return the liveries
      expect(result.isOk()).toBe(true);
      const liveries = result.unwrap();
      expect(liveries).toHaveLength(1);
      expect(liveries[0].id).toBe('l1');
    });
  });

  describe('GetLeagueMembershipsUseCase', () => {
    it('should retrieve league memberships for a league', async () => {
      // Given: A league with members
      const leagueId = 'lg1';
      const driverId = 'd4';
      const league = League.create({ id: leagueId, name: 'League 1', description: 'Desc', ownerId: 'owner' });
      await leagueRepository.create(league);
      
      const membership = LeagueMembership.create({
        id: 'm1',
        leagueId,
        driverId,
        role: 'member',
        status: 'active'
      });
      await leagueMembershipRepository.saveMembership(membership);
      
      const driver = Driver.create({ id: driverId, iracingId: '4', name: 'Member Driver', country: 'US' });
      await driverRepository.create(driver);

      // When: GetLeagueMembershipsUseCase.execute() is called
      const result = await getLeagueMembershipsUseCase.execute({ leagueId });

      // Then: It should return the memberships with driver info
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.memberships).toHaveLength(1);
      expect(data.memberships[0].driver?.id).toBe(driverId);
    });
  });

  describe('GetPendingSponsorshipRequestsUseCase', () => {
    it('should retrieve pending sponsorship requests for a driver', async () => {
      // Given: A driver has pending sponsorship requests
      const driverId = 'd5';
      const sponsorId = 's1';
      
      const sponsor = Sponsor.create({
        id: sponsorId,
        name: 'Sponsor 1',
        contactEmail: 'sponsor@example.com'
      });
      await sponsorRepository.create(sponsor);

      const request = SponsorshipRequest.create({
        id: 'sr1',
        sponsorId,
        entityType: 'driver',
        entityId: driverId,
        tier: 'main',
        offeredAmount: Money.create(1000, 'USD')
      });
      await sponsorshipRequestRepository.create(request);

      // When: GetPendingSponsorshipRequestsUseCase.execute() is called
      const result = await getPendingSponsorshipRequestsUseCase.execute({
        entityType: 'driver',
        entityId: driverId
      });

      // Then: It should return the pending requests
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.requests).toHaveLength(1);
      expect(data.requests[0].request.id).toBe('sr1');
      expect(data.requests[0].sponsor?.id.toString()).toBe(sponsorId);
    });
  });
});
