import React from 'react';
import { fireEvent } from '@testing-library/react-native';

import { render } from '@/components/testUtils';
import FinishContent from '../FinishContent';

// Mock for navigation
const mockGoBack = jest.fn();
jest.mock('@react-navigation/native', () => ({
  ...jest.requireActual('@react-navigation/native'),
  useNavigation: () => ({
    goBack: mockGoBack,
  }),
}));

// Mock for i18n
jest.mock('@/app/i18n/i18n', () => ({
  i18n: {
    scoped: jest.fn().mockReturnValue((key: string) => {
      const translations: Record<string, string> = {
        finish: 'Finished',
        restart: 'Restart'
      };
      return translations[key] || key;
    }),
    t: jest.fn((key: string) => {
      if (key === 'back') return 'Back';
      return key;
    }),
  },
}));

describe('[Component] FinishContent', () => {
  const defaultProps = {
    handleStart: jest.fn(),
    handleReset: jest.fn(),
  };

  beforeEach(() => {
    jest.clearAllMocks();
  });

  test('matches snapshot', () => {
    const tree = render(
      <FinishContent {...defaultProps} />
    ).toJSON();

    expect(tree).toMatchSnapshot();
  });

  test('displays content correctly', () => {
    const { getByText } = render(
      <FinishContent {...defaultProps} />
    );

    // Check that "Finished" text is displayed (translated from "finish")
    expect(getByText('Finished')).toBeTruthy();

    // Check that buttons are displayed
    expect(getByText('Restart')).toBeTruthy();
    expect(getByText('Back')).toBeTruthy();
  });

  test('calls handleStart when restart button is clicked', () => {
    const { getByText } = render(
      <FinishContent {...defaultProps} />
    );

    // Click the restart button
    fireEvent.press(getByText('Restart'));

    // Check that handleStart was called
    expect(defaultProps.handleStart).toHaveBeenCalledTimes(1);
  });

  test('calls goBack and handleReset when back button is clicked', () => {
    const { getByText } = render(
      <FinishContent {...defaultProps} />
    );

    // Click the back button
    fireEvent.press(getByText('Back'));

    // Check that navigation.goBack was called
    expect(mockGoBack).toHaveBeenCalled();

    // Check that handleReset was called
    expect(defaultProps.handleReset).toHaveBeenCalled();
  });
});