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

import { render } from '@/components/testUtils';
import TimerContent from '../TimerContent';
import { TimerBgColor } from '@/components/useCases/timer/business/type';

// 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> = {
        preparation: 'Préparation',
        fight: 'Combat',
        rest: 'Repos',
        prev: 'Précédent',
        next: 'Suivant',
        stop: 'Arrêter',
        start: 'Démarrer',
        preparationDescription: 'Préparez-vous !'
      };
      return translations[key] || key;
    }),
    t: jest.fn((key: string) => {
      if (key === 'back') return 'Retour';
      return key;
    }),
  },
}));

// Mock for formatTime
jest.mock('@/components/shared/business/timeHelpers', () => ({
  formatTime: jest.fn((time: number) => {
    if (time === 60000) return '01:00';
    return '00:00';
  }),
}));

describe('[Component] TimerContent', () => {
  const defaultProps = {
    isWorkPhase: true,
    timeLeft: 60000,
    reps: 5,
    currentRep: 2,
    isRunning: false,
    handleStop: jest.fn(),
    handleContinue: jest.fn(),
    nextRep: jest.fn(),
    previousRep: jest.fn(),
    bgColor: 'green' as TimerBgColor,
    handleReset: jest.fn()
  };

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

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

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

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

    expect(getByText('Combat')).toBeTruthy();
    expect(getByText('01:00')).toBeTruthy();
    expect(getByText('2 / 5')).toBeTruthy();
    expect(getByText('Précédent')).toBeTruthy();
    expect(getByText('Démarrer')).toBeTruthy();
    expect(getByText('Suivant')).toBeTruthy();
    expect(getByText('Retour')).toBeTruthy();
  });

  test('displays rest phase content correctly', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isWorkPhase={false} />
    );

    expect(getByText('Repos')).toBeTruthy();
  });

  test('displays preparation phase content correctly', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isWorkPhase={true} isPreparationPhase={true} />
    );

    expect(getByText('Préparation')).toBeTruthy();
    expect(getByText('Préparez-vous !')).toBeTruthy();
    // Previous button should be disabled in preparation phase
    expect(getByText('Précédent')).toBeTruthy();
  });

  test('shows Stop button when timer is running', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isRunning={true} />
    );

    expect(getByText('Arrêter')).toBeTruthy();
  });

  test('shows Start button when timer is stopped', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isRunning={false} />
    );

    expect(getByText('Démarrer')).toBeTruthy();
  });

  test('calls handleContinue when Start button is clicked', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} />
    );

    fireEvent.press(getByText('Démarrer'));

    expect(defaultProps.handleContinue).toHaveBeenCalledTimes(1);
  });

  test('calls handleStop when Stop button is clicked', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isRunning={true} />
    );

    fireEvent.press(getByText('Arrêter'));

    expect(defaultProps.handleStop).toHaveBeenCalledTimes(1);
  });

  test('calls previousRep when Previous button is clicked', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} />
    );

    fireEvent.press(getByText('Précédent'));

    expect(defaultProps.previousRep).toHaveBeenCalledTimes(1);
  });

  test('Previous button is disabled in preparation phase', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} isPreparationPhase={true} />
    );

    fireEvent.press(getByText('Précédent'));

    expect(defaultProps.previousRep).not.toHaveBeenCalled();
  });

  test('calls nextRep when Next button is clicked', () => {
    const { getByText } = render(
      <TimerContent {...defaultProps} />
    );

    fireEvent.press(getByText('Suivant'));

    expect(defaultProps.nextRep).toHaveBeenCalledTimes(1);
  });

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

    fireEvent.press(getByText('Retour'));

    expect(mockGoBack).toHaveBeenCalled();
    expect(defaultProps.handleReset).toHaveBeenCalled();
  });
});