import type { Logger } from '@core/shared/domain/Logger';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { Driver } from '../../domain/entities/Driver';
import { Team } from '../../domain/entities/Team';
import type { DriverRepository } from '../../domain/repositories/DriverRepository';
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
import {
    GetTeamMembersUseCase,
    type GetTeamMembersErrorCode,
    type GetTeamMembersInput
} from './GetTeamMembersUseCase';

describe('GetTeamMembersUseCase', () => {
  let useCase: GetTeamMembersUseCase;
  let membershipRepository: {
    getTeamMembers: Mock;
  };
  let driverRepository: {
    findById: Mock;
  };
  let teamRepository: {
    findById: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };
  beforeEach(() => {
    membershipRepository = {
      getTeamMembers: vi.fn(),
    };
    driverRepository = {
      findById: vi.fn(),
    };
    teamRepository = {
      findById: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new GetTeamMembersUseCase(
      membershipRepository as unknown as TeamMembershipRepository,
      driverRepository as unknown as DriverRepository,
      teamRepository as unknown as TeamRepository,
      logger as unknown as Logger
    );
  });

  it('should return team members with driver entities', async () => {
    const team = Team.create({
      id: 'team-1',
      name: 'Team Name',
      tag: 'TAG',
      description: 'Description',
      ownerId: 'driver-1',
      leagues: [],
    });

    const input: GetTeamMembersInput = { teamId: team.id };

    const memberships = [
      { teamId: team.id, driverId: 'driver-1', role: 'owner' as const, status: 'active' as const, joinedAt: new Date() },
      { teamId: team.id, driverId: 'driver-2', role: 'driver' as const, status: 'active' as const, joinedAt: new Date() },
    ];

    const driver1 = Driver.create({
      id: 'driver-1',
      iracingId: '123',
      name: 'Driver 1',
      country: 'US',
    });

    const driver2 = Driver.create({
      id: 'driver-2',
      iracingId: '456',
      name: 'Driver 2',
      country: 'UK',
    });

    teamRepository.findById.mockResolvedValue(team);
    membershipRepository.getTeamMembers.mockResolvedValue(memberships);
    driverRepository.findById.mockImplementation((id: string) => {
      if (id === 'driver-1') return Promise.resolve(driver1);
      if (id === 'driver-2') return Promise.resolve(driver2);
      return Promise.resolve(null);
    });

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    const resultValue = result.unwrap();
    expect(resultValue.team).toBe(team);
    expect(resultValue.members).toHaveLength(2);
    expect(resultValue.members[0]).toEqual({ membership: memberships[0], driver: driver1 });
    expect(resultValue.members[1]).toEqual({ membership: memberships[1], driver: driver2 });
  });

  it('should handle driver not found', async () => {
    const team = Team.create({
      id: 'team-1',
      name: 'Team Name',
      tag: 'TAG',
      description: 'Description',
      ownerId: 'driver-1',
      leagues: [],
    });

    const input: GetTeamMembersInput = { teamId: team.id };

    const memberships = [
      { teamId: team.id, driverId: 'driver-1', role: 'owner' as const, status: 'active' as const, joinedAt: new Date() },
    ];

    teamRepository.findById.mockResolvedValue(team);
    membershipRepository.getTeamMembers.mockResolvedValue(memberships);
    driverRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    const resultValue = result.unwrap();
    expect(resultValue.team).toBe(team);
    expect(resultValue.members).toEqual([
      { membership: memberships[0], driver: null },
    ]);
  });

  it('should return TEAM_NOT_FOUND when team does not exist', async () => {
    const input: GetTeamMembersInput = { teamId: 'missing-team' };

    teamRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);

    const error = result.unwrapErr() as ApplicationErrorCode<
      GetTeamMembersErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('TEAM_NOT_FOUND');
    expect(error.details.message).toBe('Team not found');
    });

  it('should return REPOSITORY_ERROR when repository throws', async () => {
    const team = Team.create({
      id: 'team-1',
      name: 'Team Name',
      tag: 'TAG',
      description: 'Description',
      ownerId: 'driver-1',
      leagues: [],
    });

    const input: GetTeamMembersInput = { teamId: team.id };

    const error = new Error('Repository failure');

    teamRepository.findById.mockResolvedValue(team);
    membershipRepository.getTeamMembers.mockRejectedValue(error);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);

    const err = result.unwrapErr() as ApplicationErrorCode<
      GetTeamMembersErrorCode,
      { message: string }
    >;

    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('Repository failure');
    });
});
