import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { describe, expect, it, vi } from 'vitest';
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
import { UpdateTeamUseCase, type UpdateTeamErrorCode, type UpdateTeamInput } from './UpdateTeamUseCase';

describe('UpdateTeamUseCase', () => {
  it('updates team successfully', async () => {
    const mockMembership = {
      role: 'owner',
    };

    const mockUpdatedTeam = { id: 'team-1' };

    const mockTeam = {
      id: 'team-1',
      update: vi.fn().mockReturnValue(mockUpdatedTeam),
    };

    const mockTeamRepository = {
      findById: vi.fn().mockResolvedValue(mockTeam),
      update: vi.fn().mockResolvedValue(undefined),
    } as unknown as TeamRepository;

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

    const useCase = new UpdateTeamUseCase(mockTeamRepository, mockMembershipRepository);

    const command: UpdateTeamInput = {
      teamId: 'team-1',
      updatedBy: 'user-1',
      updates: { name: 'New Name', tag: 'NEW' },
    };

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    const successResult = result.unwrap();
    expect(successResult.team).toBe(mockUpdatedTeam);
    expect(mockMembershipRepository.getMembership).toHaveBeenCalledWith('team-1', 'user-1');
    expect(mockTeamRepository.findById).toHaveBeenCalledWith('team-1');
    expect(mockTeam.update).toHaveBeenCalledWith({ name: 'New Name', tag: 'NEW' });
    expect(mockTeamRepository.update).toHaveBeenCalledWith(mockUpdatedTeam);
  });

  it('returns permission denied error if insufficient permissions', async () => {
    const mockMembership = {
      role: 'member',
    };

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

    const useCase = new UpdateTeamUseCase({} as unknown as TeamRepository, mockMembershipRepository);

    const command: UpdateTeamInput = {
      teamId: 'team-1',
      updatedBy: 'user-1',
      updates: { name: 'New Name' },
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<UpdateTeamErrorCode, { message: string }>;
    expect(error.code).toBe('PERMISSION_DENIED');
    expect(error.details?.message).toBe('User does not have permission to update this team');
  });

  it('returns error if team not found', async () => {
    const mockMembership = {
      role: 'owner',
    };

    const mockTeamRepository = {
      findById: vi.fn().mockResolvedValue(null),
    } as unknown as TeamRepository;

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

    const useCase = new UpdateTeamUseCase(mockTeamRepository, mockMembershipRepository);

    const command: UpdateTeamInput = {
      teamId: 'team-1',
      updatedBy: 'user-1',
      updates: { name: 'New Name' },
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<UpdateTeamErrorCode, { message: string }>;
    expect(error.code).toBe('TEAM_NOT_FOUND');
    expect(error.details?.message).toBe('Team not found');
  });

  it('returns repository error on unexpected failure', async () => {
    const mockMembership = {
      role: 'owner',
    };

    const mockTeamRepository = {
      findById: vi.fn().mockRejectedValue(new Error('db error')),
    } as unknown as TeamRepository;

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

    const useCase = new UpdateTeamUseCase(mockTeamRepository, mockMembershipRepository);

    const command: UpdateTeamInput = {
      teamId: 'team-1',
      updatedBy: 'user-1',
      updates: { name: 'New Name' },
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<UpdateTeamErrorCode, { message: string }>;
    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details?.message).toBe('db error');
  });
});
