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

test.use({ storageState: 'tests/e2e/.auth/user.json' })

test.describe('Keyvisual & Background Panels', () => {
  test('Settings page has Namenseinblender submenu and field', async ({ page }) => {
    await page.goto('/settings')
    await expect(page.getByTestId('settings-submenu-namenseinblender').first()).toBeVisible()
    await page.getByTestId('settings-submenu-namenseinblender').first().click()
    await expect(page.getByTestId('namenseinblender-macro')).toBeVisible()
  })

  test('service edit page shows keyvisual and background panels', async ({ page }) => {
    await page.goto('/services')

    // Find an edit link
    const editLink = page.getByRole('link', { name: /Bearbeiten/i }).first()
    const count = await page.getByRole('link', { name: /Bearbeiten/i }).count()
    if (count === 0) {
      // No services in DB — skip gracefully
      test.skip(true, 'No services available in test DB')
      return
    }

    await editLink.click()
    await page.waitForURL(/\/services\/\d+\/edit/)

    // Both panels must be visible
    await expect(page.getByTestId('keyvisual-panel')).toBeVisible()
    await expect(page.getByTestId('background-panel')).toBeVisible()
  })

  test('panels show upload inputs', async ({ page }) => {
    await page.goto('/services')
    const count = await page.getByRole('link', { name: /Bearbeiten/i }).count()
    if (count === 0) {
      test.skip(true, 'No services available in test DB')
      return
    }

    await page.getByRole('link', { name: /Bearbeiten/i }).first().click()
    await page.waitForURL(/\/services\/\d+\/edit/)

    // Upload inputs must exist (hidden but present in DOM)
    const kvInput = page.getByTestId('keyvisual-panel-upload-input')
    const bgInput = page.getByTestId('background-panel-upload-input')
    await expect(kvInput).toBeAttached()
    await expect(bgInput).toBeAttached()
  })

  test('panels show German labels', async ({ page }) => {
    await page.goto('/services')
    const count = await page.getByRole('link', { name: /Bearbeiten/i }).count()
    if (count === 0) {
      test.skip(true, 'No services available in test DB')
      return
    }

    await page.getByRole('link', { name: /Bearbeiten/i }).first().click()
    await page.waitForURL(/\/services\/\d+\/edit/)

    // German labels in panels
    await expect(page.getByTestId('keyvisual-panel').getByText('Keyvisual')).toBeVisible()
    await expect(page.getByTestId('background-panel').getByText('Hintergrundbild')).toBeVisible()
  })
})
