/**
 * (metadata, BVA for token trimming vs SignUpWrapper vs Status).
 */
import React from 'react'
import { render, screen } from '@testing-library/react'
jest.mock('@/components/auth/sign-up', () => ({
  __esModule: true,
  default: () => <div data-testid="sign-up-wrapper">SignUpWrapper</div>,
}))

jest.mock('@/components/auth/sign-up/status', () => ({
  __esModule: true,
  default: ({ token }: { token: string }) => (
    <div data-testid="sign-up-status">{`SignUpStatus:${token}`}</div>
  ),
}))

type SignupPageModule = typeof import('../page')
type SignupPageProps = Parameters<SignupPageModule['default']>[0]

describe('[locale]/(auth)/signup/page.tsx', () => {
  describe('metadata', () => {
    it('exports signup title and empty description', async () => {
      const { metadata } = await import('../page')

      expect(metadata).toEqual({ title: 'Signup', description: '' })
    })
  })

  describe('no effective token → SignUpWrapper', () => {
    it.each([
      { params: {}, label: 'token absent' },
      { params: { token: '' }, label: 'empty token string' },
      {
        params: { token: '  \t ' },
        label: 'whitespace-only token (trimmed empty)',
      },
    ])('$label renders SignUpWrapper only', async ({ params }) => {
      const { default: SignupPage } = await import('../page')

      const element = await SignupPage({
        searchParams: Promise.resolve(params),
      } as SignupPageProps)

      render(element as React.ReactElement)

      expect(screen.getByTestId('sign-up-wrapper')).toBeInTheDocument()
      expect(screen.queryByTestId('sign-up-status')).not.toBeInTheDocument()
    })
  })

  describe('truthy trimmed token → SignUpStatusWrapper', () => {
    it('passes trimmed token into status wrapper', async () => {
      const { default: SignupPage } = await import('../page')
      const token = '  invite-xyz  '

      const element = await SignupPage({
        searchParams: Promise.resolve({ token }),
      } as SignupPageProps)

      render(element as React.ReactElement)

      expect(screen.getByTestId('sign-up-status')).toHaveTextContent(
        'SignUpStatus:invite-xyz'
      )
      expect(screen.queryByTestId('sign-up-wrapper')).not.toBeInTheDocument()
    })

    it('BVA: single non-space character survives trim', async () => {
      const { default: SignupPage } = await import('../page')

      const element = await SignupPage({
        searchParams: Promise.resolve({ token: ' a ' }),
      } as SignupPageProps)

      render(element as React.ReactElement)
      expect(screen.getByText('SignUpStatus:a')).toBeInTheDocument()
    })
  })

  describe('module contract', () => {
    it('default export is async signup page entry', async () => {
      const { default: SignupPage } = await import('../page')
      expect(SignupPage).toEqual(expect.any(Function))
      expect(SignupPage.constructor.name).toBe('AsyncFunction')
    })
  })
})
