/**
 * ResourceList Component Tests
 */

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ResourceList } from '../resources/ResourceList'

// Mock Apollo Client
vi.mock('@/lib/graphql/client', () => ({
  apolloClient: {
    query: vi.fn(),
  },
}))

describe('ResourceList', () => {
  let queryClient: QueryClient

  beforeEach(() => {
    queryClient = new QueryClient({
      defaultOptions: {
        queries: {
          retry: false,
        },
      },
    })
  })

  it('should render loading state', () => {
    render(
      <QueryClientProvider client={queryClient}>
        <ResourceList />
      </QueryClientProvider>
    )

    expect(screen.getByText(/loading resources/i)).toBeInTheDocument()
  })

  it('should render resources when loaded', async () => {
    const mockResources = [
      {
        id: 'resource-1',
        name: 'Test Resource',
        type: 'VM',
        status: 'RUNNING',
        site: {
          id: 'site-1',
          name: 'Test Site',
          region: 'us-east-1',
        },
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
      },
    ]

    const { apolloClient } = await import('@/lib/graphql/client')
    vi.mocked(apolloClient.query).mockResolvedValueOnce({
      data: { resources: mockResources },
    } as any)

    render(
      <QueryClientProvider client={queryClient}>
        <ResourceList />
      </QueryClientProvider>
    )

    await waitFor(() => {
      expect(screen.getByText('Test Resource')).toBeInTheDocument()
    })
  })

  it('should render error state', async () => {
    const { apolloClient } = await import('@/lib/graphql/client')
    vi.mocked(apolloClient.query).mockRejectedValueOnce(new Error('Network error'))

    render(
      <QueryClientProvider client={queryClient}>
        <ResourceList />
      </QueryClientProvider>
    )

    await waitFor(() => {
      expect(screen.getByText(/error/i)).toBeInTheDocument()
    })
  })
})

