import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import Tracker from '../pages/Tracker'

// Mock child pages
vi.mock('../pages/Habits', () => ({
  default: ({ embedded }) => <div data-testid="habits">Habits {embedded ? 'embedded' : ''}</div>
}))
vi.mock('../pages/Tasks', () => ({
  default: ({ embedded }) => <div data-testid="tasks">Tasks {embedded ? 'embedded' : ''}</div>
}))
vi.mock('../pages/Stats', () => ({
  default: ({ embedded }) => <div data-testid="stats">Stats {embedded ? 'embedded' : ''}</div>
}))
vi.mock('../components/Navigation', () => ({
  default: () => <nav data-testid="navigation">Nav</nav>
}))

describe('Tracker', () => {
  it('renders header with title', () => {
    render(<Tracker />)
    expect(screen.getByText(/Трекер/)).toBeInTheDocument()
  })

  it('renders all three tab buttons', () => {
    render(<Tracker />)
    expect(screen.getByText(/Привычки/)).toBeInTheDocument()
    expect(screen.getByText(/Задачи/)).toBeInTheDocument()
    expect(screen.getByText(/Статистика/)).toBeInTheDocument()
  })

  it('shows Habits tab by default', () => {
    render(<Tracker />)
    expect(screen.getByTestId('habits')).toBeInTheDocument()
    expect(screen.queryByTestId('tasks')).not.toBeInTheDocument()
    expect(screen.queryByTestId('stats')).not.toBeInTheDocument()
  })

  it('switches to Tasks tab on click', () => {
    render(<Tracker />)
    fireEvent.click(screen.getByText(/Задачи/))
    expect(screen.getByTestId('tasks')).toBeInTheDocument()
    expect(screen.queryByTestId('habits')).not.toBeInTheDocument()
  })

  it('switches to Stats tab on click', () => {
    render(<Tracker />)
    fireEvent.click(screen.getByText(/Статистика/))
    expect(screen.getByTestId('stats')).toBeInTheDocument()
    expect(screen.queryByTestId('habits')).not.toBeInTheDocument()
  })

  it('switches back to Habits', () => {
    render(<Tracker />)
    fireEvent.click(screen.getByText(/Задачи/))
    fireEvent.click(screen.getByText(/Привычки/))
    expect(screen.getByTestId('habits')).toBeInTheDocument()
  })

  it('renders navigation', () => {
    render(<Tracker />)
    expect(screen.getByTestId('navigation')).toBeInTheDocument()
  })
})
