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 { Media } from '../../domain/entities/Media';
import type { MediaStoragePort } from '../ports/MediaStoragePort';
import {
    DeleteMediaUseCase,
    type DeleteMediaErrorCode,
    type DeleteMediaInput,
} from './DeleteMediaUseCase';

describe('DeleteMediaUseCase', () => {
  let mediaRepo: {
    findById: Mock;
    delete: Mock;
  };
  let mediaStorage: {
    deleteMedia: Mock;
  };
  let logger: Logger;
  let useCase: DeleteMediaUseCase;

  beforeEach(() => {
    mediaRepo = {
      findById: vi.fn(),
      delete: vi.fn(),
    };

    mediaStorage = {
      deleteMedia: vi.fn(),
    };

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

    useCase = new DeleteMediaUseCase(
      mediaRepo as unknown as MediaRepository,
      mediaStorage as unknown as MediaStoragePort,
      logger,
    );
  });

  it('returns MEDIA_NOT_FOUND when media is not found', async () => {
    mediaRepo.findById.mockResolvedValue(null);

    const input: DeleteMediaInput = { mediaId: 'missing' };
    const result = await useCase.execute(input);

    expect(mediaRepo.findById).toHaveBeenCalledWith('missing');
    expect(result).toBeInstanceOf(Result);
    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      DeleteMediaErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('MEDIA_NOT_FOUND');
  });

  it('returns DeleteMediaResult on success', async () => {
    const media = Media.create({
      id: 'media-1',
      filename: 'file.png',
      originalName: 'file.png',
      mimeType: 'image/png',
      size: 123,
      url: 'https://example.com/file.png',
      type: 'image',
      uploadedBy: 'user-1',
    });

    mediaRepo.findById.mockResolvedValue(media);

    const input: DeleteMediaInput = { mediaId: 'media-1' };
    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(mediaRepo.findById).toHaveBeenCalledWith('media-1');
    expect(mediaStorage.deleteMedia).toHaveBeenCalledWith(media.url.value);
    expect(mediaRepo.delete).toHaveBeenCalledWith('media-1');

    const successResult = result.unwrap();
    expect(successResult).toEqual({
      mediaId: 'media-1',
      deleted: true,
    });
  });

  it('handles repository errors by returning REPOSITORY_ERROR', async () => {
    mediaRepo.findById.mockRejectedValue(new Error('DB error'));

    const input: DeleteMediaInput = { mediaId: 'media-1' };
    const result = await useCase.execute(input);

    expect((logger.error as unknown as Mock)).toHaveBeenCalled();
    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      DeleteMediaErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('REPOSITORY_ERROR');
  });
});