/**
 * Tests fuer ExpiredEvidenceAlert Komponente
 * Sprint 4: Stakeholder-Views & Rollenbasierte Features
 */

import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'

// Mock next/navigation
jest.mock('next/navigation', () => ({
  useRouter: () => ({
    push: jest.fn(),
  }),
}))

// Mock der Komponente (da wir den Source noch importieren muessen)
import ExpiredEvidenceAlert, { ExpiredEvidence } from '@/components/compliance/ExpiredEvidenceAlert'

const mockExpiredEvidence: ExpiredEvidence[] = [
  {
    id: '1',
    title: 'SAST Scan Report Q4/2025',
    control_id: 'SDLC-001',
    control_name: 'SAST Scanning',
    expired_at: '2025-12-31',
    days_expired: 18,
    days_until_expiry: -18,
    status: 'expired',
    artifact_type: 'scan_report'
  },
  {
    id: '2',
    title: 'Backup Recovery Test',
    control_id: 'OPS-002',
    control_name: 'Backup & Recovery',
    expired_at: '2026-01-25',
    days_expired: null,
    days_until_expiry: 7,
    status: 'expiring_soon',
    artifact_type: 'test_result'
  },
]

describe('ExpiredEvidenceAlert', () => {
  describe('Banner Variante (default)', () => {
    it('rendert mit korrekter Ueberschrift fuer abgelaufene Evidence', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" />)

      expect(screen.getByText(/Nachweise erfordern Aufmerksamkeit/i)).toBeInTheDocument()
    })

    it('zeigt korrekte Anzahl abgelaufener und ablaufender Items', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" />)

      expect(screen.getByText(/1 abgelaufen/i)).toBeInTheDocument()
      expect(screen.getByText(/1 laufen bald ab/i)).toBeInTheDocument()
    })

    it('kann expandiert werden um Details anzuzeigen', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" />)

      const detailsButton = screen.getByText('Details')
      fireEvent.click(detailsButton)

      expect(screen.getByText('SAST Scan Report Q4/2025')).toBeInTheDocument()
      expect(screen.getByText('Backup Recovery Test')).toBeInTheDocument()
    })

    it('kann dismissed werden', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" />)

      const dismissButton = screen.getByTitle(/Schliessen/i)
      fireEvent.click(dismissButton)

      expect(screen.queryByText(/Nachweise erfordern Aufmerksamkeit/i)).not.toBeInTheDocument()
    })

    it('zeigt nichts an wenn keine Evidence vorhanden', () => {
      const { container } = render(<ExpiredEvidenceAlert evidenceList={[]} variant="banner" />)

      expect(container.firstChild).toBeNull()
    })
  })

  describe('Card Variante', () => {
    it('rendert Kompakt-Karte mit Header', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="card" />)

      expect(screen.getByText('Nachweis-Warnungen')).toBeInTheDocument()
    })

    it('zeigt maxItems Anzahl von Items', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="card" maxItems={1} />)

      expect(screen.getByText('SAST Scan Report Q4/2025')).toBeInTheDocument()
      expect(screen.queryByText('Backup Recovery Test')).not.toBeInTheDocument()
    })

    it('zeigt "Alle anzeigen" Link wenn mehr Items als maxItems', () => {
      const manyItems = [...mockExpiredEvidence, ...mockExpiredEvidence]
      render(<ExpiredEvidenceAlert evidenceList={manyItems} variant="card" maxItems={2} />)

      expect(screen.getByText(/Alle \d+ anzeigen/)).toBeInTheDocument()
    })
  })

  describe('Minimal Variante', () => {
    it('rendert nur Icon mit Badge', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="minimal" />)

      // Badge sollte Gesamtzahl zeigen
      expect(screen.getByText('2')).toBeInTheDocument()
    })

    it('zeigt korrekten Tooltip-Text', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="minimal" language="de" />)

      const button = screen.getByRole('button')
      expect(button).toHaveAttribute('title', '1 abgelaufen, 1 laufen bald ab')
    })
  })

  describe('Sprachumschaltung', () => {
    it('zeigt deutsche Texte bei language=de', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" language="de" />)

      expect(screen.getByText(/abgelaufen/i)).toBeInTheDocument()
      expect(screen.getByText('Nachweise verwalten')).toBeInTheDocument()
    })

    it('zeigt englische Texte bei language=en', () => {
      render(<ExpiredEvidenceAlert evidenceList={mockExpiredEvidence} variant="banner" language="en" />)

      expect(screen.getByText(/expired/i)).toBeInTheDocument()
      expect(screen.getByText('Manage Evidence')).toBeInTheDocument()
    })
  })

  describe('Callbacks', () => {
    it('ruft onViewAll Callback auf', () => {
      const mockOnViewAll = jest.fn()
      render(
        <ExpiredEvidenceAlert
          evidenceList={mockExpiredEvidence}
          variant="banner"
          onViewAll={mockOnViewAll}
        />
      )

      const manageButton = screen.getByText('Nachweise verwalten')
      fireEvent.click(manageButton)

      expect(mockOnViewAll).toHaveBeenCalledTimes(1)
    })

    it('ruft onItemClick Callback auf', () => {
      const mockOnItemClick = jest.fn()
      render(
        <ExpiredEvidenceAlert
          evidenceList={mockExpiredEvidence}
          variant="card"
          onItemClick={mockOnItemClick}
        />
      )

      const firstItem = screen.getByText('SAST Scan Report Q4/2025')
      fireEvent.click(firstItem)

      expect(mockOnItemClick).toHaveBeenCalledWith(mockExpiredEvidence[0])
    })
  })
})
