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

// AZ-470 — e2e companion for panel-width rehydration on reload (FT-P-38).
//
// FT-P-38 is the e2e-only AC for AZ-470 (the fast tests cover the debounce
// and body-shape ACs). This test will skip until production wires the
// rehydration path; today it captures the drift via `test.fail`.

test.describe('AZ-470 — panel-width rehydration (e2e companion)', () => {
  test.fail('AC-3 (FT-P-38) — rehydration on reload (drift — production has no writer)', async ({ page }) => {
    await page.goto('/annotations')
    const dividers = page.locator('div.cursor-col-resize')
    if ((await dividers.count().catch(() => 0)) === 0) {
      test.skip(true, 'No resizable divider rendered (annotations page not seeded?)')
    }
    // Capture initial widths (rendered defaults today).
    const panels = page.locator('div.bg-az-panel.shrink-0')
    const initialLeft = parseFloat(
      (await panels.first().evaluate((el: HTMLElement) => el.style.width)) || '0',
    )

    // Drag the left divider by +50 px.
    const divider = dividers.first()
    const box = await divider.boundingBox()
    if (!box) test.skip(true, 'Divider has no bounding box (display:none?)')
    await page.mouse.move(box!.x + box!.width / 2, box!.y + box!.height / 2)
    await page.mouse.down()
    await page.mouse.move(box!.x + box!.width / 2 + 50, box!.y + box!.height / 2)
    await page.mouse.up()

    // Reload — production has no PUT, so the new width is forgotten.
    await page.reload()
    await page.waitForLoadState('domcontentloaded')

    // Spec: rendered widths equal pre-reload widths within ± 1 px.
    const reloadedLeft = parseFloat(
      (await page.locator('div.bg-az-panel.shrink-0').first().evaluate(
        (el: HTMLElement) => el.style.width,
      )) || '0',
    )
    // Drift: reloadedLeft equals constructor default, NOT initialLeft+50.
    expect(Math.abs(reloadedLeft - (initialLeft + 50))).toBeLessThanOrEqual(1)
  })
})
