/**
 * E2E Tests: SDK Workflow
 *
 * Tests for completing the full compliance workflow,
 * including checkpoints and state persistence.
 */

import { test, expect, selectors, navigateToStep, waitForSDKInit } from '../fixtures/sdk-fixtures'

test.describe('Use Case Workshop', () => {
  test('can create a new use case', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'use-case-workshop')

    // Click add use case button
    const addButton = sdkPage.locator(selectors.addUseCaseButton)
    if (await addButton.isVisible()) {
      await addButton.click()

      // Fill in use case form
      const form = sdkPage.locator(selectors.useCaseForm)
      if (await form.isVisible()) {
        await sdkPage.fill('[name="name"]', 'E2E Test Use Case')
        await sdkPage.fill('[name="description"]', 'This is a test use case created by E2E tests')

        // Submit form
        await sdkPage.click('button[type="submit"]')

        // Verify use case was created
        await expect(sdkPage.locator('text=E2E Test Use Case')).toBeVisible()
      }
    }
  })

  test('displays use cases with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'use-case-workshop')

    // With demo data, should show existing use cases
    const useCaseCards = withDemoData.locator('[data-testid^="use-case-card"]')
    const count = await useCaseCards.count()
    expect(count).toBeGreaterThan(0)
  })
})

test.describe('Risk Matrix', () => {
  test('displays risk matrix', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'risks')

    // Page should load without errors
    await expect(sdkPage).toHaveURL('/sdk/risks')
  })

  test('shows risks with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'risks')

    // With demo data, should show risk cards
    const riskCards = withDemoData.locator('[data-testid^="risk-card"]')
    const count = await riskCards.count()
    expect(count).toBeGreaterThan(0)
  })

  test('can filter risks by severity', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'risks')

    // Look for severity filter
    const filterSelect = withDemoData.locator('[data-testid="severity-filter"]')
    if (await filterSelect.isVisible()) {
      await filterSelect.selectOption('CRITICAL')

      // Verify filtered results
      const riskCards = withDemoData.locator('[data-testid^="risk-card"]')
      const visibleCards = riskCards.filter({ hasText: 'CRITICAL' })
      const count = await visibleCards.count()
      expect(count).toBeGreaterThanOrEqual(0)
    }
  })
})

test.describe('Controls', () => {
  test('displays controls grid', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'controls')

    await expect(sdkPage).toHaveURL('/sdk/controls')
  })

  test('shows controls with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'controls')

    const controlCards = withDemoData.locator('[data-testid^="control-card"]')
    const count = await controlCards.count()
    expect(count).toBeGreaterThan(0)
  })

  test('can view control details', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'controls')

    // Click on first control card
    const firstControl = withDemoData.locator('[data-testid^="control-card"]').first()
    if (await firstControl.isVisible()) {
      await firstControl.click()

      // Should show control details (modal or expanded view)
      const detailView = withDemoData.locator('[data-testid="control-details"]')
      if (await detailView.isVisible({ timeout: 2000 })) {
        await expect(detailView).toBeVisible()
      }
    }
  })
})

test.describe('DSFA', () => {
  test('displays DSFA page', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'dsfa')

    await expect(sdkPage).toHaveURL('/sdk/dsfa')
  })

  test('shows DSFA content with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'dsfa')

    // Demo data includes a DSFA with sections
    const dsfaSections = withDemoData.locator('[data-testid^="dsfa-section"]')
    const count = await dsfaSections.count()
    expect(count).toBeGreaterThan(0)
  })
})

test.describe('TOMs', () => {
  test('displays TOMs page', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'tom')

    await expect(sdkPage).toHaveURL('/sdk/tom')
  })

  test('shows TOMs with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'tom')

    const tomCards = withDemoData.locator('[data-testid^="tom-card"]')
    const count = await tomCards.count()
    expect(count).toBeGreaterThan(0)
  })
})

test.describe('VVT (Verarbeitungsverzeichnis)', () => {
  test('displays VVT page', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'vvt')

    await expect(sdkPage).toHaveURL('/sdk/vvt')
  })

  test('shows processing activities with demo data', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'vvt')

    const activityCards = withDemoData.locator('[data-testid^="processing-activity"]')
    const count = await activityCards.count()
    expect(count).toBeGreaterThan(0)
  })
})

test.describe('Checkpoint Validation', () => {
  test('validates use case checkpoint', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'use-case-workshop')

    // Look for checkpoint validation button
    const validateButton = withDemoData.locator('[data-testid="validate-checkpoint"]')
    if (await validateButton.isVisible()) {
      await validateButton.click()

      // With demo data, checkpoint should pass
      const successIndicator = withDemoData.locator('[data-testid="checkpoint-passed"]')
      await expect(successIndicator).toBeVisible({ timeout: 5000 })
    }
  })

  test('shows checkpoint errors when requirements not met', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'use-case-workshop')

    // Without demo data, checkpoint should fail
    const validateButton = sdkPage.locator('[data-testid="validate-checkpoint"]')
    if (await validateButton.isVisible()) {
      await validateButton.click()

      // Should show error
      const errorIndicator = sdkPage.locator('[data-testid="checkpoint-failed"]')
      if (await errorIndicator.isVisible({ timeout: 5000 })) {
        await expect(errorIndicator).toBeVisible()
      }
    }
  })
})

test.describe('State Persistence', () => {
  test('persists state after page reload', async ({ withDemoData }) => {
    await navigateToStep(withDemoData, 'use-case-workshop')

    // Get current use case count
    const useCaseCards = withDemoData.locator('[data-testid^="use-case-card"]')
    const initialCount = await useCaseCards.count()

    // Reload page
    await withDemoData.reload()
    await waitForSDKInit(withDemoData)

    // Count should be same after reload
    const afterReloadCards = withDemoData.locator('[data-testid^="use-case-card"]')
    const afterReloadCount = await afterReloadCards.count()

    expect(afterReloadCount).toBe(initialCount)
  })
})
