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

describe('CreateTeamUseCase', () => {
  let useCase: CreateTeamUseCase;
  let teamRepository: {
    create: Mock;
  };
  let membershipRepository: {
    getActiveMembershipForDriver: Mock;
    saveMembership: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    teamRepository = {
      create: vi.fn(),
    };
    membershipRepository = {
      getActiveMembershipForDriver: vi.fn(),
      saveMembership: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new CreateTeamUseCase(teamRepository as unknown as TeamRepository,
      membershipRepository as unknown as TeamMembershipRepository,
      logger as unknown as Logger);
  });

  it('should create team successfully', async () => {
    const command: CreateTeamInput = {
      name: 'Test Team',
      tag: 'TT',
      description: 'A test team',
      ownerId: 'owner-123',
      leagues: ['league-1'],
    };

    membershipRepository.getActiveMembershipForDriver.mockResolvedValue(null);
    const mockTeam = {
      id: 'team-uuid',
      name: 'Test Team',
      tag: 'TT',
      description: 'A test team',
      ownerId: 'owner-123',
      leagues: ['league-1'],
      createdAt: new Date(),
    };
    teamRepository.create.mockResolvedValue(mockTeam);
    membershipRepository.saveMembership.mockResolvedValue(undefined);

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.team.id).toBe('team-uuid');
    expect(teamRepository.create).toHaveBeenCalledTimes(1);
    expect(membershipRepository.saveMembership).toHaveBeenCalledTimes(1);
  });

  it('should return error when driver already belongs to a team', async () => {
    const command: CreateTeamInput = {
      name: 'Test Team',
      tag: 'TT',
      description: 'A test team',
      ownerId: 'owner-123',
      leagues: ['league-1'],
    };

    membershipRepository.getActiveMembershipForDriver.mockResolvedValue({
      teamId: 'existing-team',
      driverId: 'owner-123',
      role: 'member',
      status: 'active',
      joinedAt: new Date(),
    });

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
    expect(result.unwrapErr().details?.message).toBe(
      'Driver already belongs to a team',
    );
    expect(teamRepository.create).not.toHaveBeenCalled();
    expect(membershipRepository.saveMembership).not.toHaveBeenCalled();
  });

  it('should return error when repository throws', async () => {
    const command: CreateTeamInput = {
      name: 'Test Team',
      tag: 'TT',
      description: 'A test team',
      ownerId: 'owner-123',
      leagues: ['league-1'],
    };

    membershipRepository.getActiveMembershipForDriver.mockResolvedValue(null);
    teamRepository.create.mockRejectedValue(new Error('DB error'));

    const result = await useCase.execute(command);

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