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

import { CookieResultView } from '../CookieResultView'

const SNAP = {
  id: 'abc',
  site_domain: 'bmw.de',
  created_at: '2026-06-10T22:16:11',
  cmp_vendors: [
    {
      name: 'Salesforce', category: 'necessary', country: 'US',
      recipient_type: 'PROCESSOR', compliance_score: 91,
      cookies: [
        { name: 'LSKey-c$Policy', functional_role: 'consent_state', purpose: '', expiry: '1 Jahr' },
        { name: 'sid', functional_role: 'auth_token', purpose: 'Login', expiry: 'Session' },
      ],
    },
    {
      name: 'BMW AG — eShop', category: 'necessary', country: '',
      recipient_type: 'INTERNAL', compliance_score: 100,
      cookies: [{ name: 'x', functional_role: 'preference', purpose: 'Sprache' }],
    },
    {
      name: 'Meta / Facebook', category: 'marketing', country: 'IE',
      recipient_type: 'CONTROLLER', compliance_score: 100, cookies: [],
    },
  ],
}

describe('CookieResultView', () => {
  it('zeigt KPIs + Empfänger-Gruppen aus dem Snapshot', () => {
    render(<CookieResultView snapshot={SNAP} />)
    expect(screen.getByText(/Cookie-Auswertung/)).toBeInTheDocument()
    // KPI-Kacheln vorhanden (3 Anbieter, 3 Cookies)
    expect(screen.getByText('Anbieter')).toBeInTheDocument()
    expect(screen.getByText('Cookies gesamt')).toBeInTheDocument()
    expect(screen.getAllByText('3').length).toBeGreaterThanOrEqual(2)
    // Gruppen: Eigene + Auftragsverarbeiter + Joint Controller (CONTROLLER)
    expect(screen.getByText(/Eigene Verarbeitungen/)).toBeInTheDocument()
    expect(screen.getByText(/Auftragsverarbeiter/)).toBeInTheDocument()
    expect(screen.getByText(/Joint Controller/)).toBeInTheDocument()
    expect(screen.getByText('Salesforce')).toBeInTheDocument()
    expect(screen.getByText('Meta / Facebook')).toBeInTheDocument()
  })

  it('klappt einen Vendor auf und zeigt die Cookies', () => {
    render(<CookieResultView snapshot={SNAP} />)
    fireEvent.click(screen.getByText('Salesforce'))
    expect(screen.getByText('LSKey-c$Policy')).toBeInTheDocument()
    expect(screen.getByText(/kein Zweck/)).toBeInTheDocument()  // leerer purpose
  })

  it('schaltet auf die Banner-Kategorie-Sicht um', () => {
    render(<CookieResultView snapshot={SNAP} />)
    fireEvent.click(screen.getByText('Banner-Kategorie'))
    expect(screen.getByText(/Notwendig \(essenziell\)/)).toBeInTheDocument()
    expect(screen.getByText('Salesforce')).toBeInTheDocument()
    expect(screen.getByText('Meta / Facebook')).toBeInTheDocument()
  })

  it('markiert falsch einsortierte Cookies (Tracker als notwendig)', () => {
    // 'sid' ist als necessary deklariert, Library sagt marketing → § 25-relevant.
    render(<CookieResultView snapshot={SNAP} cookieCategories={{ sid: 'marketing' }} />)
    expect(screen.getByText('Falsch einsortiert (lt. Library)')).toBeInTheDocument()
    fireEvent.click(screen.getByText('Salesforce'))
    expect(screen.getByText(/sollte: Marketing/)).toBeInTheDocument()
  })

  it('filtert nach Speichertyp (Framework vs. Cookie)', () => {
    // LSKey-c$Policy ist Framework-Storage, alle anderen echte Cookies.
    render(<CookieResultView snapshot={SNAP} storageTypes={{ 'lskey-c$policy': 'framework_storage' }} />)
    const chip = screen.getByText(/Framework \(1\)/)
    expect(chip).toBeInTheDocument()           // Chip-Leiste erscheint (Nicht-Cookie vorhanden)
    fireEvent.click(chip)
    // Nur Salesforce (hat das Framework-Objekt) bleibt sichtbar.
    expect(screen.getByText('Salesforce')).toBeInTheDocument()
    expect(screen.queryByText('BMW AG — eShop')).not.toBeInTheDocument()
    expect(screen.queryByText('Meta / Facebook')).not.toBeInTheDocument()
  })
})
