import * as useCertificates from '../../hooks/use-certificates'

import { Certificate } from './Certificate'

import { renderWithProviders } from 'utils/test'

describe('<CertificatePage />', () => {
  it('should render the loading state initially', () => {
    vi.spyOn(useCertificates, 'useCertificate').mockImplementation(() => ({
      certificate: null,
      error: null,
      isLoading: true,
    }))

    const { getByTestId } = renderWithProviders(<Certificate />, {
      route: '/certificates/dW5rbm93bi1jZXJ0LWtleQ==',
      withPage: true,
    })

    expect(getByTestId('skeleton')).toBeInTheDocument()
  })

  it('should render error message when API returns error', () => {
    vi.spyOn(useCertificates, 'useCertificate').mockImplementation(() => ({
      certificate: null,
      error: new Error('Internal Server Error'),
      isLoading: false,
    }))

    const { getByTestId } = renderWithProviders(<Certificate />, {
      route: '/certificates/c29tZS1jZXJ0',
      withPage: true,
    })

    expect(getByTestId('error-text')).toBeInTheDocument()
  })

  it('should render not found page when certificate is null', () => {
    vi.spyOn(useCertificates, 'useCertificate').mockImplementation(() => ({
      certificate: null,
      error: null,
      isLoading: false,
    }))

    const { getByTestId } = renderWithProviders(<Certificate />, {
      route: '/certificates/bm90Zm91bmQ=',
      withPage: true,
    })

    expect(getByTestId('Not found page')).toBeInTheDocument()
  })

  it('should render certificate details successfully', () => {
    const mockCertificate = {
      name: 'dGVzdC1jZXJ0',
      commonName: 'test.com',
      sans: ['test.com', 'www.test.com'],
      issuerOrg: 'Test CA',
      issuerCN: 'Test Root CA',
      notAfter: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
      notBefore: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
      status: 'enabled' as const,
      serialNumber: '123456',
      version: '3',
      keyType: 'RSA',
      signatureAlgorithm: 'SHA256WithRSA',
      certFingerprint: 'a1b2c3d4e5f60708090a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60',
      publicKeyFingerprint: 'b2c3d4e5f60708090a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f6071',
    }

    vi.spyOn(useCertificates, 'useCertificate').mockImplementation(() => ({
      certificate: { ...mockCertificate, daysLeft: 365 },
      error: null,
      isLoading: false,
    }))

    const { getByText } = renderWithProviders(<Certificate />, {
      route: '/certificates/dGVzdC1jZXJ0',
      withPage: true,
    })

    // Check for actual rendered content
    expect(getByText('Certificate')).toBeInTheDocument()
    expect(getByText('Issued To')).toBeInTheDocument()
    expect(getByText('Issued By')).toBeInTheDocument()
    expect(getByText('www.test.com')).toBeInTheDocument()
    expect(getByText('Test CA')).toBeInTheDocument()
    expect(getByText('Test Root CA')).toBeInTheDocument()
  })
})
