/** QA-1: Tiere (Rennmäuse) — Liste/Suche, CRUD, Detail-Tabs (FEAT-1 + FEAT-6). */
import { de, expect, gotoSection, skipUnlessMock, test, uniqueName } from './fixtures'

const t = de.pages.gerbils
const tabs = de.pages.tierTabs

test('Liste lädt und die Namenssuche filtert', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/rennmaeuse')
  await expect(page.getByRole('link', { name: /Krümel/ })).toBeVisible()
  await expect(page.getByRole('link', { name: /Fridolin/ })).toBeVisible()

  await page.getByPlaceholder(t.searchPlaceholder).first().fill('Krüm')
  await expect(page.getByRole('link', { name: /Krümel/ })).toBeVisible()
  await expect(page.getByRole('link', { name: /Fridolin/ })).toBeHidden()
})

test('Neue Rennmaus anlegen — Validierung + Erfolg', async ({ page }) => {
  await gotoSection(page, de.nav.gerbils)
  await page.getByRole('link', { name: t.newButton }).click()
  await expect(page.getByRole('heading', { name: t.form.createTitle })).toBeVisible()

  // Pflichtfeld-Validierung auf Deutsch
  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.genderRequired)).toBeVisible()

  const name = uniqueName('Testmaus')
  await page.getByLabel(`${t.fields.name} *`).fill(name)
  await page.getByLabel(`${t.fields.gender} *`).selectOption({ label: t.genderLabels.female })
  await page.getByRole('button', { name: t.form.save, exact: true }).click()

  // Erfolg: Detailseite des neuen Tieres
  await expect(page.getByRole('heading', { name })).toBeVisible()
  await expect(page.getByText(t.detail.masterData)).toBeVisible()
})

test('Detailseite zeigt Stammdaten + Tab-Inhalte (Gesundheit/Gewicht/Fotos)', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/rennmaeuse/kruemel')
  await expect(page.getByRole('heading', { name: 'Krümel' })).toBeVisible()
  await expect(page.getByText(t.detail.masterData)).toBeVisible()

  // Fotos (Standard-Tab): leerer Zustand + Datei-Pflicht-Validierung
  await expect(page.getByText(tabs.photos.empty)).toBeVisible()
  await page.getByRole('button', { name: tabs.photos.uploadButton }).click()
  await expect(page.getByText(tabs.photos.validation.fileRequired)).toBeVisible()

  // Gesundheit: Seed-Eintrag sichtbar, neuen Eintrag anlegen
  await page.getByRole('tab', { name: t.detail.tabs.health }).click()
  await expect(page.getByText('Jahresimpfung')).toBeVisible()
  await expect(page.getByText(tabs.health.typeLabels.Vaccination, { exact: true })).toBeVisible()
  await page.getByRole('button', { name: tabs.health.newButton }).click()
  // 'Art' exakt ansteuern ('Tierarzt' enthält 'arzt'; Label-Text enthält die Optionen)
  await page
    .locator('label.field', { has: page.locator(`span:text-is("${tabs.health.fields.type}")`) })
    .locator('select')
    .selectOption({ label: tabs.health.typeLabels.Examination })
  await page.getByLabel(`${tabs.health.fields.description} *`).fill('Routinekontrolle, alles gut')
  await page.getByRole('button', { name: tabs.actions.save, exact: true }).click()
  await expect(page.getByText('Routinekontrolle, alles gut')).toBeVisible()

  // Gewicht: Verlaufskurve (2 Seed-Einträge) + Schnell-Erfassung
  await page.getByRole('tab', { name: t.detail.tabs.weight }).click()
  await expect(page.locator('svg.line-chart')).toBeVisible()
  await expect(page.getByText(/78\s*g/)).toBeVisible()
  await page.getByLabel(tabs.weight.fields.weightGrams).fill('85')
  await page.getByRole('button', { name: tabs.weight.addButton, exact: true }).click()
  await expect(page.getByText(/85\s*g/)).toBeVisible()
})

test('Tier bearbeiten — Notizen ändern', async ({ page }) => {
  skipUnlessMock()
  await page.goto('/rennmaeuse/kruemel/bearbeiten')
  await expect(page.getByRole('heading', { name: t.form.editTitle })).toBeVisible()
  await page.getByLabel(t.fields.notes).fill('Sehr zutraulich.')
  await page.getByRole('button', { name: t.form.save, exact: true }).click()
  await expect(page.getByRole('heading', { name: 'Krümel' })).toBeVisible()
  await expect(page.getByText('Sehr zutraulich.')).toBeVisible()
})
