import type { Logger } from '@core/shared/domain/Logger';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { League } from '../../domain/entities/League';
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import {
    GetLeagueAdminPermissionsUseCase,
    type GetLeagueAdminPermissionsErrorCode,
    type GetLeagueAdminPermissionsInput
} from './GetLeagueAdminPermissionsUseCase';

describe('GetLeagueAdminPermissionsUseCase', () => {
  let mockLeagueRepo: LeagueRepository;
  let mockMembershipRepo: LeagueMembershipRepository;
  let mockFindById: Mock;
  let mockGetMembership: Mock;
  const logger: Logger = {
    debug: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  };

  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 unknown as LeagueRepository;

    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 unknown as LeagueMembershipRepository;
  });

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

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

  it('returns LEAGUE_NOT_FOUND when league does not exist', async () => {
    mockFindById.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
    expect(err.code).toBe('LEAGUE_NOT_FOUND');
    expect(err.details.message).toBe('League not found');
  });

  it('returns USER_NOT_MEMBER when membership is missing', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
    expect(err.code).toBe('USER_NOT_MEMBER');
    expect(err.details.message).toBe('User is not a member of this league');
  });

  it('returns USER_NOT_MEMBER when membership is not active', async () => {
    mockFindById.mockResolvedValue({ id: 'league1' });
    mockGetMembership.mockResolvedValue({ status: 'inactive', role: 'admin' });

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

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
    expect(err.code).toBe('USER_NOT_MEMBER');
    expect(err.details.message).toBe('User is not a member of this league');
  });

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

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

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
    expect(err.code).toBe('USER_NOT_MEMBER');
    expect(err.details.message).toBe('User is not a member of this league');
  });

  it('returns admin permissions for admin role', async () => {
    const league = { id: 'league1' } as unknown as League;
    mockFindById.mockResolvedValue(league);
    mockGetMembership.mockResolvedValue({ status: 'active', role: 'admin' });

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

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();

    expect(presented.league).toBe(league);
    expect(presented.permissions).toEqual({
      canManageSchedule: true,
      canManageMembers: true,
      canManageSponsorships: true,
      canManageScoring: true,
    });
  });

  it('returns admin permissions for owner role', async () => {
    const league = { id: 'league1' } as unknown as League;
    mockFindById.mockResolvedValue(league);
    mockGetMembership.mockResolvedValue({ status: 'active', role: 'owner' });

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

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();

    expect(presented.league).toBe(league);
    expect(presented.permissions).toEqual({
      canManageSchedule: true,
      canManageMembers: true,
      canManageSponsorships: true,
      canManageScoring: true,
    });
  });

  it('wraps repository errors in REPOSITORY_ERROR', async () => {
    const error = new Error('repo failed');
    mockFindById.mockRejectedValue(error);

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

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>;
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('repo failed');
  });
});
