import { describe, it, expect, vi } from 'vitest';
import { TransferLeagueOwnershipUseCase } from './TransferLeagueOwnershipUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';

describe('TransferLeagueOwnershipUseCase', () => {
  it('transfers ownership successfully', async () => {
    const mockLeague = {
      id: 'league-1',
      ownerId: 'owner-1',
      update: vi.fn().mockReturnValue({}),
    };

    const mockNewOwnerMembership = {
      leagueId: 'league-1',
      driverId: 'owner-2',
      status: 'active',
      role: 'member',
    };

    const mockCurrentOwnerMembership = {
      leagueId: 'league-1',
      driverId: 'owner-1',
      status: 'active',
      role: 'owner',
    };

    const mockLeagueRepository = {
      findById: vi.fn().mockResolvedValue(mockLeague),
      update: vi.fn().mockResolvedValue(undefined),
    } as unknown as ILeagueRepository;

    const mockMembershipRepository = {
      getMembership: vi.fn()
        .mockResolvedValueOnce(mockNewOwnerMembership)
        .mockResolvedValueOnce(mockCurrentOwnerMembership),
      saveMembership: vi.fn().mockResolvedValue(undefined),
    } as unknown as ILeagueMembershipRepository;

    const useCase = new TransferLeagueOwnershipUseCase(
      mockLeagueRepository,
      mockMembershipRepository,
    );

    const command = {
      leagueId: 'league-1',
      currentOwnerId: 'owner-1',
      newOwnerId: 'owner-2',
    };

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    expect(mockLeagueRepository.findById).toHaveBeenCalledWith('league-1');
    expect(mockMembershipRepository.getMembership).toHaveBeenCalledWith('league-1', 'owner-2');
    expect(mockMembershipRepository.getMembership).toHaveBeenCalledWith('league-1', 'owner-1');
    expect(mockMembershipRepository.saveMembership).toHaveBeenCalledWith({
      ...mockNewOwnerMembership,
      role: 'owner',
    });
    expect(mockMembershipRepository.saveMembership).toHaveBeenCalledWith({
      ...mockCurrentOwnerMembership,
      role: 'admin',
    });
    expect(mockLeague.update).toHaveBeenCalledWith({ ownerId: 'owner-2' });
    expect(mockLeagueRepository.update).toHaveBeenCalledWith({});
  });

  it('returns error when league not found', async () => {
    const mockLeagueRepository = {
      findById: vi.fn().mockResolvedValue(null),
    } as unknown as ILeagueRepository;

    const mockMembershipRepository = {} as unknown as ILeagueMembershipRepository;

    const useCase = new TransferLeagueOwnershipUseCase(
      mockLeagueRepository,
      mockMembershipRepository,
    );

    const command = {
      leagueId: 'league-1',
      currentOwnerId: 'owner-1',
      newOwnerId: 'owner-2',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'LEAGUE_NOT_FOUND' });
  });

  it('returns error when not current owner', async () => {
    const mockLeague = {
      id: 'league-1',
      ownerId: 'owner-2',
    };

    const mockLeagueRepository = {
      findById: vi.fn().mockResolvedValue(mockLeague),
    } as unknown as ILeagueRepository;

    const mockMembershipRepository = {} as unknown as ILeagueMembershipRepository;

    const useCase = new TransferLeagueOwnershipUseCase(
      mockLeagueRepository,
      mockMembershipRepository,
    );

    const command = {
      leagueId: 'league-1',
      currentOwnerId: 'owner-1',
      newOwnerId: 'owner-2',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'NOT_CURRENT_OWNER' });
  });

  it('returns error when new owner is not active member', async () => {
    const mockLeague = {
      id: 'league-1',
      ownerId: 'owner-1',
    };

    const mockLeagueRepository = {
      findById: vi.fn().mockResolvedValue(mockLeague),
    } as unknown as ILeagueRepository;

    const mockMembershipRepository = {
      getMembership: vi.fn().mockResolvedValue(null),
    } as unknown as ILeagueMembershipRepository;

    const useCase = new TransferLeagueOwnershipUseCase(
      mockLeagueRepository,
      mockMembershipRepository,
    );

    const command = {
      leagueId: 'league-1',
      currentOwnerId: 'owner-1',
      newOwnerId: 'owner-2',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'NEW_OWNER_NOT_ACTIVE_MEMBER' });
  });
});