/**
 * Tests for the shared queryRAG utility (ai-sdk /sdk/v1/rag/search, bge-m3).
 */

import { describe, it, expect, beforeEach, vi } from 'vitest'

// Mock fetch globally
const mockFetch = vi.fn()
vi.stubGlobal('fetch', mockFetch)

describe('queryRAG', () => {
  let queryRAG: (query: string, topK?: number, collection?: string) => Promise<string>

  beforeEach(async () => {
    vi.resetModules()
    mockFetch.mockReset()
    // Dynamic import to pick up fresh env
    const mod = await import('../rag-query')
    queryRAG = mod.queryRAG
  })

  it('should return formatted results on success (ai-sdk shape)', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({
        results: [
          { text: 'Art. 35 regelt die DSFA...', regulation_short: 'DSGVO' },
          { text: 'Risikobewertung erforderlich', regulation_code: 'EU_2016_679' },
        ],
      }),
    })

    const result = await queryRAG('DSFA Art. 35')

    expect(result).toContain('[Quelle 1: DSGVO]')
    expect(result).toContain('Art. 35 regelt die DSFA...')
    expect(result).toContain('[Quelle 2: EU_2016_679]')
    expect(mockFetch).toHaveBeenCalledTimes(1)
  })

  it('should POST to the ai-sdk /sdk/v1/rag/search endpoint', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({ results: [] }),
    })

    await queryRAG('test query')

    expect(mockFetch).toHaveBeenCalledWith(
      expect.stringContaining('/sdk/v1/rag/search'),
      expect.objectContaining({
        method: 'POST',
        headers: expect.objectContaining({ 'Content-Type': 'application/json' }),
      })
    )
  })

  it('should include collection in request body when provided', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({ results: [] }),
    })

    await queryRAG('test query', 3, 'bp_dsfa_corpus')

    const callArgs = mockFetch.mock.calls[0]
    const body = JSON.parse(callArgs[1].body)
    expect(body.collection).toBe('bp_dsfa_corpus')
    expect(body.query).toBe('test query')
    expect(body.top_k).toBe(3)
  })

  it('should omit collection from body when not provided', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({ results: [] }),
    })

    await queryRAG('test query')

    const callArgs = mockFetch.mock.calls[0]
    const body = JSON.parse(callArgs[1].body)
    expect(body.collection).toBeUndefined()
    expect(body.query).toBe('test query')
    expect(body.top_k).toBe(3)
  })

  it('should pass custom topK in request body', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({ results: [] }),
    })

    await queryRAG('test query', 7)

    const callArgs = mockFetch.mock.calls[0]
    const body = JSON.parse(callArgs[1].body)
    expect(body.top_k).toBe(7)
  })

  it('should return empty string on HTTP error', async () => {
    mockFetch.mockResolvedValueOnce({ ok: false, status: 500 })
    expect(await queryRAG('test query')).toBe('')
  })

  it('should return empty string on network error', async () => {
    mockFetch.mockRejectedValueOnce(new Error('Connection refused'))
    expect(await queryRAG('test query')).toBe('')
  })

  it('should return empty string when no results', async () => {
    mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({ results: [] }) })
    expect(await queryRAG('test query')).toBe('')
  })

  it('should handle results with missing source fields gracefully', async () => {
    mockFetch.mockResolvedValueOnce({
      ok: true,
      json: async () => ({ results: [{ text: 'Some content without source' }] }),
    })

    const result = await queryRAG('test')

    expect(result).toContain('[Quelle 1: Unbekannt]')
    expect(result).toContain('Some content without source')
  })
})
