import { SocialGraphRepository } from '@core/social/domain/repositories/SocialGraphRepository';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { Driver } from '../../domain/entities/Driver';
import { DriverRepository } from '../../domain/repositories/DriverRepository';
import { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import { TeamRepository } from '../../domain/repositories/TeamRepository';
import type { DriverExtendedProfileProvider } from '../ports/DriverExtendedProfileProvider';
import type { DriverStatsUseCase } from './DriverStatsUseCase';
import {
    GetProfileOverviewUseCase,
} from './GetProfileOverviewUseCase';
import type { RankingUseCase } from './RankingUseCase';

describe('GetProfileOverviewUseCase', () => {
  let useCase: GetProfileOverviewUseCase;
  let driverRepository: {
    findById: Mock;
  };
  let teamRepository: {
    findAll: Mock;
  };
  let teamMembershipRepository: {
    getMembership: Mock;
  };
  let socialRepository: {
    getFriends: Mock;
  };
  let driverStatsUseCase: {
    getDriverStats: Mock;
  };
  let rankingUseCase: {
    getAllDriverRankings: Mock;
  };
  let driverExtendedProfileProvider: {
    getExtendedProfile: Mock;
  };
  beforeEach(() => {
    driverRepository = {
      findById: vi.fn(),
    };

    teamRepository = {
      findAll: vi.fn(),
    };

    teamMembershipRepository = {
      getMembership: vi.fn(),
    };

    socialRepository = {
      getFriends: vi.fn(),
    };

    driverStatsUseCase = {
      getDriverStats: vi.fn(),
    };

    rankingUseCase = {
      getAllDriverRankings: vi.fn(),
    };

    driverExtendedProfileProvider = {
      getExtendedProfile: vi.fn(),
    };

    useCase = new GetProfileOverviewUseCase(driverRepository as unknown as DriverRepository,
      teamRepository as unknown as TeamRepository,
      teamMembershipRepository as unknown as TeamMembershipRepository,
      socialRepository as unknown as SocialGraphRepository,
      driverExtendedProfileProvider as unknown as DriverExtendedProfileProvider,
      driverStatsUseCase as unknown as DriverStatsUseCase,
      rankingUseCase as unknown as RankingUseCase);
  });

  it('should return profile overview for existing driver', async () => {
    const driverId = 'driver-1';
    const driver = Driver.create({
      id: driverId,
      iracingId: '12345',
      name: 'Test Driver',
      country: 'US',
      joinedAt: new Date('2023-01-01'),
    });

    driverRepository.findById.mockResolvedValue(driver);
    driverStatsUseCase.getDriverStats.mockResolvedValue({
      rating: 1500,
      wins: 5,
      podiums: 10,
      dnfs: 2,
      totalRaces: 20,
      avgFinish: 8.5,
      bestFinish: 1,
      worstFinish: 15,
      overallRank: 50,
      consistency: 85,
    });
    rankingUseCase.getAllDriverRankings.mockResolvedValue([
      { driverId: 'driver-1', rating: 1500, wins: 5, totalRaces: 20, overallRank: 50 },
      { driverId: 'driver-2', rating: 1400, wins: 3, totalRaces: 18, overallRank: 75 },
    ]);
    teamRepository.findAll.mockResolvedValue([]);
    socialRepository.getFriends.mockResolvedValue([]);
    driverExtendedProfileProvider.getExtendedProfile.mockReturnValue({
      bio: 'Test bio',
      location: 'Test location',
      favoriteTrack: 'Test track',
    });

    const result = await useCase.execute({ driverId });

    expect(result.isOk()).toBe(true);
    });
});
