import React from 'react';
import {useTranslation} from 'react-i18next';
import {render} from '@testing-library/react-native';
import {BlockType} from '@modules/chats/interfaces';
import {getPersistedUserId} from '@services/localStorage';

import {useBlockTextHandler} from '../useBlockTextHandler';

const mockUseTranslation = useTranslation as jest.Mock;
const mockGetPersistedUserId = getPersistedUserId as jest.Mock;

jest.mock('react-i18next', () => ({
  useTranslation: jest.fn(),
}));

jest.mock('i18next', () => ({
  language: 'en',
}));

jest.mock('@services/localStorage', () => ({
  getPersistedUserId: jest.fn(),
  persistedUserId: 1,
}));

const renderHook = (props: Parameters<typeof useBlockTextHandler>[0]) => {
  let result = '';

  const HookWrapper = () => {
    result = useBlockTextHandler(props);
    return null;
  };

  render(React.createElement(HookWrapper));

  return result;
};

describe('useBlockTextHandler', () => {
  beforeEach(() => {
    mockUseTranslation.mockReset();
    mockGetPersistedUserId.mockReset();
    mockUseTranslation.mockReturnValue({
      t: (key: string) => key,
    });
    mockGetPersistedUserId.mockReturnValue(1);
  });

  it('returns empty string when required data is missing', () => {
    expect(renderHook({})).toBe('');
  });

  it('returns proper text for channel created by current user', () => {
    const text = renderHook({
      block: {type: BlockType.ChannelCreated},
      emitterUserId: 1,
      emitterUsername: 'Alice',
    });

    expect(text).toBe('chat.system_messages.channel_created_by_user');
  });

  it('returns proper text for channel joined by others', () => {
    const text = renderHook({
      block: {
        type: BlockType.ChannelJoined,
        usernames: ['Bob', 'Charlie', 'Dana', 'Eve'],
        user_ids: [2, 3, 4, 5],
      },
      emitterUserId: 2,
      emitterUsername: 'Bob',
    });

    expect(text).toBe('chat.system_messages.channel_joined_by_others');
  });

  it('returns proper text when current user left the channel', () => {
    const text = renderHook({
      block: {
        type: BlockType.ChannelLeft,
        user_id: 1,
        username: 'Alice',
      },
      emitterUserId: 1,
      emitterUsername: 'Alice',
    });

    expect(text).toBe('chat.system_messages.channel_left_by_user');
  });

  it('returns proper text for call ended scenarios', () => {
    const noResponse = renderHook({
      block: {type: BlockType.CallEnded, reason: 'NO_RESPONSE'},
      emitterUserId: 2,
      emitterUsername: 'Bob',
    });

    const ended = renderHook({
      block: {type: BlockType.CallEnded, reason: 'ALL_PARTICIPANTS_LEFT'},
      emitterUserId: 2,
      emitterUsername: 'Bob',
    });

    const rejected = renderHook({
      block: {type: BlockType.CallEnded, reason: 'REJECTED'},
      emitterUserId: 2,
      emitterUsername: 'Bob',
    });

    expect(noResponse).toBe('chat.system_messages.call_missed_by_others');
    expect(ended).toBe('chat.system_messages.call_ended');
    expect(rejected).toBe('chat.system_messages.call_refused');
  });

  it('returns proper text for call initialized', () => {
    const text = renderHook({
      block: {
        type: BlockType.CallInitialized,
        member: {
          id: 2,
          employee_internal_id: 'EMP-2',
          first_name: 'Bob',
          last_name: 'Lee',
          status: 'ACTIVE',
          version: 1,
        },
      },
      emitterUserId: 2,
      emitterUsername: 'Bob',
    });

    expect(text).toBe('chat.system_messages.call_initialized');
  });
});
