import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { MemoryRouter, Route, Routes } from 'react-router-dom'

import { WelcomeRouter } from '../WelcomeRouter'
import { useAuthStore } from '@/store/authStore'
import type { User } from '@/types'

function makeUser(overrides: Partial<User> = {}): User {
  return {
    id: 'user-1',
    email: 'test@example.com',
    name: 'Test User',
    role: 'engineer',
    is_super_admin: false,
    is_active: true,
    must_change_password: false,
    account_id: 'acct-1',
    account_role: 'owner',
    team_id: null,
    created_at: '2026-05-01T00:00:00Z',
    last_login: null,
    phone: null,
    job_title: null,
    timezone: 'UTC',
    avatar_url: null,
    email_verified_at: null,
    onboarding_step_completed: null,
    onboarding_dismissed: false,
    ...overrides,
  }
}

function renderRouter() {
  return render(
    <MemoryRouter initialEntries={['/welcome']}>
      <Routes>
        <Route path="/welcome" element={<WelcomeRouter />} />
        <Route path="/welcome/step-1" element={<div>step-1</div>} />
        <Route path="/welcome/step-2" element={<div>step-2</div>} />
        <Route path="/welcome/step-3" element={<div>step-3</div>} />
        <Route path="/home" element={<div>dashboard</div>} />
      </Routes>
    </MemoryRouter>,
  )
}

describe('WelcomeRouter', () => {
  beforeEach(() => {
    useAuthStore.setState({
      user: null,
      account: null,
      subscription: null,
      token: null,
      isAuthenticated: false,
    })
  })

  afterEach(() => {
    vi.clearAllMocks()
  })

  it('redirects to step-1 on null onboarding_step_completed', async () => {
    useAuthStore.setState({
      user: makeUser({ onboarding_step_completed: null }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('step-1')).toBeInTheDocument()
    })
  })

  it('redirects to step-1 when onboarding_step_completed is 0', async () => {
    useAuthStore.setState({
      user: makeUser({ onboarding_step_completed: 0 }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('step-1')).toBeInTheDocument()
    })
  })

  it('redirects to step-2 when onboarding_step_completed is 1', async () => {
    useAuthStore.setState({
      user: makeUser({ onboarding_step_completed: 1 }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('step-2')).toBeInTheDocument()
    })
  })

  it('redirects to step-3 when onboarding_step_completed is 2', async () => {
    useAuthStore.setState({
      user: makeUser({ onboarding_step_completed: 2 }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('step-3')).toBeInTheDocument()
    })
  })

  it('redirects to /home when onboarding_step_completed >= 3', async () => {
    useAuthStore.setState({
      user: makeUser({ onboarding_step_completed: 3 }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('dashboard')).toBeInTheDocument()
    })
  })

  it('redirects to /home when onboarding_dismissed is true', async () => {
    useAuthStore.setState({
      user: makeUser({
        onboarding_step_completed: 1,
        onboarding_dismissed: true,
      }),
    })
    renderRouter()
    await waitFor(() => {
      expect(screen.getByText('dashboard')).toBeInTheDocument()
    })
  })
})
