import { describe, it, expect } from 'vitest'
import { resolveAuthorities } from '../supervisory-authority-resolver'

// =============================================================================
// Datenschutz-Aufsichtsbehoerde (DSGVO)
// =============================================================================

describe('resolveAuthorities — Datenschutz', () => {
  it('resolves DE + BW to LfDI BW', () => {
    const results = resolveAuthorities('BW', 'DE', ['dsgvo'])
    const dp = results.find(r => r.domain === 'Datenschutz')
    expect(dp).toBeDefined()
    expect(dp!.authority.abbreviation).toBe('LfDI BW')
  })

  it('resolves DE + BY to BayLDA', () => {
    const results = resolveAuthorities('BY', 'DE', ['dsgvo'])
    const dp = results.find(r => r.domain === 'Datenschutz')
    expect(dp).toBeDefined()
    expect(dp!.authority.abbreviation).toBe('BayLDA')
  })

  it('resolves DE without state to BfDI', () => {
    const results = resolveAuthorities(undefined, 'DE', ['dsgvo'])
    const dp = results.find(r => r.domain === 'Datenschutz')
    expect(dp).toBeDefined()
    expect(dp!.authority.abbreviation).toBe('BfDI')
  })

  it('resolves AT to DSB AT', () => {
    const results = resolveAuthorities(undefined, 'AT', ['dsgvo'])
    const dp = results.find(r => r.domain === 'Datenschutz')
    expect(dp).toBeDefined()
    expect(dp!.authority.abbreviation).toBe('DSB AT')
  })

  it('resolves CH to EDOEB', () => {
    const results = resolveAuthorities(undefined, 'CH', ['dsgvo'])
    const dp = results.find(r => r.domain === 'Datenschutz')
    expect(dp).toBeDefined()
    expect(dp!.authority.abbreviation).toBe('EDOEB')
  })
})

// =============================================================================
// Regulierungs-spezifische Behoerden
// =============================================================================

describe('resolveAuthorities — regulation-specific', () => {
  it('includes BSI for NIS2 in DE', () => {
    const results = resolveAuthorities('BW', 'DE', ['dsgvo', 'nis2'])
    const nis2 = results.find(r => r.domain.includes('NIS2'))
    expect(nis2).toBeDefined()
    expect(nis2!.authority.abbreviation).toBe('BSI')
  })

  it('includes BaFin for financial_policy in DE', () => {
    const results = resolveAuthorities('BW', 'DE', ['dsgvo', 'financial_policy'])
    const fin = results.find(r => r.domain.includes('Finanzaufsicht'))
    expect(fin).toBeDefined()
    expect(fin!.authority.abbreviation).toBe('BaFin')
  })

  it('includes BNetzA for ai_act in DE', () => {
    const results = resolveAuthorities('BW', 'DE', ['dsgvo', 'ai_act'])
    const ai = results.find(r => r.domain.includes('KI-Aufsicht'))
    expect(ai).toBeDefined()
    expect(ai!.authority.abbreviation).toBe('BNetzA')
  })

  it('includes NCSA for NIS2 outside DE', () => {
    const results = resolveAuthorities(undefined, 'AT', ['dsgvo', 'nis2'])
    const nis2 = results.find(r => r.domain.includes('NIS2'))
    expect(nis2).toBeDefined()
    expect(nis2!.authority.abbreviation).toBe('NCSA')
  })
})

// =============================================================================
// Edge Cases
// =============================================================================

describe('resolveAuthorities — edge cases', () => {
  it('returns empty array when no regulations', () => {
    const results = resolveAuthorities('BW', 'DE', [])
    expect(results).toEqual([])
  })

  it('returns multiple authorities for multiple regulations', () => {
    const results = resolveAuthorities('BW', 'DE', ['dsgvo', 'nis2', 'ai_act', 'financial_policy'])
    expect(results.length).toBe(4)
  })

  it('does not include BaFin for non-DE financial_policy', () => {
    const results = resolveAuthorities(undefined, 'AT', ['financial_policy'])
    const fin = results.find(r => r.domain.includes('Finanzaufsicht'))
    expect(fin).toBeUndefined()
  })

  it('does not include BNetzA for non-DE ai_act', () => {
    const results = resolveAuthorities(undefined, 'AT', ['ai_act'])
    const ai = results.find(r => r.domain.includes('KI-Aufsicht'))
    expect(ai).toBeUndefined()
  })
})
