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

/**
 * Tests for Chunk-Browser logic:
 * - Collection dropdown has all 10 collections
 * - COLLECTION_TOTALS has expected keys
 * - Text search highlighting logic
 * - Pagination state management
 */

// Replicate the COMPLIANCE_COLLECTIONS from the dropdown
const COMPLIANCE_COLLECTIONS = [
  'bp_compliance_gesetze',
  'bp_compliance_ce',
  'bp_compliance_datenschutz',
  'bp_dsfa_corpus',
  'bp_compliance_recht',
  'bp_legal_templates',
  'bp_compliance_gdpr',
  'bp_compliance_schulrecht',
  'bp_dsfa_templates',
  'bp_dsfa_risks',
] as const

// Replicate COLLECTION_TOTALS from page.tsx
const COLLECTION_TOTALS: Record<string, number> = {
  bp_compliance_gesetze: 58304,
  bp_compliance_ce: 18183,
  bp_legal_templates: 7689,
  bp_compliance_datenschutz: 2448,
  bp_dsfa_corpus: 7867,
  bp_compliance_recht: 1425,
  bp_nibis_eh: 7996,
  total_legal: 76487,
  total_all: 103912,
}

describe('Chunk-Browser Logic', () => {
  describe('COMPLIANCE_COLLECTIONS', () => {
    it('should have exactly 10 collections', () => {
      expect(COMPLIANCE_COLLECTIONS).toHaveLength(10)
    })

    it('should include bp_compliance_ce for IFRS documents', () => {
      expect(COMPLIANCE_COLLECTIONS).toContain('bp_compliance_ce')
    })

    it('should include bp_compliance_datenschutz for EFRAG/ENISA', () => {
      expect(COMPLIANCE_COLLECTIONS).toContain('bp_compliance_datenschutz')
    })

    it('should include bp_compliance_gesetze as default', () => {
      expect(COMPLIANCE_COLLECTIONS[0]).toBe('bp_compliance_gesetze')
    })

    it('should have all collection names starting with bp_', () => {
      COMPLIANCE_COLLECTIONS.forEach((col) => {
        expect(col).toMatch(/^bp_/)
      })
    })
  })

  describe('COLLECTION_TOTALS', () => {
    it('should have bp_compliance_ce key', () => {
      expect(COLLECTION_TOTALS).toHaveProperty('bp_compliance_ce')
    })

    it('should have bp_compliance_datenschutz key', () => {
      expect(COLLECTION_TOTALS).toHaveProperty('bp_compliance_datenschutz')
    })

    it('should have positive counts for all collections', () => {
      Object.values(COLLECTION_TOTALS).forEach((count) => {
        expect(count).toBeGreaterThan(0)
      })
    })

    it('total_all should be greater than total_legal', () => {
      expect(COLLECTION_TOTALS.total_all).toBeGreaterThan(COLLECTION_TOTALS.total_legal)
    })
  })

  describe('Text search filtering logic', () => {
    const mockChunks = [
      { id: '1', text: 'DSGVO Artikel 1 Datenschutz', regulation_code: 'GDPR' },
      { id: '2', text: 'IFRS 16 Leasing Standard', regulation_code: 'EU_IFRS' },
      { id: '3', text: 'Datenschutz Grundverordnung', regulation_code: 'GDPR' },
      { id: '4', text: 'ENISA Supply Chain Security', regulation_code: 'ENISA' },
    ]

    it('should filter chunks by text search (case insensitive)', () => {
      const search = 'datenschutz'
      const filtered = mockChunks.filter((c) =>
        c.text.toLowerCase().includes(search.toLowerCase())
      )
      expect(filtered).toHaveLength(2)
    })

    it('should return all chunks when search is empty', () => {
      const search = ''
      const filtered = search
        ? mockChunks.filter((c) => c.text.toLowerCase().includes(search.toLowerCase()))
        : mockChunks
      expect(filtered).toHaveLength(4)
    })

    it('should return 0 chunks when no match', () => {
      const search = 'blockchain'
      const filtered = mockChunks.filter((c) =>
        c.text.toLowerCase().includes(search.toLowerCase())
      )
      expect(filtered).toHaveLength(0)
    })

    it('should match IFRS chunks', () => {
      const search = 'IFRS'
      const filtered = mockChunks.filter((c) =>
        c.text.toLowerCase().includes(search.toLowerCase())
      )
      expect(filtered).toHaveLength(1)
      expect(filtered[0].regulation_code).toBe('EU_IFRS')
    })
  })

  describe('Pagination state', () => {
    it('should start at page 0', () => {
      const currentPage = 0
      expect(currentPage).toBe(0)
    })

    it('should increment page on next', () => {
      let currentPage = 0
      currentPage += 1
      expect(currentPage).toBe(1)
    })

    it('should maintain offset history for back navigation', () => {
      const history: (string | null)[] = []
      history.push(null) // page 0 offset
      history.push('uuid-20') // page 1 offset
      history.push('uuid-40') // page 2 offset

      // Go back to page 1
      const prevOffset = history[history.length - 2]
      expect(prevOffset).toBe('uuid-20')
    })

    it('should reset state on collection change', () => {
      let chunkOffset: string | null = 'some-offset'
      let chunkHistory: (string | null)[] = [null, 'uuid-1']
      let chunkCurrentPage = 3

      // Simulate collection change
      chunkOffset = null
      chunkHistory = []
      chunkCurrentPage = 0

      expect(chunkOffset).toBeNull()
      expect(chunkHistory).toHaveLength(0)
      expect(chunkCurrentPage).toBe(0)
    })
  })
})
