import type { Logger } from '@core/shared/domain/Logger';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
import { GetDriverTeamUseCase, type GetDriverTeamInput } from './GetDriverTeamUseCase';

describe('GetDriverTeamUseCase', () => {
  let mockTeamRepo: TeamRepository;
  let mockMembershipRepo: TeamMembershipRepository;
  let mockLogger: Logger;

  beforeEach(() => {
    mockTeamRepo = {
      findById: vi.fn(),
      findAll: vi.fn(),
      findByLeagueId: vi.fn(),
      create: vi.fn(),
      update: vi.fn(),
      delete: vi.fn(),
      exists: vi.fn(),
    } as unknown as TeamRepository;

    mockMembershipRepo = {
      getActiveMembershipForDriver: vi.fn(),
      getMembership: vi.fn(),
      getTeamMembers: vi.fn(),
      saveMembership: vi.fn(),
      removeMembership: vi.fn(),
      getJoinRequests: vi.fn(),
      countByTeamId: vi.fn(),
      saveJoinRequest: vi.fn(),
      removeJoinRequest: vi.fn(),
    } as unknown as TeamMembershipRepository;

    mockLogger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
  });

  it('should return driver team data when membership and team exist', async () => {
    const useCase = new GetDriverTeamUseCase(mockTeamRepo,
      mockMembershipRepo,
      mockLogger);

    const driverId = 'driver1';
    const membership = { id: 'membership1', driverId, teamId: 'team1' };
    const team = { id: 'team1', name: 'Team One' };

    mockMembershipRepo.getActiveMembershipForDriver.mockResolvedValue(membership);
    mockTeamRepo.findById.mockResolvedValue(team);

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

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.driverId).toBe(driverId);
    expect(presented.team).toBe(team);
    expect(presented.membership).toBe(membership);
  });

  it('should return error when no active membership found', async () => {
    const useCase = new GetDriverTeamUseCase(mockTeamRepo,
      mockMembershipRepo,
      mockLogger);

    const driverId = 'driver1';

    mockMembershipRepo.getActiveMembershipForDriver.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('MEMBERSHIP_NOT_FOUND');
    expect(result.unwrapErr().details.message).toBe('No active membership found for driver driver1');
  });

  it('should return error when team not found', async () => {
    const useCase = new GetDriverTeamUseCase(mockTeamRepo,
      mockMembershipRepo,
      mockLogger);

    const driverId = 'driver1';
    const membership = { id: 'membership1', driverId, teamId: 'team1' };

    mockMembershipRepo.getActiveMembershipForDriver.mockResolvedValue(membership);
    mockTeamRepo.findById.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('TEAM_NOT_FOUND');
    expect(result.unwrapErr().details.message).toBe('Team not found for teamId team1');
  });

  it('should return error when repository throws', async () => {
    const useCase = new GetDriverTeamUseCase(mockTeamRepo,
      mockMembershipRepo,
      mockLogger);

    const driverId = 'driver1';
    const error = new Error('Repository error');

    mockMembershipRepo.getActiveMembershipForDriver.mockRejectedValue(error);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
    expect(result.unwrapErr().details.message).toBe('Repository error');
  });
});
