import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import { BrowserRouter } from 'react-router-dom'
import { EmailVerificationGate } from '../EmailVerificationGate'
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: 'engineer',
    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,
    ...overrides,
  }
}

const FROZEN_NOW = new Date('2026-05-06T00:00:00Z')

function renderWithRouter(ui: React.ReactElement) {
  return render(<BrowserRouter>{ui}</BrowserRouter>)
}

describe('EmailVerificationGate', () => {
  beforeEach(() => {
    vi.useFakeTimers()
    vi.setSystemTime(FROZEN_NOW)
    useAuthStore.setState({ user: null, token: null, isAuthenticated: false })
  })

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

  it('renders children when no user is signed in', () => {
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.getByText('protected')).toBeInTheDocument()
  })

  it('renders children when user has verified email', () => {
    useAuthStore.setState({
      user: makeUser({ email_verified_at: '2026-04-01T00:00:00Z' }),
    })
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.getByText('protected')).toBeInTheDocument()
  })

  it('renders children on day 1 unverified (within grace)', () => {
    // created 1 day before frozen now.
    useAuthStore.setState({
      user: makeUser({ created_at: '2026-05-05T00:00:00Z' }),
    })
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.getByText('protected')).toBeInTheDocument()
  })

  it('renders children on day 6 unverified (last day of grace)', () => {
    // created 6 days before frozen now.
    useAuthStore.setState({
      user: makeUser({ created_at: '2026-04-30T00:00:00Z' }),
    })
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.getByText('protected')).toBeInTheDocument()
  })

  it('renders wall on day 7 unverified user', () => {
    // created 7 days before frozen now -> elapsed=7, > grace=6 -> wall.
    useAuthStore.setState({
      user: makeUser({ created_at: '2026-04-29T00:00:00Z' }),
    })
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.queryByText('protected')).not.toBeInTheDocument()
    expect(screen.getByTestId('email-verification-wall')).toBeInTheDocument()
    expect(screen.getByText(/Verify your email to continue/i)).toBeInTheDocument()
  })

  it('renders wall on day 8 unverified user', () => {
    // created 8 days before frozen now.
    useAuthStore.setState({
      user: makeUser({ created_at: '2026-04-28T00:00:00Z' }),
    })
    renderWithRouter(
      <EmailVerificationGate>
        <div>protected</div>
      </EmailVerificationGate>,
    )
    expect(screen.queryByText('protected')).not.toBeInTheDocument()
    expect(screen.getByTestId('email-verification-wall')).toBeInTheDocument()
  })
})
