/**
 * Tests fuer Role-Select Landing Page
 * 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
const mockPush = jest.fn()
jest.mock('next/navigation', () => ({
  useRouter: () => ({
    push: mockPush,
  }),
}))

// Mock localStorage
const localStorageMock = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
}
Object.defineProperty(window, 'localStorage', { value: localStorageMock })

// Import nach Mocks
import RoleSelectPage from '@/app/admin/compliance/role-select/page'

describe('RoleSelectPage', () => {
  beforeEach(() => {
    jest.clearAllMocks()
    localStorageMock.getItem.mockReturnValue(null)
  })

  describe('Rendering', () => {
    it('rendert alle 4 Rollen-Karten', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText('Executive')).toBeInTheDocument()
      expect(screen.getByText('Auditor')).toBeInTheDocument()
      expect(screen.getByText('Compliance Officer')).toBeInTheDocument()
      expect(screen.getByText(/Entwickler|Developer/)).toBeInTheDocument()
    })

    it('rendert Seitentitel', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Compliance Portal/i)).toBeInTheDocument()
    })

    it('zeigt Quick-Access Links', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Controls/i)).toBeInTheDocument()
      expect(screen.getByText(/Evidence/i)).toBeInTheDocument()
      expect(screen.getByText(/Risks|Risiken/i)).toBeInTheDocument()
    })
  })

  describe('Rollen-Karten Klick', () => {
    it('navigiert zu Executive Dashboard bei Klick auf Executive', () => {
      render(<RoleSelectPage />)

      const executiveCard = screen.getByText('Executive').closest('button')
      fireEvent.click(executiveCard!)

      expect(mockPush).toHaveBeenCalledWith('/admin/compliance?tab=executive')
    })

    it('navigiert zu Audit Workspace bei Klick auf Auditor', () => {
      render(<RoleSelectPage />)

      const auditorCard = screen.getByText('Auditor').closest('button')
      fireEvent.click(auditorCard!)

      expect(mockPush).toHaveBeenCalledWith('/admin/compliance/audit-workspace')
    })

    it('navigiert zum Compliance Dashboard bei Klick auf Compliance Officer', () => {
      render(<RoleSelectPage />)

      const complianceCard = screen.getByText('Compliance Officer').closest('button')
      fireEvent.click(complianceCard!)

      expect(mockPush).toHaveBeenCalledWith('/admin/compliance')
    })

    it('navigiert zu technischer Ansicht bei Klick auf Developer', () => {
      render(<RoleSelectPage />)

      const developerCard = screen.getByText(/Entwickler|Developer/).closest('button')
      fireEvent.click(developerCard!)

      expect(mockPush).toHaveBeenCalledWith('/admin/compliance?tab=technisch')
    })
  })

  describe('Sprachumschaltung', () => {
    it('zeigt deutschen Text als Standard', () => {
      localStorageMock.getItem.mockReturnValue(null)
      render(<RoleSelectPage />)

      // Sollte standardmaessig Deutsch sein
      expect(screen.getByText(/Wählen Sie Ihre Ansicht|Waehlen Sie Ihre Ansicht/i)).toBeInTheDocument()
    })

    it('speichert Sprachpräferenz in localStorage', () => {
      render(<RoleSelectPage />)

      // Finde Language Toggle und klicke
      const langToggle = screen.getByRole('button', { name: /EN/i })
      fireEvent.click(langToggle)

      expect(localStorageMock.setItem).toHaveBeenCalledWith('compliance-language', 'en')
    })

    it('laedt Sprachpräferenz aus localStorage', () => {
      localStorageMock.getItem.mockReturnValue('en')
      render(<RoleSelectPage />)

      expect(screen.getByText('Choose your view')).toBeInTheDocument()
    })
  })

  describe('Rollenbeschreibungen', () => {
    it('zeigt Executive Beschreibung', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Ampel-Status|Traffic Light/i)).toBeInTheDocument()
      expect(screen.getByText(/Top-Risiken|Top Risks/i)).toBeInTheDocument()
    })

    it('zeigt Auditor Beschreibung', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Checkliste|Checklist/i)).toBeInTheDocument()
      expect(screen.getByText(/Sign-off/i)).toBeInTheDocument()
    })

    it('zeigt Compliance Officer Beschreibung', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Workflows/i)).toBeInTheDocument()
    })

    it('zeigt Developer Beschreibung', () => {
      render(<RoleSelectPage />)

      expect(screen.getByText(/Code-Referenzen|Code Refs/i)).toBeInTheDocument()
    })
  })

  describe('Praeferenz-Speicherung', () => {
    it('speichert letzte Rollenauswahl', () => {
      render(<RoleSelectPage />)

      const executiveCard = screen.getByText('Executive').closest('button')
      fireEvent.click(executiveCard!)

      expect(localStorageMock.setItem).toHaveBeenCalledWith('compliance-last-role', 'executive')
    })
  })
})
