/**
 * (auth-layout branches for reg-ticket segment).
 */
import React from 'react'
import { render, screen, waitFor } from '@testing-library/react'

const mutateFn = jest.fn()

jest.mock('@/providers/auth-provider', () => ({
  useAuth: jest.fn(),
}))

jest.mock('@/share/layout/end-user/header/hook', () => ({
  useGetUrlLoginMutation: jest.fn(() => ({
    mutate: mutateFn,
  })),
}))

jest.mock('@/share/components/full-page/loading', () => ({
  __esModule: true,
  default: () => <div data-testid="loading-page" />,
}))

import RegTicketLayout from '../layout'

describe('[locale]/(end-user)/reg-ticket/layout.tsx (AuthLayout)', () => {
  const { useAuth } = jest.requireMock('@/providers/auth-provider') as {
    useAuth: jest.Mock
  }

  beforeEach(() => {
    mutateFn.mockClear()
    useAuth.mockReset()
  })

  it('shows LoadingPage when auth is resolving', () => {
    useAuth.mockReturnValue({ isLoading: true, authInfo: null })
    render(
      <RegTicketLayout>
        <div>x</div>
      </RegTicketLayout>
    )
    expect(screen.getByTestId('loading-page')).toBeInTheDocument()
  })

  it('shows children after load and triggers login URL mutate when missing role', async () => {
    useAuth.mockReturnValue({ isLoading: false, authInfo: { id: 'u' } })
    render(
      <RegTicketLayout>
        <div>kid</div>
      </RegTicketLayout>
    )
    expect(screen.getByText('kid')).toBeInTheDocument()
    await waitFor(() => expect(mutateFn).toHaveBeenCalledTimes(1))
  })

  it('does not mutate when authInfo includes role', async () => {
    useAuth.mockReturnValue({
      isLoading: false,
      authInfo: { id: 'u', role: 'admin' },
    })
    render(
      <RegTicketLayout>
        <div>k</div>
      </RegTicketLayout>
    )
    await waitFor(() => expect(mutateFn).not.toHaveBeenCalled())
  })
})
