import { Result } from '@core/shared/domain/Result';
import { describe, expect, it, vi } from 'vitest';
import { MediaService } from './MediaService';

describe('MediaService', () => {
  const logger = { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() };

  it('requestAvatarGeneration returns presenter response on success', async () => {
    const requestAvatarGenerationPresenter = {
      transform: vi.fn((result) => ({ success: true, requestId: result.requestId, avatarUrls: result.avatarUrls, errorMessage: '' })),
      responseModel: { success: true, requestId: 'r1', avatarUrls: ['u1'], errorMessage: '' },
    };

    const requestAvatarGenerationUseCase = {
      execute: vi.fn(async () => Result.ok({ requestId: 'r1', status: 'completed', avatarUrls: ['u1'] })),
    };

    const service = new MediaService(
      requestAvatarGenerationUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      requestAvatarGenerationPresenter as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(
      service.requestAvatarGeneration({ userId: 'u1', facePhotoData: {} as never, suitColor: 'red' as never }),
    ).resolves.toEqual({ success: true, requestId: 'r1', avatarUrls: ['u1'], errorMessage: '' });

    expect(requestAvatarGenerationUseCase.execute).toHaveBeenCalledWith({
      userId: 'u1',
      facePhotoData: {} as never,
      suitColor: 'red',
    });
    expect(requestAvatarGenerationPresenter.transform).toHaveBeenCalledWith({ requestId: 'r1', status: 'completed', avatarUrls: ['u1'] });
  });

  it('requestAvatarGeneration returns failure DTO on error', async () => {
    const service = new MediaService(
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'fail' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(
      service.requestAvatarGeneration({ userId: 'u1', facePhotoData: {} as never, suitColor: 'red' as never }),
    ).resolves.toEqual({
      success: false,
      requestId: '',
      avatarUrls: [],
      errorMessage: 'fail',
    });
  });

  it('uploadMedia returns presenter response on success', async () => {
    const uploadMediaPresenter = { 
      transform: vi.fn((result) => ({ success: true, mediaId: result.mediaId })),
      responseModel: { success: true, mediaId: 'm1' } 
    };
    const uploadMediaUseCase = { execute: vi.fn(async () => Result.ok({ mediaId: 'm1', url: 'https://example.com/m1.png' })) };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      uploadMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      uploadMediaPresenter as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(
      service.uploadMedia({ file: {} as never, userId: 'u1', metadata: { a: 1 } } as never),
    ).resolves.toEqual({
      success: true,
      mediaId: 'm1',
    });

    expect(uploadMediaUseCase.execute).toHaveBeenCalledWith({
      file: {} as never,
      uploadedBy: 'u1',
      metadata: { a: 1 },
    });
    expect(uploadMediaPresenter.transform).toHaveBeenCalledWith({ mediaId: 'm1', url: 'https://example.com/m1.png' });
  });

  it('uploadMedia uses empty uploadedBy when userId missing', async () => {
    const uploadMediaUseCase = { execute: vi.fn(async () => Result.ok({ mediaId: 'm1', url: 'https://example.com/m1.png' })) };
    const uploadMediaPresenter = { 
      transform: vi.fn((result) => ({ success: true, mediaId: result.mediaId })),
      responseModel: { success: true, mediaId: 'm1' } 
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      uploadMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      uploadMediaPresenter as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.uploadMedia({ file: {} as never } as never)).resolves.toEqual({ success: true, mediaId: 'm1' });
    expect(uploadMediaUseCase.execute).toHaveBeenCalledWith({ file: {} as never, uploadedBy: '', metadata: {} });
    expect(uploadMediaPresenter.transform).toHaveBeenCalledWith({ mediaId: 'm1', url: 'https://example.com/m1.png' });
  });

  it('uploadMedia returns failure DTO on error', async () => {
    const uploadMediaUseCase = {
      execute: vi.fn(async () => Result.err({ code: 'UPLOAD_FAILED', details: { message: 'nope' } })),
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      uploadMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.uploadMedia({ file: {} as never, userId: 'u1' } as never)).resolves.toEqual({
      success: false,
      error: 'nope',
    });
  });

  it('getMedia returns presenter response on success', async () => {
    const uploadedAt = new Date();
    const getMediaUseCase = { execute: vi.fn(async () => Result.ok({ media: { id: 'm1', filename: 'test.png', originalName: 'test.png', mimeType: 'image/png', size: 100, url: 'https://example.com/m1.png', type: 'image', uploadedBy: 'u1', uploadedAt } })) };
    const getMediaPresenter = { 
      transform: vi.fn((result) => ({ id: result.media.id, url: result.media.url, type: result.media.type, uploadedAt: result.media.uploadedAt, size: result.media.size })),
      responseModel: { id: 'm1', url: 'https://example.com/m1.png', type: 'image', uploadedAt, size: 100 } 
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      getMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      getMediaPresenter as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    const result = await service.getMedia('m1');
    expect(result).toEqual({ id: 'm1', url: 'https://example.com/m1.png', type: 'image', uploadedAt, size: 100 });
    expect(getMediaUseCase.execute).toHaveBeenCalledWith({ mediaId: 'm1' });
    expect(getMediaPresenter.transform).toHaveBeenCalledWith({ media: { id: 'm1', filename: 'test.png', originalName: 'test.png', mimeType: 'image/png', size: 100, url: 'https://example.com/m1.png', type: 'image', uploadedBy: 'u1', uploadedAt } });
  });

  it('getMedia returns null on MEDIA_NOT_FOUND', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'MEDIA_NOT_FOUND', details: { message: 'n/a' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getMedia('m1')).resolves.toBeNull();
  });

  it('getMedia throws on non-not-found error', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'boom' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getMedia('m1')).rejects.toThrow('boom');
  });

  it('deleteMedia returns presenter response on success', async () => {
    const deleteMediaUseCase = { execute: vi.fn(async () => Result.ok({ mediaId: 'm1', deleted: true })) };
    const deleteMediaPresenter = { 
      transform: vi.fn((result) => ({ success: result.deleted })),
      responseModel: { success: true } 
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      deleteMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      deleteMediaPresenter as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.deleteMedia('m1')).resolves.toEqual({ success: true });
    expect(deleteMediaUseCase.execute).toHaveBeenCalledWith({ mediaId: 'm1' });
    expect(deleteMediaPresenter.transform).toHaveBeenCalledWith({ mediaId: 'm1', deleted: true });
  });

  it('deleteMedia returns failure DTO on error', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'nope' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.deleteMedia('m1')).resolves.toEqual({ success: false, error: 'nope' });
  });

  it('getAvatar returns presenter response on success', async () => {
    const selectedAt = new Date();
    const getAvatarUseCase = { execute: vi.fn(async () => Result.ok({ avatar: { id: 'a1', driverId: 'd1', mediaUrl: 'https://example.com/avatar.png', selectedAt } })) };
    const getAvatarPresenter = { 
      transform: vi.fn((result) => ({ avatarUrl: result.avatar.mediaUrl })),
      responseModel: { avatarUrl: 'https://example.com/avatar.png' } 
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      getAvatarUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      getAvatarPresenter as never,
      { responseModel: {} } as never,
    );

    await expect(service.getAvatar('d1')).resolves.toEqual({ avatarUrl: 'https://example.com/avatar.png' });
    expect(getAvatarUseCase.execute).toHaveBeenCalledWith({ driverId: 'd1' });
    expect(getAvatarPresenter.transform).toHaveBeenCalledWith({ avatar: { id: 'a1', driverId: 'd1', mediaUrl: 'https://example.com/avatar.png', selectedAt } });
  });

  it('getAvatar returns null on AVATAR_NOT_FOUND', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'AVATAR_NOT_FOUND', details: { message: 'n/a' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getAvatar('d1')).resolves.toBeNull();
  });

  it('getAvatar throws on non-not-found error', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'boom' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getAvatar('d1')).rejects.toThrow('boom');
  });

  it('updateAvatar returns presenter response on success', async () => {
    const updateAvatarUseCase = { execute: vi.fn(async () => Result.ok({ avatarId: 'a1', driverId: 'd1' })) };
    const updateAvatarPresenter = {
      transform: vi.fn(() => ({ success: true })),
      responseModel: { success: true }
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      updateAvatarUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      updateAvatarPresenter as never,
    );

    await expect(service.updateAvatar('d1', { avatarUrl: 'u1' } as never)).resolves.toEqual({ success: true });
    expect(updateAvatarUseCase.execute).toHaveBeenCalledWith({ driverId: 'd1', mediaUrl: 'u1' });
    expect(updateAvatarPresenter.transform).toHaveBeenCalledWith({ avatarId: 'a1', driverId: 'd1' });
  });

  it('updateAvatar returns failure DTO on error', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'nope' } })) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
    );

    await expect(service.updateAvatar('d1', { avatarUrl: 'u' } as never)).resolves.toEqual({
      success: false,
      error: 'nope',
    });
  });

  it('requestAvatarGeneration uses fallback errorMessage when no details.message', async () => {
    const service = new MediaService(
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(
      service.requestAvatarGeneration({ userId: 'u1', facePhotoData: {} as never, suitColor: 'red' as never }),
    ).resolves.toEqual({
      success: false,
      requestId: '',
      avatarUrls: [],
      errorMessage: 'Failed to request avatar generation',
    });
  });

  it('uploadMedia uses fallback error when no details.message', async () => {
    const uploadMediaUseCase = {
      execute: vi.fn(async () => Result.err({ code: 'UPLOAD_FAILED' } as never)),
    };

    const service = new MediaService(
      { execute: vi.fn() } as never,
      uploadMediaUseCase as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.uploadMedia({ file: {} as never, userId: 'u1' } as never)).resolves.toEqual({
      success: false,
      error: 'Upload failed',
    });
  });

  it('getMedia throws fallback message when no details.message and not not-found', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getMedia('m1')).rejects.toThrow('Failed to get media');
  });

  it('deleteMedia uses fallback message when no details.message', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.deleteMedia('m1')).resolves.toEqual({ success: false, error: 'Failed to delete media' });
  });

  it('getAvatar throws fallback message when no details.message and not not-found', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
    );

    await expect(service.getAvatar('d1')).rejects.toThrow('Failed to get avatar');
  });

  it('updateAvatar uses fallback message when no details.message', async () => {
    const service = new MediaService(
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { execute: vi.fn() } as never,
      { execute: vi.fn() } as never,
      logger as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: {} } as never,
      { responseModel: { success: true } } as never,
    );

    await expect(service.updateAvatar('d1', { avatarUrl: 'u' } as never)).resolves.toEqual({
      success: false,
      error: 'Failed to update avatar',
    });
  });
});
