import { describe, it, expect } from 'vitest'
import {
  QAF_COLUMN_GROUPS,
  type ColumnGroup,
  groupContains,
  columnGroupOf,
} from '@/lib/qaf/column-groups'
import type { QAFRow } from '@/lib/qaf-parser'

describe('QAF_COLUMN_GROUPS', () => {
  it('has exactly 22 columns total across material + fertigung + sbm (matching QAF template)', () => {
    const total =
      QAF_COLUMN_GROUPS.material.size +
      QAF_COLUMN_GROUPS.fertigung.size +
      QAF_COLUMN_GROUPS.sbm.size
    expect(total).toBe(22)
  })

  it('material has 2 columns: beschaffungswaehrung + wechselkurs', () => {
    expect(QAF_COLUMN_GROUPS.material).toEqual(
      new Set<keyof QAFRow>(['beschaffungswaehrung', 'wechselkurs']),
    )
  })

  it('fertigung has 11 columns', () => {
    expect(QAF_COLUMN_GROUPS.fertigung.size).toBe(11)
  })

  it('sbm has 9 columns', () => {
    expect(QAF_COLUMN_GROUPS.sbm.size).toBe(9)
  })

  it('groups are disjoint (no column in two groups)', () => {
    const material = QAF_COLUMN_GROUPS.material
    const fertigung = QAF_COLUMN_GROUPS.fertigung
    const sbm = QAF_COLUMN_GROUPS.sbm
    for (const col of material) {
      expect(fertigung.has(col)).toBe(false)
      expect(sbm.has(col)).toBe(false)
    }
    for (const col of fertigung) {
      expect(sbm.has(col)).toBe(false)
    }
  })
})

describe('groupContains', () => {
  it('returns true when column is in given group', () => {
    expect(groupContains('material', 'beschaffungswaehrung')).toBe(true)
    expect(groupContains('fertigung', 'zykluszeit')).toBe(true)
    expect(groupContains('sbm', 'fek')).toBe(true)
  })

  it('returns false when column not in given group', () => {
    expect(groupContains('material', 'fek')).toBe(false)
    expect(groupContains('fertigung', 'beschaffungswaehrung')).toBe(false)
  })

  it('"alle" returns true for any column', () => {
    expect(groupContains('alle', 'beschaffungswaehrung')).toBe(true)
    expect(groupContains('alle', 'fek')).toBe(true)
    expect(groupContains('alle', 'zykluszeit')).toBe(true)
  })
})

describe('columnGroupOf', () => {
  it('returns the group a column belongs to', () => {
    expect(columnGroupOf('beschaffungswaehrung')).toBe('material')
    expect(columnGroupOf('zykluszeit')).toBe('fertigung')
    expect(columnGroupOf('fek')).toBe('sbm')
  })

  it('every group label can be cycled through', () => {
    const labels: ColumnGroup[] = ['alle', 'material', 'fertigung', 'sbm']
    expect(labels).toHaveLength(4)
  })
})
