import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { JoinLeagueUseCase, type JoinLeagueResult, type JoinLeagueInput, type JoinLeagueErrorCode } from './JoinLeagueUseCase';
import { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { LeagueMembership } from '../../domain/entities/LeagueMembership';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('JoinLeagueUseCase', () => {
  let useCase: JoinLeagueUseCase;
  let membershipRepository: {
    getMembership: Mock;
    saveMembership: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };
  let output: UseCaseOutputPort<JoinLeagueResult> & { present: Mock };

  beforeEach(() => {
    membershipRepository = {
      getMembership: vi.fn(),
      saveMembership: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    output = {
      present: vi.fn(),
    } as unknown as UseCaseOutputPort<JoinLeagueResult> & { present: Mock };

    useCase = new JoinLeagueUseCase(
      membershipRepository as unknown as ILeagueMembershipRepository,
      logger as unknown as Logger,
      output,
    );
  });

  it('should join league successfully', async () => {
    const command: JoinLeagueInput = { leagueId: 'league-1', driverId: 'driver-1' };

    membershipRepository.getMembership.mockResolvedValue(null);
    membershipRepository.saveMembership.mockResolvedValue(
      LeagueMembership.create({
        id: 'membership-1',
        leagueId: 'league-1',
        driverId: 'driver-1',
        role: 'member',
        status: 'active',
        joinedAt: new Date(),
      }),
    );

    const result = await useCase.execute(command);

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

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = output.present.mock.calls[0]![0] as JoinLeagueResult;
    expect(presented.membership.id).toBe('membership-1');
    expect(presented.membership.leagueId.toString()).toBe('league-1');
    expect(presented.membership.driverId.toString()).toBe('driver-1');
    expect(presented.membership.role.toString()).toBe('member');
    expect(presented.membership.status.toString()).toBe('active');
  });

  it('should return error when already a member', async () => {
    const command: JoinLeagueInput = { leagueId: 'league-1', driverId: 'driver-1' };

    membershipRepository.getMembership.mockResolvedValue({
      id: 'membership-1',
      leagueId: 'league-1',
      driverId: 'driver-1',
      role: 'member',
      status: 'active',
      joinedAt: new Date(),
    });

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
    expect(err.code).toBe('ALREADY_MEMBER');
    expect(err.details?.message).toBe('Driver is already a member of this league or has a pending membership.');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should return error on repository failure', async () => {
    const command: JoinLeagueInput = { leagueId: 'league-1', driverId: 'driver-1' };
    const error = new Error('Repository error');

    membershipRepository.getMembership.mockRejectedValue(error);

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<JoinLeagueErrorCode, { message: string }>;
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details?.message).toBe('Repository error');
    expect(output.present).not.toHaveBeenCalled();
  });
});