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 { AvatarGenerationRequest } from '../../domain/entities/AvatarGenerationRequest';
import {
    SelectAvatarUseCase,
    type SelectAvatarErrorCode,
    type SelectAvatarInput,
} from './SelectAvatarUseCase';

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

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

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

    useCase = new SelectAvatarUseCase(
      avatarRepo as unknown as AvatarGenerationRepository,
      logger,
    );
  });

  it('returns REQUEST_NOT_FOUND when request does not exist', async () => {
    avatarRepo.findById.mockResolvedValue(null);

    const input: SelectAvatarInput = { requestId: 'missing', selectedIndex: 0 };
    const result = await useCase.execute(input);

    expect(avatarRepo.findById).toHaveBeenCalledWith('missing');
    expect(result).toBeInstanceOf(Result);
    expect(result.isErr()).toBe(true);

    const err = result.unwrapErr() as ApplicationErrorCode<SelectAvatarErrorCode, { message: string }>;
    expect(err.code).toBe('REQUEST_NOT_FOUND');
  });

  it('returns REQUEST_NOT_COMPLETED when request is not completed', async () => {
    const request = AvatarGenerationRequest.create({
      id: 'req-1',
      userId: 'user-1',
      facePhotoUrl: 'data:image/png;base64,abc',
      suitColor: 'red' as unknown as Parameters<typeof AvatarGenerationRequest.create>[0]['suitColor'],
    });

    avatarRepo.findById.mockResolvedValue(request);

    const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 0 };
    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<SelectAvatarErrorCode, { message: string }>;
    expect(err.code).toBe('REQUEST_NOT_COMPLETED');

    expect(avatarRepo.save).not.toHaveBeenCalled();
  });

  it('returns SelectAvatarResult when request is completed', async () => {
    const request = AvatarGenerationRequest.create({
      id: 'req-1',
      userId: 'user-1',
      facePhotoUrl: 'data:image/png;base64,abc',
      suitColor: 'red' as unknown as Parameters<typeof AvatarGenerationRequest.create>[0]['suitColor'],
    });
    request.completeWithAvatars(['https://example.com/a.png', 'https://example.com/b.png']);

    avatarRepo.findById.mockResolvedValue(request);

    const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 1 };
    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(request.selectedAvatarIndex).toBe(1);
    expect(request.selectedAvatarUrl).toBe('https://example.com/b.png');

    expect(avatarRepo.save).toHaveBeenCalledWith(request);

    const successResult = result.unwrap();
    expect(successResult).toEqual({
      requestId: 'req-1',
      selectedAvatarUrl: 'https://example.com/b.png',
    });
  });

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

    const input: SelectAvatarInput = { requestId: 'req-1', selectedIndex: 0 };
    const result = await useCase.execute(input);

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

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