import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useCountries, useNetworkTopologies, useComplianceRequirements } from '../useInfrastructureData'

// Mock fetch
global.fetch = vi.fn()

const createWrapper = () => {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
        cacheTime: 0,
      },
    },
  })
  return ({ children }: { children: React.ReactNode }) => (
    <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
  )
}

describe('useInfrastructureData', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  describe('useCountries', () => {
    it('should fetch countries successfully', async () => {
      const mockCountries = [
        { name: 'Italy', region: 'Europe', relationshipType: 'Full Diplomatic Relations' },
        { name: 'Germany', region: 'Europe', relationshipType: 'Full Diplomatic Relations' },
      ]

      ;(global.fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockCountries,
      })

      const { result } = renderHook(() => useCountries(), {
        wrapper: createWrapper(),
      })

      await waitFor(() => expect(result.current.isSuccess).toBe(true))

      expect(result.current.data).toEqual(mockCountries)
      expect(global.fetch).toHaveBeenCalledWith('/api/infrastructure/data/smom_countries.json')
    })

    it('should filter countries by region', async () => {
      const mockCountries = [
        { name: 'Italy', region: 'Europe', relationshipType: 'Full Diplomatic Relations' },
        { name: 'Angola', region: 'Africa (Sub-Saharan)', relationshipType: 'Full Diplomatic Relations' },
      ]

      ;(global.fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockCountries,
      })

      const { result } = renderHook(() => useCountries({ region: 'Europe' }), {
        wrapper: createWrapper(),
      })

      await waitFor(() => expect(result.current.isSuccess).toBe(true))

      expect(result.current.data).toHaveLength(1)
      expect(result.current.data?.[0].name).toBe('Italy')
    })

    it('should handle fetch errors', async () => {
      ;(global.fetch as any).mockRejectedValueOnce(new Error('Network error'))

      const { result } = renderHook(() => useCountries(), {
        wrapper: createWrapper(),
      })

      await waitFor(() => expect(result.current.isError).toBe(true))

      expect(result.current.error).toBeTruthy()
    })
  })

  describe('useNetworkTopologies', () => {
    it('should fetch topologies successfully', async () => {
      const mockTopologies = [
        {
          id: '1',
          region: 'Europe',
          entity: 'SMOM',
          nodes: [],
          edges: [],
        },
      ]

      ;(global.fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockTopologies,
      })

      const { result } = renderHook(() => useNetworkTopologies(), {
        wrapper: createWrapper(),
      })

      await waitFor(() => expect(result.current.isSuccess).toBe(true))

      expect(result.current.topologies).toEqual(mockTopologies)
    })
  })

  describe('useComplianceRequirements', () => {
    it('should fetch compliance requirements successfully', async () => {
      const mockRequirements = [
        {
          country: 'Italy',
          region: 'Europe',
          frameworks: ['GDPR'],
          status: 'Compliant',
          requirements: ['Data protection'],
        },
      ]

      ;(global.fetch as any).mockResolvedValueOnce({
        ok: true,
        json: async () => mockRequirements,
      })

      const { result } = renderHook(() => useComplianceRequirements(), {
        wrapper: createWrapper(),
      })

      await waitFor(() => expect(result.current.isSuccess).toBe(true))

      expect(result.current.requirements).toEqual(mockRequirements)
    })
  })
})

