import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import Navigation from '../components/Navigation'
import { useAuthStore } from '../store/auth'

vi.mock('../store/auth', () => ({
  useAuthStore: vi.fn(),
}))

describe('Navigation component', () => {
  const renderNav = (user = null, path = '/') =>
    render(
      <MemoryRouter initialEntries={[path]}>
        <Navigation />
      </MemoryRouter>
    )

  beforeEach(() => {
    useAuthStore.mockImplementation((selector) => selector({ user: null }))
  })

  it('renders navigation with main links', () => {
    renderNav()
    expect(screen.getByText('Главная')).toBeInTheDocument()
    expect(screen.getByText('Трекер')).toBeInTheDocument()
    expect(screen.getByText('Накопления')).toBeInTheDocument()
    expect(screen.getByText('Настройки')).toBeInTheDocument()
  })

  it('renders navigation as nav element', () => {
    renderNav()
    expect(screen.getByRole('navigation')).toBeInTheDocument()
  })

  it('renders links for all nav items', () => {
    renderNav()
    const links = screen.getAllByRole('link')
    expect(links.length).toBeGreaterThanOrEqual(4)
  })

  it('renders with user as owner', () => {
    useAuthStore.mockImplementation((selector) => selector({ user: { id: 1 } }))
    renderNav()
    expect(screen.getByText('Главная')).toBeInTheDocument()
  })

  it('renders with non-owner user', () => {
    useAuthStore.mockImplementation((selector) => selector({ user: { id: 2 } }))
    renderNav()
    expect(screen.getByText('Главная')).toBeInTheDocument()
  })
})
