import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import {
  RemoveLeagueMemberUseCase,
  type RemoveLeagueMemberInput,
  type RemoveLeagueMemberResult,
  type RemoveLeagueMemberErrorCode,
} from './RemoveLeagueMemberUseCase';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('RemoveLeagueMemberUseCase', () => {
  let useCase: RemoveLeagueMemberUseCase;
  let leagueMembershipRepository: { getMembership: Mock; getLeagueMembers: Mock; saveMembership: Mock };
  let output: UseCaseOutputPort<RemoveLeagueMemberResult> & { present: Mock };

  beforeEach(() => {
    leagueMembershipRepository = {
      getMembership: vi.fn(),
      getLeagueMembers: vi.fn(),
      saveMembership: vi.fn(),
    };
    output = {
      present: vi.fn(),
    } as unknown as UseCaseOutputPort<RemoveLeagueMemberResult> & { present: Mock };
    useCase = new RemoveLeagueMemberUseCase(
      leagueMembershipRepository as unknown as ILeagueMembershipRepository,
      output,
    );
  });

  it('should remove league member by setting status to inactive', async () => {
    const leagueId = 'league-1';
    const targetDriverId = 'driver-1';
    const membership = {
      id: `${leagueId}:${targetDriverId}`,
      leagueId: { toString: () => leagueId },
      driverId: { toString: () => targetDriverId },
      role: { toString: () => 'member' },
      status: { toString: () => 'active' },
      joinedAt: { toDate: () => new Date() },
    };

    leagueMembershipRepository.getMembership.mockResolvedValue(membership);

    const input: RemoveLeagueMemberInput = { leagueId, targetDriverId };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();

    expect(leagueMembershipRepository.saveMembership).toHaveBeenCalledTimes(1);
    const savedMembership = leagueMembershipRepository.saveMembership.mock.calls[0]?.[0];
    expect(savedMembership).toBeDefined();
    expect(savedMembership!.status.toString()).toBe('inactive');

    expect(output.present).toHaveBeenCalledTimes(1);
    expect(output.present).toHaveBeenCalledWith({
      leagueId,
      memberId: targetDriverId,
      removedRole: 'member',
    } satisfies RemoveLeagueMemberResult);
  });

  it('should return error if membership not found', async () => {
    leagueMembershipRepository.getMembership.mockResolvedValue(null);

    const input: RemoveLeagueMemberInput = { leagueId: 'league-1', targetDriverId: 'driver-1' };

    const result = await useCase.execute(input);

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

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

    expect(error.code).toBe('MEMBERSHIP_NOT_FOUND');
    expect(error.details.message).toBe('Membership not found for given league and driver');

    expect(output.present).not.toHaveBeenCalled();
  });

  it('should return repository error when an exception occurs', async () => {
    const leagueId = 'league-1';
    const targetDriverId = 'driver-1';

    leagueMembershipRepository.getMembership.mockRejectedValue(new Error('DB error'));

    const input: RemoveLeagueMemberInput = { leagueId, targetDriverId };

    const result = await useCase.execute(input);

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

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

    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB error');

    expect(output.present).not.toHaveBeenCalled();
  });

  it('prevents removing the last owner', async () => {
    const leagueId = 'league-1';
    const targetDriverId = 'owner-1';
    const membership = {
      id: `${leagueId}:${targetDriverId}`,
      leagueId: { toString: () => leagueId },
      driverId: { toString: () => targetDriverId },
      role: { toString: () => 'owner' },
      status: { toString: () => 'active' },
      joinedAt: { toDate: () => new Date() },
    };

    leagueMembershipRepository.getMembership.mockResolvedValue(membership);
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([membership]);

    const result = await useCase.execute({ leagueId, targetDriverId });

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

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

    expect(error.code).toBe('CANNOT_REMOVE_LAST_OWNER');
    expect(output.present).not.toHaveBeenCalled();
    expect(leagueMembershipRepository.saveMembership).not.toHaveBeenCalled();
  });
});