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

describe('GetLeagueAdminPermissionsUseCase', () => {
  let mockLeagueRepo: ILeagueRepository;
  let mockMembershipRepo: ILeagueMembershipRepository;
  let mockFindById: Mock;
  let mockGetMembership: Mock;

  beforeEach(() => {
    mockFindById = vi.fn();
    mockGetMembership = vi.fn();
    mockLeagueRepo = {
      findById: mockFindById,
      findAll: vi.fn(),
      create: vi.fn(),
      update: vi.fn(),
      delete: vi.fn(),
      exists: vi.fn(),
      findByOwnerId: vi.fn(),
      searchByName: vi.fn(),
    } as ILeagueRepository;
    mockMembershipRepo = {
      getMembership: mockGetMembership,
      getMembershipsForDriver: vi.fn(),
      saveMembership: vi.fn(),
      removeMembership: vi.fn(),
      getJoinRequests: vi.fn(),
      saveJoinRequest: vi.fn(),
      removeJoinRequest: vi.fn(),
      countByLeagueId: vi.fn(),
      getLeagueMembers: vi.fn(),
    } as ILeagueMembershipRepository;
  });

  const createUseCase = () => new GetLeagueAdminPermissionsUseCase(
    mockLeagueRepo,
    mockMembershipRepo,
  );

  const params = {
    leagueId: 'league1',
    performerDriverId: 'driver1',
  };

  it('should return no permissions when league not found', async () => {
    mockFindById.mockResolvedValue(null);

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
  });

  it('should return no permissions when membership not found', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue(null);

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
  });

  it('should return no permissions when membership not active', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue({ status: 'inactive', role: 'admin' });

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
  });

  it('should return no permissions when role is member', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue({ status: 'active', role: 'member' });

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: false, canUpdateRoles: false });
  });

  it('should return permissions when role is admin', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue({ status: 'active', role: 'admin' });

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: true, canUpdateRoles: true });
  });

  it('should return permissions when role is owner', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue({ status: 'active', role: 'owner' });

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({ canRemoveMember: true, canUpdateRoles: true });
  });
});