/**
 * (certificate query: loading, error/null→notFound, success→form + header).
 */
import React from 'react'
import { render, screen } from '@testing-library/react'

const mockNotFound = jest.fn(() => null)
const mockBack = jest.fn()

jest.mock('next/navigation', () => ({
  notFound: () => mockNotFound(),
  useRouter: () => ({ back: mockBack }),
}))

jest.mock('next-intl', () => ({
  useTranslations: () => (k: string) => k,
}))

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

jest.mock('../form', () => ({
  __esModule: true,
  default: () => <div data-testid="reg-ticket-form-stub" />,
}))

const mockUseGetScenarioCertificate = jest.fn()

jest.mock('@/services/scenario/scenario.query-options', () => ({
  useGetScenarioCertificate: (...a: unknown[]) =>
    mockUseGetScenarioCertificate(...a),
}))

import CreateRegTicketWrapper from '../index'

describe('[components/regTicket/create] CreateRegTicketWrapper', () => {
  const props = {
    scenario_id: 's',
    version_id: 'v',
    application_id: 'app',
    api_id: 'api',
  } as const

  beforeEach(() => {
    mockNotFound.mockClear()
    mockUseGetScenarioCertificate.mockReset()
  })

  describe('useGetScenarioCertificate contract', () => {
    it('invokes hook with application_id, scenario_id, version_id, api_id', () => {
      mockUseGetScenarioCertificate.mockReturnValue({
        isLoading: true,
        isError: false,
        isSuccess: false,
      })
      render(<CreateRegTicketWrapper {...props} />)
      expect(mockUseGetScenarioCertificate).toHaveBeenCalledWith(
        'app',
        's',
        'v',
        'api',
      )
      expect(mockUseGetScenarioCertificate).toHaveBeenCalledTimes(1)
    })
  })

  describe('loading', () => {
    it('shows loading page while certificate query runs', () => {
      mockUseGetScenarioCertificate.mockReturnValue({
        isLoading: true,
        isError: false,
        isSuccess: false,
      })
      render(<CreateRegTicketWrapper {...props} />)
      expect(screen.getByTestId('reg-ticket-create-loading')).toBeInTheDocument()
    })
  })

  describe('errors — notFound', () => {
    it('calls notFound when query errors', () => {
      mockUseGetScenarioCertificate.mockReturnValue({
        isLoading: false,
        isError: true,
        isSuccess: false,
      })
      render(<CreateRegTicketWrapper {...props} />)
      expect(mockNotFound).toHaveBeenCalledTimes(1)
    })

    it('calls notFound when certificate payload is null', () => {
      mockUseGetScenarioCertificate.mockReturnValue({
        isLoading: false,
        isError: false,
        isSuccess: true,
        data: { data: null },
      })
      render(<CreateRegTicketWrapper {...props} />)
      expect(mockNotFound).toHaveBeenCalledTimes(1)
    })
  })

  describe('happy path', () => {
    it('renders form stub and create header when certificate exists', () => {
      mockUseGetScenarioCertificate.mockReturnValue({
        isLoading: false,
        isError: false,
        isSuccess: true,
        data: {
          data: {
            id: 'cid',
            certificateNumber: 'n',
            scenarioName: 'sn',
            apiName: 'an',
            apiId: 'api',
            applicationId: 'app',
            issueDate: 'd',
            status: 'ACTIVE',
          },
        },
      })
      render(<CreateRegTicketWrapper {...props} />)
      expect(screen.getByTestId('reg-ticket-form-stub')).toBeInTheDocument()
      expect(screen.getByText('create.header')).toBeInTheDocument()
    })
  })
})
