/**
 * (happy path, BVA for routing.defaultLocale, module contract).
 */
import { describe, it, expect, jest, beforeEach } from '@jest/globals'

describe('app/page.tsx — RootPage', () => {
  beforeEach(() => {
    jest.resetModules()
    jest.doMock('@/i18n/routing', () => ({
      routing: {
        locales: ['vi', 'en'],
        defaultLocale: 'vi',
        localePrefix: 'always',
        localeDetection: false,
      },
    }))
    jest.doMock('next/navigation', () => ({
      redirect: jest.fn(),
    }))
  })

  it('happy path: redirects exactly once using defaultLocale from routing', async () => {
    const mockRedirect = jest.fn()
    jest.doMock('next/navigation', () => ({
      redirect: mockRedirect,
    }))

    const { default: RootPage } = await import('../../page')
    RootPage()

    expect(mockRedirect).toHaveBeenCalledTimes(1)
    expect(mockRedirect).toHaveBeenCalledWith('vi')
  })

  it.each([
    { defaultLocale: 'vi', expected: 'vi' },
    { defaultLocale: 'en', expected: 'en' },
  ])(
    'BVA / contract: redirects to defaultLocale=$defaultLocale',
    async ({ defaultLocale, expected }) => {
      jest.resetModules()

      jest.doMock('@/i18n/routing', () => ({
        routing: {
          locales: ['vi', 'en'],
          defaultLocale,
          localePrefix: 'always',
          localeDetection: false,
        },
      }))

      const mockRedirect = jest.fn()
      jest.doMock('next/navigation', () => ({
        redirect: mockRedirect,
      }))

      const { default: RootPage } = await import('../../page')
      RootPage()

      expect(mockRedirect).toHaveBeenCalledWith(expected)
    }
  )

  it('default export is a function (callable entry)', async () => {
    const { default: RootPage } = await import('../../page')
    expect(RootPage).toEqual(expect.any(Function))
  })
})

describe('app/not-found.tsx — root segment module', () => {
  it('contract: default export is a function component', async () => {
    const mod = await import('../../not-found')
    expect(mod.default).toEqual(expect.any(Function))
  })
})
