import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { describe, expect, it, vi, type Mock } from 'vitest';
import { Avatar } from '../../domain/entities/Avatar';
import {
    UpdateAvatarUseCase,
    type UpdateAvatarErrorCode,
    type UpdateAvatarInput,
    type UpdateAvatarResult,
} from './UpdateAvatarUseCase';

vi.mock('uuid', () => ({
  v4: () => 'avatar-1',
}));

describe('UpdateAvatarUseCase', () => {
  let avatarRepo: { findActiveByDriverId: Mock; save: Mock };
  let logger: Logger;
  let useCase: UpdateAvatarUseCase;

  beforeEach(() => {
    avatarRepo = {
      findActiveByDriverId: vi.fn(),
      save: vi.fn(),
    };

    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    } as unknown as Logger;

    useCase = new UpdateAvatarUseCase(
      avatarRepo as unknown as AvatarRepository,
      logger,
    );
  });

  it('creates new avatar when no current active avatar exists', async () => {
    avatarRepo.findActiveByDriverId.mockResolvedValue(null);

    const input: UpdateAvatarInput = {
      driverId: 'driver-1',
      mediaUrl: 'https://example.com/avatar.png',
    };

    const result = await useCase.execute(input);

    expect(result).toBeInstanceOf(Result);
    expect(result.isOk()).toBe(true);

    expect(avatarRepo.findActiveByDriverId).toHaveBeenCalledWith('driver-1');
    expect(avatarRepo.save).toHaveBeenCalledTimes(1);

    const saved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
    expect(saved.driverId).toBe('driver-1');
    expect(saved.mediaUrl.value).toBe('https://example.com/avatar.png');
    expect(saved.isActive).toBe(true);

    const successResult = result.unwrap();
    expect(successResult).toEqual({ avatarId: 'avatar-1', driverId: 'driver-1' });
  });

  it('deactivates current avatar before saving new avatar', async () => {
    const currentAvatar = Avatar.reconstitute({
      id: 'old-avatar',
      driverId: 'driver-1',
      mediaUrl: 'https://example.com/old.png',
      selectedAt: new Date('2020-01-01T00:00:00.000Z'),
      isActive: true,
    });

    avatarRepo.findActiveByDriverId.mockResolvedValue(currentAvatar);

    const input: UpdateAvatarInput = {
      driverId: 'driver-1',
      mediaUrl: 'https://example.com/new.png',
    };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(avatarRepo.save).toHaveBeenCalledTimes(2);

    const firstSaved = (avatarRepo.save as unknown as Mock).mock.calls[0]![0] as Avatar;
    expect(firstSaved.id).toBe('old-avatar');
    expect(firstSaved.isActive).toBe(false);

    const secondSaved = (avatarRepo.save as unknown as Mock).mock.calls[1]![0] as Avatar;
    expect(secondSaved.id).toBe('avatar-1');
    expect(secondSaved.isActive).toBe(true);

    const successResult = result.unwrap();
    expect(successResult).toEqual({ avatarId: 'avatar-1', driverId: 'driver-1' });
  });

  it('returns REPOSITORY_ERROR when repository throws', async () => {
    avatarRepo.findActiveByDriverId.mockRejectedValue(new Error('DB error'));

    const input: UpdateAvatarInput = {
      driverId: 'driver-1',
      mediaUrl: 'https://example.com/avatar.png',
    };

    const result: Result<UpdateAvatarResult, ApplicationErrorCode<UpdateAvatarErrorCode, { message: string }>> =
      await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr();
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details?.message).toBe('DB error');

    expect((logger.error as unknown as Mock)).toHaveBeenCalled();
  });
});