/**
 * Tests for SDK API Client extensions (modules, UCCA, import, screening).
 */
import { describe, it, expect, vi, beforeEach } from 'vitest'

// Mock fetch globally
const mockFetch = vi.fn()
global.fetch = mockFetch

// Import after mocking
import { SDKApiClient } from '../api-client'

const client = new SDKApiClient({
  baseUrl: '/api/sdk/v1',
  tenantId: 'test-tenant',
})

describe('SDK API Client', () => {
  beforeEach(() => {
    mockFetch.mockReset()
  })

  describe('getModules', () => {
    it('fetches modules from backend', async () => {
      mockFetch.mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({ modules: [{ id: 'mod-1', name: 'DSGVO' }], total: 1 }),
      })

      const result = await client.getModules()
      expect(result.modules).toHaveLength(1)
      expect(result.total).toBe(1)
      expect(mockFetch).toHaveBeenCalledWith(
        expect.stringContaining('/api/sdk/v1/modules'),
        expect.any(Object)
      )
    })

    it('throws on network error', async () => {
      mockFetch.mockRejectedValueOnce(new Error('Network error'))
      await expect(client.getModules()).rejects.toThrow()
    })
  })

  describe('analyzeDocument', () => {
    it('sends FormData to import analyze endpoint', async () => {
      mockFetch.mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({
          data: { document_id: 'doc-1', detected_type: 'DSFA', confidence: 0.85 },
        }),
      })

      const formData = new FormData()
      const result = await client.analyzeDocument(formData) as any
      expect(result.document_id).toBe('doc-1')
      expect(mockFetch).toHaveBeenCalledWith(
        expect.stringContaining('/api/sdk/v1/import/analyze'),
        expect.objectContaining({ method: 'POST' })
      )
    })
  })

  describe('scanDependencies', () => {
    it('sends FormData to screening scan endpoint', async () => {
      mockFetch.mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({
          data: { id: 'scan-1', status: 'completed', total_components: 10, total_issues: 2 },
        }),
      })

      const formData = new FormData()
      const result = await client.scanDependencies(formData) as any
      expect(result.id).toBe('scan-1')
      expect(result.total_components).toBe(10)
    })
  })

  describe('assessUseCase', () => {
    it('sends intake data to UCCA assess endpoint', async () => {
      mockFetch.mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({ id: 'assessment-1', feasibility: 'GREEN' }),
      })

      const result = await client.assessUseCase({
        name: 'Test Use Case',
        domain: 'education',
      }) as any
      expect(result.feasibility).toBe('GREEN')
    })
  })

  describe('getAssessments', () => {
    it('fetches assessment list', async () => {
      mockFetch.mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({ data: [{ id: 'a1' }, { id: 'a2' }] }),
      })

      const result = await client.getAssessments()
      expect(result).toHaveLength(2)
    })
  })
})
