import { describe, it, expect, vi, type Mock } from 'vitest';
import {
  UpdateLeagueMemberRoleUseCase,
  type UpdateLeagueMemberRoleInput,
  type UpdateLeagueMemberRoleResult,
  type UpdateLeagueMemberRoleErrorCode,
} from './UpdateLeagueMemberRoleUseCase';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('UpdateLeagueMemberRoleUseCase', () => {
  it('updates league member role successfully', async () => {
    const mockMembership = {
      id: 'league-1:driver-1',
      leagueId: { toString: () => 'league-1' },
      driverId: { toString: () => 'driver-1' },
      role: { toString: () => 'member' },
      status: { toString: () => 'active' },
      joinedAt: { toDate: () => new Date() },
    };

    const mockLeagueMembershipRepository = {
      getLeagueMembers: vi.fn().mockResolvedValue([mockMembership]),
      saveMembership: vi.fn().mockResolvedValue(undefined),
    } as unknown as ILeagueMembershipRepository;

    const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
      present: vi.fn(),
    };

    const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);

    const input: UpdateLeagueMemberRoleInput = {
      leagueId: 'league-1',
      targetDriverId: 'driver-1',
      newRole: 'admin',
    };

    const result = await useCase.execute(input);

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

    expect(mockLeagueMembershipRepository.getLeagueMembers).toHaveBeenCalledWith('league-1');
    expect(mockLeagueMembershipRepository.saveMembership).toHaveBeenCalledTimes(1);

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = (output.present as Mock).mock.calls[0]![0] as UpdateLeagueMemberRoleResult;

    expect(presented.membership.id).toBe('league-1:driver-1');
    expect(presented.membership.leagueId.toString()).toBe('league-1');
    expect(presented.membership.driverId.toString()).toBe('driver-1');
    expect(presented.membership.role.toString()).toBe('admin');
  });

  it('returns error if membership not found', async () => {
    const mockLeagueMembershipRepository = {
      getLeagueMembers: vi.fn().mockResolvedValue([]),
    } as unknown as ILeagueMembershipRepository;

    const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
      present: vi.fn(),
    };

    const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);

    const input: UpdateLeagueMemberRoleInput = {
      leagueId: 'league-1',
      targetDriverId: 'driver-1',
      newRole: 'admin',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      UpdateLeagueMemberRoleErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('MEMBERSHIP_NOT_FOUND');
    expect(error.details.message).toBe('League membership not found');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('handles repository errors', async () => {
    const mockLeagueMembershipRepository = {
      getLeagueMembers: vi
        .fn()
        .mockRejectedValue(new Error('Database connection failed')),
    } as unknown as ILeagueMembershipRepository;

    const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
      present: vi.fn(),
    };

    const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);

    const input: UpdateLeagueMemberRoleInput = {
      leagueId: 'league-1',
      targetDriverId: 'driver-1',
      newRole: 'admin',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      UpdateLeagueMemberRoleErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('Database connection failed');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('rejects invalid roles', async () => {
    const mockMembership = {
      id: 'league-1:driver-1',
      leagueId: { toString: () => 'league-1' },
      driverId: { toString: () => 'driver-1' },
      role: { toString: () => 'member' },
      status: { toString: () => 'active' },
      joinedAt: { toDate: () => new Date() },
    };

    const mockLeagueMembershipRepository = {
      getLeagueMembers: vi.fn().mockResolvedValue([mockMembership]),
      saveMembership: vi.fn().mockResolvedValue(undefined),
    } as unknown as ILeagueMembershipRepository;

    const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
      present: vi.fn(),
    };

    const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);

    const result = await useCase.execute({
      leagueId: 'league-1',
      targetDriverId: 'driver-1',
      newRole: 'manager',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      UpdateLeagueMemberRoleErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('INVALID_ROLE');
    expect(output.present).not.toHaveBeenCalled();
    expect(mockLeagueMembershipRepository.saveMembership).not.toHaveBeenCalled();
  });

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

    const mockLeagueMembershipRepository = {
      getLeagueMembers: vi.fn().mockResolvedValue([mockOwnerMembership]),
      saveMembership: vi.fn().mockResolvedValue(undefined),
    } as unknown as ILeagueMembershipRepository;

    const output: UseCaseOutputPort<UpdateLeagueMemberRoleResult> & { present: Mock } = {
      present: vi.fn(),
    };

    const useCase = new UpdateLeagueMemberRoleUseCase(mockLeagueMembershipRepository, output);

    const result = await useCase.execute({
      leagueId: 'league-1',
      targetDriverId: 'owner-1',
      newRole: 'admin',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      UpdateLeagueMemberRoleErrorCode,
      { message: string }
    >;

    expect(error.code).toBe('CANNOT_DOWNGRADE_LAST_OWNER');
    expect(output.present).not.toHaveBeenCalled();
    expect(mockLeagueMembershipRepository.saveMembership).not.toHaveBeenCalled();
  });
});