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

test.describe('Cases', () => {
  test.beforeEach(async ({ page }) => {
    // Log in before each test
    await page.goto('/login')
    await page.getByLabel('E-Mail').fill('admin@dak-portal.de')
    await page.getByLabel('Passwort').fill('admin123')
    await page.getByRole('button', { name: 'Anmelden' }).click()
    await expect(page).toHaveURL(/\/dashboard/)

    // Navigate to cases page
    await page.getByRole('link', { name: /fälle/i }).click()
    await expect(page).toHaveURL(/\/cases/)
  })

  test('case list table is visible', async ({ page }) => {
    // Table headers should be visible
    await expect(page.getByRole('columnheader', { name: 'Fall-ID' })).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'Datum' })).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'KVNR' })).toBeVisible()
    await expect(page.getByRole('columnheader', { name: 'Fallgruppe' })).toBeVisible()
  })

  test('search input filters cases', async ({ page }) => {
    // The search input should be present
    const searchInput = page.getByPlaceholder(/suche/i)
    await expect(searchInput).toBeVisible()

    // Type a search query
    await searchInput.fill('onko')

    // Wait for debounce and results to update
    await page.waitForTimeout(500)

    // The page should still show the cases heading
    await expect(page.getByRole('heading', { name: 'Fälle' })).toBeVisible()
  })

  test('clicking a row opens detail sheet', async ({ page }) => {
    // Wait for table rows to appear
    const firstRow = page.locator('tbody tr').first()
    await expect(firstRow).toBeVisible()

    // Click the first row
    await firstRow.click()

    // The detail sheet should open with case information
    await expect(page.getByText('Fall-ID')).toBeVisible()
    await expect(page.getByRole('button', { name: /bearbeiten/i })).toBeVisible()
  })
})
