import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { ErrorMessage, FullScreenError } from '@/components/ui/ErrorMessage';

describe('ErrorMessage', () => {
  it('renders error message with retry button', () => {
    const onRetry = jest.fn();
    const { getByText } = render(
      <ErrorMessage message="Test error" onRetry={onRetry} />
    );

    expect(getByText('Test error')).toBeTruthy();
    expect(getByText('Retry')).toBeTruthy();
  });

  it('calls onRetry when retry button is pressed', () => {
    const onRetry = jest.fn();
    const { getByText } = render(
      <ErrorMessage message="Test error" onRetry={onRetry} />
    );

    fireEvent.press(getByText('Retry'));
    expect(onRetry).toHaveBeenCalledTimes(1);
  });

  it('renders without retry button when onRetry is not provided', () => {
    const { queryByText } = render(<ErrorMessage message="Test error" />);

    expect(queryByText('Retry')).toBeNull();
  });

  it('renders with skip button when onSkip is provided', () => {
    const onSkip = jest.fn();
    const { getByText } = render(
      <ErrorMessage message="Test error" onSkip={onSkip} />
    );

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

describe('FullScreenError', () => {
  it('renders full screen error with retry button', () => {
    const onRetry = jest.fn();
    const { getByText } = render(
      <FullScreenError message="Connection failed" onRetry={onRetry} />
    );

    expect(getByText('Something went wrong')).toBeTruthy();
    expect(getByText('Connection failed')).toBeTruthy();
    expect(getByText('Try Again')).toBeTruthy();
  });

  it('calls onRetry when Try Again button is pressed', () => {
    const onRetry = jest.fn();
    const { getByText } = render(
      <FullScreenError message="Connection failed" onRetry={onRetry} />
    );

    fireEvent.press(getByText('Try Again'));
    expect(onRetry).toHaveBeenCalledTimes(1);
  });

  it('renders custom title', () => {
    const { getByText } = render(
      <FullScreenError title="Custom Error" message="Test message" />
    );

    expect(getByText('Custom Error')).toBeTruthy();
  });

  it('renders without retry button when onRetry is not provided', () => {
    const { queryByText } = render(
      <FullScreenError message="Connection failed" />
    );

    expect(queryByText('Try Again')).toBeNull();
  });
});
