/**
 * E2E Tests: SDK Navigation
 *
 * Tests for navigating through the SDK workflow,
 * sidebar functionality, and step transitions.
 */

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

test.describe('SDK Navigation', () => {
  test('should display SDK dashboard', async ({ sdkPage }) => {
    await expect(sdkPage).toHaveURL(/\/sdk/)
  })

  test('should navigate to Use Case Workshop', async ({ sdkPage }) => {
    await sdkPage.click('text=Use Case Workshop')
    await expect(sdkPage).toHaveURL('/sdk/advisory-board')
  })

  test('should navigate through all Phase 1 steps', async ({ sdkPage }) => {
    const phase1Steps = [
      { id: 'use-case-workshop', url: '/sdk/advisory-board', name: 'Use Case Workshop' },
      { id: 'screening', url: '/sdk/screening', name: 'System Screening' },
      { id: 'modules', url: '/sdk/modules', name: 'Compliance Modules' },
      { id: 'requirements', url: '/sdk/requirements', name: 'Requirements' },
      { id: 'controls', url: '/sdk/controls', name: 'Controls' },
      { id: 'evidence', url: '/sdk/evidence', name: 'Evidence' },
      { id: 'audit-checklist', url: '/sdk/audit-checklist', name: 'Audit Checklist' },
      { id: 'risks', url: '/sdk/risks', name: 'Risk Matrix' },
    ]

    for (const step of phase1Steps) {
      await navigateToStep(sdkPage, step.id)
      await expect(sdkPage).toHaveURL(step.url)
      // Verify page loaded (no error state)
      await expect(sdkPage.locator('body')).not.toContainText('Error')
    }
  })

  test('should navigate through all Phase 2 steps', async ({ sdkPage }) => {
    const phase2Steps = [
      { id: 'ai-act', url: '/sdk/ai-act', name: 'AI Act Klassifizierung' },
      { id: 'obligations', url: '/sdk/obligations', name: 'Pflichtenübersicht' },
      { id: 'dsfa', url: '/sdk/dsfa', name: 'DSFA' },
      { id: 'tom', url: '/sdk/tom', name: 'TOMs' },
      { id: 'loeschfristen', url: '/sdk/loeschfristen', name: 'Löschfristen' },
      { id: 'vvt', url: '/sdk/vvt', name: 'Verarbeitungsverzeichnis' },
      { id: 'consent', url: '/sdk/consent', name: 'Rechtliche Vorlagen' },
      { id: 'cookie-banner', url: '/sdk/cookie-banner', name: 'Cookie Banner' },
      { id: 'einwilligungen', url: '/sdk/einwilligungen', name: 'Einwilligungen' },
      { id: 'dsr', url: '/sdk/dsr', name: 'DSR Portal' },
      { id: 'escalations', url: '/sdk/escalations', name: 'Escalations' },
    ]

    for (const step of phase2Steps) {
      await navigateToStep(sdkPage, step.id)
      await expect(sdkPage).toHaveURL(step.url)
      await expect(sdkPage.locator('body')).not.toContainText('Error')
    }
  })

  test('should use next/previous buttons for navigation', async ({ sdkPage }) => {
    // Start at Use Case Workshop
    await navigateToStep(sdkPage, 'use-case-workshop')

    // Check that prev button is disabled on first step
    const prevButton = sdkPage.locator(selectors.prevButton)
    if (await prevButton.isVisible()) {
      await expect(prevButton).toBeDisabled()
    }

    // Click next button
    const nextButton = sdkPage.locator(selectors.nextButton)
    if (await nextButton.isVisible()) {
      await nextButton.click()
      await expect(sdkPage).toHaveURL('/sdk/screening')
    }
  })

  test('sidebar should highlight current step', async ({ sdkPage }) => {
    await navigateToStep(sdkPage, 'requirements')

    // The current step in sidebar should have active styling
    const sidebarStep = sdkPage.locator(selectors.sidebarStep('requirements'))
    if (await sidebarStep.isVisible()) {
      await expect(sidebarStep).toHaveAttribute('aria-current', 'page')
    }
  })
})

test.describe('SDK Navigation with Demo Data', () => {
  test('should display progress bar with demo data', async ({ withDemoData }) => {
    const progressBar = withDemoData.locator(selectors.progressBar)
    if (await progressBar.isVisible()) {
      // Demo data has completed steps, so progress should be > 0
      const progressValue = await progressBar.getAttribute('aria-valuenow')
      expect(parseInt(progressValue || '0')).toBeGreaterThan(0)
    }
  })

  test('should show completed steps in sidebar', async ({ withDemoData }) => {
    // With demo data, some steps should be marked as completed
    const sidebar = withDemoData.locator(selectors.sidebar)
    if (await sidebar.isVisible()) {
      // Look for completed step indicators
      const completedSteps = sidebar.locator('[data-completed="true"]')
      const count = await completedSteps.count()
      expect(count).toBeGreaterThan(0)
    }
  })
})

test.describe('Deep Linking', () => {
  test('should load correct step from direct URL', async ({ page }) => {
    // Navigate directly to a specific step
    await page.goto('/sdk/risks')
    await page.waitForLoadState('networkidle')

    // Page should display Risk Matrix content
    await expect(page).toHaveURL('/sdk/risks')
  })

  test('should handle invalid step URL gracefully', async ({ page }) => {
    // Navigate to non-existent step
    await page.goto('/sdk/non-existent-step')
    await page.waitForLoadState('networkidle')

    // Should redirect to dashboard or show 404
    // Verify no crash
    await expect(page.locator('body')).not.toContainText('Unhandled')
  })
})
