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

const BASE = 'https://macmini:3007'
const PROJECT_ID = 'bb7d5b88-469d-401f-a0e3-ae5b867e4a1c' // Kniehebelpresse HP-500

async function dismissBanner(page: import('@playwright/test').Page) {
  for (let i = 0; i < 3; i++) {
    try {
      const btn = page.locator('button', { hasText: 'Nur notwendige Cookies' })
      if (await btn.isVisible({ timeout: 2000 })) { await btn.click({ force: true }); await page.waitForTimeout(800) }
      else break
    } catch { break }
  }
}

test.describe('IACE Project — All Tabs', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}`, { waitUntil: 'domcontentloaded' })
    await dismissBanner(page)
  })

  test('Overview page loads without error', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}`, { waitUntil: 'domcontentloaded' })
    await dismissBanner(page)
    await page.waitForTimeout(2000)
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
    // Should show project name in h1 or sidebar
    await expect(page.locator('text=Kniehebelpresse').first()).toBeVisible({ timeout: 15000 })
  })

  test('Components tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/components`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Classification tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/classification`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Hazards tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/hazards`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Mitigations tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/mitigations`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Verification tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/verification`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Evidence tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/evidence`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Tech-File tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/tech-file`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Monitoring tab loads', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/monitoring`, { waitUntil: 'domcontentloaded' })
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
  })

  test('Interview page loads — Grenzen & Verwendung form', async ({ page }) => {
    await page.goto(`${BASE}/sdk/iace/${PROJECT_ID}/interview`, { waitUntil: 'domcontentloaded' })
    await dismissBanner(page)
    await page.waitForTimeout(2000)
    const body = await page.textContent('body')
    expect(body).not.toContain('Application error')
    // Interview was redesigned to a form-based flow (Grenzen & Verwendung)
    await expect(page.locator('text=Grenzen').first()).toBeVisible({ timeout: 10000 })
  })
})
