/**
 * (generate-certificate query branches + PDF chrome).
 */
import React from 'react'
import { render, screen, waitFor } from '@testing-library/react'

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

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

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

jest.mock('@/share/ui/spinner', () => ({
  SpinnerCustom: () => <div data-testid="spinner" />,
}))

const mockUseGenrateCertifiate = jest.fn()

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

jest.mock('react-pdf', () => {
  const React = require('react')
  const pdfjs = { GlobalWorkerOptions: { workerSrc: '' } }
  function MockDocument({
    children,
    onLoadSuccess,
    onLoadError,
  }: {
    children?: React.ReactNode
    onLoadSuccess?: (args: { numPages: number }) => void
    onLoadError?: () => void
  }) {
    React.useEffect(() => {
      onLoadSuccess?.({ numPages: 1 })
    }, [onLoadSuccess, onLoadError])
    return <div data-testid="pdf-doc">{children}</div>
  }
  function MockPage() {
    return <div data-testid="pdf-page" />
  }
  return { pdfjs, Document: MockDocument, Page: MockPage }
})

import { CertificateView } from '../view'

beforeAll(() => {
  global.ResizeObserver = class {
    observe() {}
    disconnect() {}
  } as unknown as typeof ResizeObserver
  global.URL.createObjectURL = jest.fn(() => 'blob:mock-url')
  global.URL.revokeObjectURL = jest.fn()
})

describe('[components/api-product/tryout/certificate] CertificateView', () => {
  beforeEach(() => {
    mockNotFound.mockClear()
    mockUseGenrateCertifiate.mockReset()
  })

  describe('loading', () => {
    it('shows spinner while blob query is loading', () => {
      mockUseGenrateCertifiate.mockReturnValue({
        isLoading: true,
        isError: false,
        isSuccess: false,
      })
      render(<CertificateView cert_id="c1" />)
      expect(screen.getByTestId('spinner')).toBeInTheDocument()
    })
  })

  describe('errors / invalid payload', () => {
    it.each([
      {
        label: 'query error',
        ret: { isLoading: false, isError: true, isSuccess: false },
      },
      {
        label: 'success but non-blob data',
        ret: {
          isLoading: false,
          isError: false,
          isSuccess: true,
          data: { not: 'blob' },
        },
      },
    ])('calls notFound once on $label', ({ ret }) => {
      mockUseGenrateCertifiate.mockReturnValue(ret)
      render(<CertificateView cert_id="c1" />)
      expect(mockNotFound).toHaveBeenCalledTimes(1)
    })
  })

  describe('happy path', () => {
    it('renders PDF chrome and download link when Blob is ready', async () => {
      const blob = new Blob(['%PDF'], { type: 'application/pdf' })
      mockUseGenrateCertifiate.mockReturnValue({
        isLoading: false,
        isError: false,
        isSuccess: true,
        data: blob,
      })
      render(<CertificateView cert_id="cid-1" />)
      await waitFor(() => {
        expect(screen.getByTestId('pdf-doc')).toBeInTheDocument()
      })
      await waitFor(() => {
        expect(screen.getByTestId('pdf-page')).toBeInTheDocument()
      })
      const link = screen.getByRole('link', { name: /btn.download/i })
      expect(link).toHaveAttribute('href', 'blob:mock-url')
      expect(link).toHaveAttribute('download', 'sandbox-certificate-cid-1.pdf')
      expect(mockNotFound).not.toHaveBeenCalled()
    })
  })
})
