/** LITTER-MORTALITY: deathsWithin8Weeks im Wurf-Formular und -Detail. */
import { de, expect, skipUnlessMock, test, uniqueName } from './fixtures'

const t = de.pages.litters

test('Wurf-Detail zeigt Frühverluste wenn gesetzt', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/wuerfe/w-kruemel')
  await expect(page.getByRole('heading', { name: 'Wurf K' })).toBeVisible()
  // Frühverluste-Zeile im def-list: Label + Wert '1'
  const mortalityRow = page.locator('.def-row', {
    has: page.locator('dt', { hasText: t.fields.deathsWithin8Weeks }),
  })
  await expect(mortalityRow).toBeVisible()
  await expect(mortalityRow.locator('dd')).toHaveText('1')
})

test('Wurf-Formular speichert Frühverluste und zeigt sie im Detail', async ({ page }) => {
  skipUnlessMock()
  const name = uniqueName('WurfM')
  await page.goto('/wuerfe/neu')
  await page.getByLabel(`${t.fields.name} *`).fill(name)
  await page.getByLabel(`${t.fields.date} *`).fill('2026-01-10')

  // Frühverluste eingeben
  await page.getByLabel(t.fields.deathsWithin8Weeks).fill('2')
  await expect(page.getByText(t.validation.deathsExceedTotalBorn)).not.toBeVisible()

  await page.getByRole('button', { name: t.form.save, exact: true }).click()
  await expect(page.getByRole('heading', { name })).toBeVisible()
  // Frühverluste-Zeile im Detail
  const mortalityRow = page.locator('.def-row', {
    has: page.locator('dt', { hasText: t.fields.deathsWithin8Weeks }),
  })
  await expect(mortalityRow).toBeVisible()
  await expect(mortalityRow.locator('dd')).toHaveText('2')
})

test('Frühverluste > Wurfstärke zeigt Warnmeldung', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/wuerfe/neu')

  await page.getByLabel(t.fields.totalBorn).fill('3')
  await page.getByLabel(t.fields.deathsWithin8Weeks).fill('5')
  await expect(page.getByText(t.validation.deathsExceedTotalBorn)).toBeVisible()
})
