import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GetDriverTeamUseCase } from './GetDriverTeamUseCase';
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { Logger } from '@core/shared/application';

describe('GetDriverTeamUseCase', () => {
  const mockFindById = vi.fn();
  const mockGetActiveMembershipForDriver = vi.fn();
  const mockTeamRepo: ITeamRepository = {
    findById: mockFindById,
    findAll: vi.fn(),
    findByLeagueId: vi.fn(),
    create: vi.fn(),
    update: vi.fn(),
    delete: vi.fn(),
    exists: vi.fn(),
  };

  const mockMembershipRepo: ITeamMembershipRepository = {
    getActiveMembershipForDriver: mockGetActiveMembershipForDriver,
    getMembership: vi.fn(),
    getTeamMembers: vi.fn(),
    saveMembership: vi.fn(),
    removeMembership: vi.fn(),
    getJoinRequests: vi.fn(),
    countByTeamId: vi.fn(),
    saveJoinRequest: vi.fn(),
    removeJoinRequest: vi.fn(),
  };

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

  beforeEach(() => {
    vi.clearAllMocks();
  });

  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' };

    mockGetActiveMembershipForDriver.mockResolvedValue(membership);
    mockFindById.mockResolvedValue(team);

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

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      team,
      membership,
      driverId,
    });
  });

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

    const driverId = 'driver1';

    mockGetActiveMembershipForDriver.mockResolvedValue(null);

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

    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' };

    mockGetActiveMembershipForDriver.mockResolvedValue(membership);
    mockFindById.mockResolvedValue(null);

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

    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');

    mockGetActiveMembershipForDriver.mockRejectedValue(error);

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

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