/** QA-1: Zucht (FEAT-3) — Wurf anlegen, Detail, Jungtier erfassen. */
import { de, expect, gotoSection, skipUnlessMock, test, uniqueName } from './fixtures'

const t = de.pages.litters
const tg = de.pages.gerbils

test('Wurf anlegen — Abgabedatum wird mit +35 Tagen vorgeschlagen', async ({ page }) => {
  await gotoSection(page, de.nav.litters)
  await page.getByRole('link', { name: t.newButton }).click()
  await expect(page.getByRole('heading', { name: t.form.createTitle })).toBeVisible()

  // Pflichtfeld-Validierung
  await page.getByRole('button', { name: t.form.save, exact: true }).click()
  await expect(page.getByText(t.validation.nameRequired)).toBeVisible()
  await expect(page.getByText(t.validation.dateRequired)).toBeVisible()

  const name = uniqueName('Wurf')
  await page.getByLabel(`${t.fields.name} *`).fill(name)
  await page.getByLabel(`${t.fields.date} *`).fill('2026-01-01')
  // Vorschlag: Wurfdatum + 35 Tage
  await expect(page.getByLabel(t.fields.expectedGoHomeDate)).toHaveValue('2026-02-05')
  await page.getByRole('button', { name: t.form.save, exact: true }).click()

  await expect(page.getByRole('heading', { name })).toBeVisible()
  await expect(page.getByText(t.detail.parents)).toBeVisible()
})

test('Wurf-Detail zeigt Elterntiere und erwartete Farbschläge', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/wuerfe/w-kruemel')
  await expect(page.getByRole('heading', { name: 'Wurf K' })).toBeVisible()
  await expect(page.getByRole('link', { name: 'Fridolin' })).toBeVisible()
  await expect(page.getByRole('link', { name: 'Luna' })).toBeVisible()
  // Beide Eltern haben Genotypen -> Farbvorhersage rendert
  await expect(page.getByText(t.detail.expectedColors).first()).toBeVisible()
})

test('Jungtier erfassen — Formular ist mit Wurf + Geburtsdatum vorbelegt', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/wuerfe/w-kruemel')
  await page.getByRole('link', { name: t.detail.registerJuvenile }).click()
  await expect(page.getByRole('heading', { name: tg.form.createTitle })).toBeVisible()

  // Vorbelegung aus ?litterId=&dob=
  await expect(page.getByLabel(tg.fields.litter)).toHaveValue('w-kruemel')
  await expect(page.getByLabel(tg.fields.dateOfBirth)).toHaveValue('2025-03-12')

  const name = uniqueName('Jungtier')
  await page.getByLabel(`${tg.fields.name} *`).fill(name)
  await page.getByLabel(`${tg.fields.gender} *`).selectOption({ label: tg.genderLabels.male })
  await page.getByRole('button', { name: tg.form.save, exact: true }).click()
  await expect(page.getByRole('heading', { name })).toBeVisible()

  // Das Jungtier hängt am Wurf
  await expect(page.getByText('Wurf K')).toBeVisible()

  // ... und erscheint auf der Wurf-Detailseite unter „Jungtiere“
  await page.goto('/wuerfe/w-kruemel')
  await expect(page.getByRole('link', { name: new RegExp(name) })).toBeVisible()
})
