import { test, expect } from '@playwright/test'

/**
 * E2E Tests for /korrektur
 *
 * Tests the Korrekturplattform main page:
 * - Page loads correctly
 * - Stats cards are displayed
 * - Klausur cards work
 * - Quick actions work
 * - Modals work
 */

test.describe('Korrektur Hauptseite', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/korrektur')
    await page.waitForLoadState('networkidle')
  })

  // ==========================================================================
  // PAGE LOAD TESTS
  // ==========================================================================

  test('page loads with correct title', async ({ page }) => {
    await expect(page.getByRole('heading', { name: 'Korrekturplattform' })).toBeVisible()
  })

  test('page shows subtitle', async ({ page }) => {
    await expect(page.getByText('KI-gestuetzte Abiturklausur-Korrektur')).toBeVisible()
  })

  // ==========================================================================
  // STATS CARDS TESTS
  // ==========================================================================

  test('stats cards are displayed', async ({ page }) => {
    await page.waitForTimeout(500)

    // Check for stat labels
    await expect(page.getByText('Offene Korrekturen')).toBeVisible()
    await expect(page.getByText('Erledigt (Woche)')).toBeVisible()
    await expect(page.getByText('Durchschnitt')).toBeVisible()
    await expect(page.getByText('Zeit gespart')).toBeVisible()
  })

  // ==========================================================================
  // KLAUSUREN SECTION TESTS
  // ==========================================================================

  test('Klausuren section header is visible', async ({ page }) => {
    await page.waitForTimeout(500)
    await expect(page.getByRole('heading', { name: 'Klausuren' })).toBeVisible()
  })

  test('Neue Klausur card is visible', async ({ page }) => {
    await page.waitForTimeout(600)
    await expect(page.getByText('Neue Klausur')).toBeVisible()
  })

  test('clicking Neue Klausur opens modal', async ({ page }) => {
    await page.waitForTimeout(600)

    // Click on Neue Klausur card
    await page.getByText('Neue Klausur').click()

    // Modal should open
    await expect(page.getByText('Neue Klausur erstellen')).toBeVisible()
  })

  // ==========================================================================
  // CREATE KLAUSUR MODAL TESTS
  // ==========================================================================

  test('create modal has required fields', async ({ page }) => {
    await page.waitForTimeout(600)

    // Open modal
    await page.getByText('Neue Klausur').click()

    // Check for form fields
    await expect(page.getByLabel('Titel')).toBeVisible()
    await expect(page.getByLabel('Fach')).toBeVisible()
    await expect(page.getByLabel('Jahr')).toBeVisible()
    await expect(page.getByLabel('Semester')).toBeVisible()
    await expect(page.getByLabel('Modus')).toBeVisible()
  })

  test('create modal has submit button', async ({ page }) => {
    await page.waitForTimeout(600)

    // Open modal
    await page.getByText('Neue Klausur').click()

    await expect(page.getByRole('button', { name: 'Erstellen' })).toBeVisible()
  })

  test('create modal cancel closes modal', async ({ page }) => {
    await page.waitForTimeout(600)

    // Open modal
    await page.getByText('Neue Klausur').click()
    await expect(page.getByText('Neue Klausur erstellen')).toBeVisible()

    // Cancel
    await page.getByRole('button', { name: 'Abbrechen' }).click()

    // Modal should be closed
    await expect(page.getByText('Neue Klausur erstellen')).not.toBeVisible()
  })

  // ==========================================================================
  // QUICK ACTIONS TESTS
  // ==========================================================================

  test('Schnellaktionen section is visible', async ({ page }) => {
    await page.waitForTimeout(700)
    await expect(page.getByRole('heading', { name: 'Schnellaktionen' })).toBeVisible()
  })

  test('QR Upload quick action is visible', async ({ page }) => {
    await page.waitForTimeout(800)
    await expect(page.getByText('QR Upload')).toBeVisible()
  })

  test('Direkt hochladen quick action is visible', async ({ page }) => {
    await page.waitForTimeout(800)
    await expect(page.getByText('Direkt hochladen')).toBeVisible()
  })

  test('Schnellstart quick action is visible', async ({ page }) => {
    await page.waitForTimeout(800)
    await expect(page.getByText('Schnellstart')).toBeVisible()
  })

  test('EH hochladen quick action is visible', async ({ page }) => {
    await page.waitForTimeout(900)
    await expect(page.getByText('EH hochladen')).toBeVisible()
  })

  test('Abitur-Archiv quick action is visible', async ({ page }) => {
    await page.waitForTimeout(900)
    await expect(page.getByText('Abitur-Archiv')).toBeVisible()
  })

  test('clicking Abitur-Archiv navigates to archiv', async ({ page }) => {
    await page.waitForTimeout(900)

    // Click on Abitur-Archiv
    await page.getByText('Abitur-Archiv').click()

    // Should navigate to archiv
    await expect(page).toHaveURL(/\/korrektur\/archiv/)
  })

  // ==========================================================================
  // DIRECT UPLOAD MODAL TESTS
  // ==========================================================================

  test('clicking Direkt hochladen opens upload modal', async ({ page }) => {
    await page.waitForTimeout(800)

    await page.getByText('Direkt hochladen').click()

    // Modal should show - use heading selector to avoid matching button
    await expect(page.getByRole('heading', { name: 'Arbeiten hochladen' })).toBeVisible()
  })

  test('upload modal has drag drop zone', async ({ page }) => {
    await page.waitForTimeout(800)

    await page.getByText('Direkt hochladen').click()

    await expect(page.getByText('Dateien hierher ziehen')).toBeVisible()
  })

  test('upload modal cancel closes modal', async ({ page }) => {
    await page.waitForTimeout(800)

    await page.getByText('Direkt hochladen').click()
    await expect(page.getByRole('heading', { name: 'Arbeiten hochladen' })).toBeVisible()

    await page.getByRole('button', { name: 'Abbrechen' }).click()

    await expect(page.getByRole('heading', { name: 'Arbeiten hochladen' })).not.toBeVisible()
  })

  // ==========================================================================
  // EH UPLOAD MODAL TESTS
  // ==========================================================================

  test('clicking EH hochladen opens EH modal', async ({ page }) => {
    await page.waitForTimeout(900)

    await page.getByText('EH hochladen').click()

    await expect(page.getByText('Erwartungshorizont hochladen')).toBeVisible()
  })

  test('EH modal has file drop zone', async ({ page }) => {
    await page.waitForTimeout(900)

    await page.getByText('EH hochladen').click()

    await expect(page.getByText('EH-Datei hierher ziehen')).toBeVisible()
  })

  // ==========================================================================
  // THEME TOGGLE TESTS
  // ==========================================================================

  test('theme toggle is visible', async ({ page }) => {
    // Theme toggle should be in header
    const themeToggle = page.locator('[class*="ThemeToggle"], button').filter({ has: page.locator('svg') }).first()
    await expect(themeToggle).toBeVisible()
  })

  // ==========================================================================
  // SIDEBAR TESTS
  // ==========================================================================

  test('sidebar is visible', async ({ page }) => {
    // Sidebar should be present
    const sidebar = page.locator('aside')
    await expect(sidebar).toBeVisible()
  })

  test('sidebar has BreakPilot logo', async ({ page }) => {
    await expect(page.getByText('BreakPilot')).toBeVisible()
  })
})
