import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent, act } from '@testing-library/react'
import PublicView from './views/PublicView.jsx'

beforeEach(() => {
  vi.useFakeTimers()
})

afterEach(() => {
  vi.restoreAllMocks()
  vi.useRealTimers()
})

// Helper: navigate from HomeView to the search screen
async function navigateToSearch() {
  fireEvent.click(screen.getByRole('button', { name: /manual search/i }))
}

describe('HomeView', () => {
  it('renders two action buttons on the home screen', () => {
    render(<PublicView />)
    expect(screen.getByRole('button', { name: /scan tsi/i })).toBeInTheDocument()
    expect(screen.getByRole('button', { name: /manual search/i })).toBeInTheDocument()
  })

  it('navigates to search screen when Manual Search is clicked', async () => {
    render(<PublicView />)
    await navigateToSearch()
    expect(screen.getByPlaceholderText(/search for a medicine/i)).toBeInTheDocument()
  })
})

describe('PublicView (search screen)', () => {
  it('renders search bar on load with no results', async () => {
    render(<PublicView />)
    await navigateToSearch()
    expect(screen.getByPlaceholderText(/search for a medicine/i)).toBeInTheDocument()
    expect(screen.queryByRole('list')).not.toBeInTheDocument()
  })

  it('does not fetch for queries shorter than 2 chars', async () => {
    const fetchMock = vi.spyOn(globalThis, 'fetch')
    render(<PublicView />)
    await navigateToSearch()

    fireEvent.change(screen.getByPlaceholderText(/search for a medicine/i), {
      target: { value: 'a' },
    })

    await act(async () => {
      await vi.runAllTimersAsync()
    })

    expect(fetchMock).not.toHaveBeenCalled()
  })

  it('fetches and displays results for valid query', async () => {
    const medicines = [
      { id: 'REG001', name: 'Ibuprofeno 400mg', active_ingredient: 'Ibuprofeno', dosage: '400mg', form: 'Comprimido' },
    ]
    vi.spyOn(globalThis, 'fetch').mockResolvedValue({
      json: async () => medicines,
    })

    render(<PublicView />)
    await navigateToSearch()

    fireEvent.change(screen.getByPlaceholderText(/search for a medicine/i), {
      target: { value: 'ibu' },
    })

    await act(async () => {
      await vi.runAllTimersAsync()
    })

    expect(screen.getByText('Ibuprofeno 400mg')).toBeInTheDocument()
  })

  it('clears results when query is cleared after a search', async () => {
    const medicines = [
      { id: 'REG001', name: 'Ibuprofeno 400mg', active_ingredient: 'Ibuprofeno', dosage: '400mg', form: 'Comprimido' },
    ]
    vi.spyOn(globalThis, 'fetch').mockResolvedValue({
      json: async () => medicines,
    })

    render(<PublicView />)
    await navigateToSearch()
    const input = screen.getByPlaceholderText(/search for a medicine/i)

    fireEvent.change(input, { target: { value: 'ibu' } })
    await act(async () => { await vi.runAllTimersAsync() })
    expect(screen.getByText('Ibuprofeno 400mg')).toBeInTheDocument()

    fireEvent.change(input, { target: { value: '' } })
    await act(async () => { await vi.runAllTimersAsync() })

    expect(screen.queryByText('Ibuprofeno 400mg')).not.toBeInTheDocument()
  })
})

