import { describe, it, expect } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'

import { CookieFindings } from '../CookieFindings'

const FINDINGS = [
  { vendor: 'Salesforce', cookie: '_ga', type: 'tracker_as_necessary', severity: 'HIGH',
    declared: 'necessary', library_purpose: '', remediation: '', kind: 'finding' },
  { vendor: 'Acme', cookie: 'foo', type: 'missing_purpose', severity: 'MEDIUM',
    declared: '', library_purpose: '', remediation: '', kind: 'finding' },
  { vendor: 'Google', cookie: '_gid', type: 'third_country', severity: 'MEDIUM',
    declared: 'US', library_purpose: '', remediation: '', kind: 'hinweis' },
]

describe('CookieFindings', () => {
  it('gruppiert nach Typ und trennt Findings von Hinweisen', () => {
    render(<CookieFindings findings={FINDINGS} />)
    expect(screen.getByText(/3 Befunde/)).toBeInTheDocument()
    expect(screen.getByText(/Findings — zu beheben/)).toBeInTheDocument()
    expect(screen.getByText(/Hinweise — neutral/)).toBeInTheDocument()
    expect(screen.getByText(/Tracker als/)).toBeInTheDocument()
    expect(screen.getByText('Drittland-Transfer')).toBeInTheDocument()
  })

  it('klappt eine Gruppe auf und zeigt Maßnahme + Ticket-Button', () => {
    render(<CookieFindings findings={FINDINGS} />)
    fireEvent.click(screen.getByText(/Zweck fehlt/))
    expect(screen.getByText(/Maßnahme:/)).toBeInTheDocument()
    expect(screen.getByText(/Ticket-Text kopieren/)).toBeInTheDocument()
  })

  it('schaltet auf die Matrix-Sicht um', () => {
    render(<CookieFindings findings={FINDINGS} />)
    fireEvent.click(screen.getByText('Matrix'))
    expect(screen.getByText(/Handlung nötig/)).toBeInTheDocument()
  })

  it('zeigt grünen Hinweis bei 0 Befunden', () => {
    render(<CookieFindings findings={[]} />)
    expect(screen.getByText(/Keine Abweichungen/)).toBeInTheDocument()
  })
})
