import React from 'react';
import { fireEvent, render } from '@testing-library/react-native';
import SearchScreen from '../../app/(tabs)/search';

const mockPush = jest.fn();

jest.mock('expo-router', () => ({
  useRouter: () => ({
    push: mockPush,
  }),
}));

jest.mock('react-native-safe-area-context', () => {
  const React = require('react');
  const { View } = require('react-native');
  return {
    SafeAreaView: ({ children }: { children: React.ReactNode }) => <View>{children}</View>,
  };
});

jest.mock('../../context/AppContext', () => ({
  useApp: () => ({
    plants: [],
    isDarkMode: false,
    colorPalette: 'forest',
    language: 'de',
    billingSummary: { credits: { available: 5 } },
    refreshBillingSummary: jest.fn().mockResolvedValue(undefined),
    t: {
      searchTitle: 'Suche',
      searchPlaceholder: 'Pflanzen suchen...',
      catCareEasy: 'Pflegeleicht',
      catLowLight: 'Wenig Licht',
      catBrightLight: 'Helles Licht',
      catSun: 'Sonnig',
      catPetFriendly: 'Tierfreundlich',
      catAirPurifier: 'Luftreiniger',
      catHighHumidity: 'Hohe Luftfeuchte',
      catHanging: 'Hängend',
      catPatterned: 'Gemustert',
      catFlowering: 'Blühend',
      catSucculents: 'Sukkulenten',
      catTree: 'Bäume',
      catLarge: 'Groß',
      catMedicinal: 'Heilpflanzen',
      lexiconTitle: 'Pflanzen-Lexikon',
      lexiconDesc: 'Lexikon Beschreibung',
      browseLexicon: 'Im Lexikon stöbern',
    },
  }),
}));

jest.mock('../../constants/Colors', () => ({
  useColors: () => ({
    background: '#ffffff',
    text: '#111111',
    textSecondary: '#444444',
    textMuted: '#666666',
    cardBg: '#ffffff',
    cardBorder: '#dddddd',
    cardShadow: '#000000',
    chipBorder: '#dddddd',
    successTint: '#dff5e3',
    success: '#2d8a4b',
    infoTint: '#d9f1ff',
    info: '#2469a7',
    primaryTint: '#e8f2ea',
    primaryDark: '#1f5a37',
    warningTint: '#fff3d6',
    warning: '#b7791f',
    dangerTint: '#fde3e3',
    danger: '#b91c1c',
    surfaceStrong: '#eeeeee',
    surface: '#f5f5f5',
    heroButtonBorder: '#cad7cc',
    iconOnImage: '#ffffff',
    textOnImage: '#ffffff',
    heroButton: '#dce9df',
    primary: '#2f855a',
    onPrimary: '#ffffff',
    fabShadow: '#000000',
  }),
}));

jest.mock('../../components/ThemeBackdrop', () => ({
  ThemeBackdrop: () => null,
}));

jest.mock('../../services/plantDatabaseService', () => ({
  PlantDatabaseService: {
    searchPlants: jest.fn().mockResolvedValue([]),
  },
}));

describe('SearchScreen category navigation', () => {
  beforeEach(() => {
    mockPush.mockClear();
  });

  it('navigates to lexicon with categoryId only when a category chip is tapped', () => {
    const { getByText } = render(<SearchScreen />);

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

    expect(mockPush).toHaveBeenCalledWith({
      pathname: '/lexicon',
      params: {
        categoryId: 'easy',
      },
    });
  });
});
