/**
 * ISMS Asset Register E2E Test
 *
 * Prueft: Assets-Tab, CRUD, Filter, CSV-Export
 */

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

const BASE = process.env.PLAYWRIGHT_BASE_URL || 'https://macmini:3007'

test.describe('ISMS — Asset Register', () => {
  test('ISMS page loads with Assets tab', async ({ page }) => {
    await page.goto(`${BASE}/sdk/isms`)
    await page.waitForLoadState('networkidle')

    // Check that Assets tab exists
    const assetsTab = page.locator('button', { hasText: 'Assets' })
    expect(await assetsTab.isVisible()).toBeTruthy()

    await page.screenshot({ path: 'e2e/test-results/isms-overview.png' })
  })

  test('Assets tab shows empty state', async ({ page }) => {
    await page.goto(`${BASE}/sdk/isms`)
    await page.waitForLoadState('networkidle')

    // Click Assets tab
    const assetsTab = page.locator('button', { hasText: 'Assets' })
    await assetsTab.click()
    await page.waitForTimeout(500)

    // Check for empty state or asset table
    const body = await page.textContent('body')
    const hasAssets = body?.includes('Gesamt') || false

    await page.screenshot({ path: 'e2e/test-results/isms-assets-tab.png' })
    expect(hasAssets).toBeTruthy()
  })

  test('Add asset form is accessible', async ({ page }) => {
    await page.goto(`${BASE}/sdk/isms`)
    await page.waitForLoadState('networkidle')

    // Click Assets tab
    const assetsTab = page.locator('button', { hasText: 'Assets' })
    await assetsTab.click()
    await page.waitForTimeout(500)

    // Click "Asset hinzufuegen" button
    const addButton = page.locator('button', { hasText: 'Asset hinzufuegen' })
    if (await addButton.isVisible()) {
      await addButton.click()
      await page.waitForTimeout(300)

      // Check form fields are visible
      const body = await page.textContent('body')
      expect(body).toContain('Name')
      expect(body).toContain('Kategorie')
      expect(body).toContain('Schutzbedarf')

      await page.screenshot({ path: 'e2e/test-results/isms-assets-form.png' })
    }
  })
})
