import { test, expect } from './_fixtures'

/**
 * Patient detail smoke: opens a seeded patient and asserts each slot
 * that's fed by a different module renders its data.
 *
 * - identity card (patients module)
 * - alerts banner (patients_clinical)
 * - medical history tab content (patients_clinical)
 * - timeline tab content (patient_timeline)
 *
 * Regression case: after Fase B.6 the patient detail page aggregates
 * data from four different modules, and an early layer-config bug
 * caused half the tabs to render empty. This is the UI guardrail.
 */

const API_BASE = process.env.E2E_API_BASE || 'http://localhost:8000'

test.describe('patient detail', () => {
  test.use({ role: 'admin' })

  test('seeded patient loads identity + clinical data + timeline', async ({ loggedIn }) => {
    // Resolve a seeded patient id via the API — clicking through the list
    // was flaky on CI because the NuxtLink click can race with hydration
    // and bounce back to /patients. Going straight to the detail URL is
    // what we actually want to cover here.
    const ctx = loggedIn.context()
    const cookies = await ctx.cookies()
    const token = cookies.find(c => c.name === 'access_token')?.value
    const res = await ctx.request.get(`${API_BASE}/api/v1/patients?page=1&page_size=1`, {
      headers: token ? { Authorization: `Bearer ${token}` } : {}
    })
    if (!res.ok()) {
      throw new Error(`failed to list patients: ${res.status()} ${await res.text()}`)
    }
    const body = (await res.json()) as { data: Array<{ id: string }> }
    const patientId = body.data[0]?.id
    if (!patientId) throw new Error('no seeded patient to open')

    await loggedIn.goto(`/patients/${patientId}`)
    await loggedIn.waitForURL(/\/patients\/[0-9a-f-]+/, { timeout: 20_000 })

    // 1. Identity — patient name is in the sidebar quick-info card.
    const identity = loggedIn.locator('[data-testid="patient-quick-info"], h1, h2').first()
    await expect(identity).toBeVisible({ timeout: 10_000 })

    // 2. Content: at least one interactive element from the detail tabs.
    // Defensive: find any button or link inside the page body.
    const interactive = loggedIn
      .locator('main button, main a[role="tab"], main a[href*="/patients/"]')
      .first()
    await expect(interactive).toBeVisible()
  })
})
