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

test('accepted report is adjudicated before the merit node', async ({ page }) => {
  const cutsceneState = {
    playthrough: { id: 'adjudication-playthrough', mysterySlug: 'barricelli-files', levelSlug: 'completed-level', status: 'active' },
    node: {
      id: 'adjudication-node', kind: 'cutscene', label: 'Scene 8 · Case adjudication', componentKey: 'case-adjudication',
      presentation: {
        kind: 'case-adjudication', reportTitle: 'Barricelli Inventor Finding', investigatorName: 'Test Investigator',
        submittedAt: '2026-08-23T12:00:00.000Z', finding: 'SUPPORTED BY THE SUBMITTED EVIDENCE', stampText: 'CASE VERIFIED',
        claims: [{ statement: 'Nils Aall Barricelli was an inventor.', evidence: [{
          displayNumber: 1, title: 'GB695913A Improved chest of drawers', fileType: 'image',
          relationText: 'Proves that Barricelli filed a patent.', publishedAt: '1953-08-19T00:00:00.000Z',
          sourceCitation: 'Google Patents', sourceUri: 'https://patents.google.com/patent/GB695913A/en',
        }] }],
      },
    },
  }
  await page.route('**/api/playthroughs/current', route => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(cutsceneState) }))
  await page.route('**/api/playthroughs/adjudication-playthrough/advance', route => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({
    playthrough: cutsceneState.playthrough,
    node: { id: 'merit-node', kind: 'merit', label: 'The Barricelli Luggage', awardsFlag: 'barricelli_luggage' },
  }) }))

  await page.goto('/?resume=1')
  const adjudication = page.getByRole('dialog', { name: 'Case adjudication' })
  await expect(adjudication).toContainText('Nils Aall Barricelli was an inventor.')
  await expect(adjudication).toContainText('EXHIBIT 1 — ACCEPTED')
  await expect(adjudication).toContainText('Google Patents')
  await expect(adjudication).toContainText('SUPPORTED BY THE SUBMITTED EVIDENCE', { timeout: 5_000 })
  await expect(adjudication).toHaveClass(/phase-stamped/, { timeout: 5_000 })
  expect(await adjudication.locator('.adjudication-stamp').evaluate(element => Number.parseFloat(getComputedStyle(element).opacity))).toBeGreaterThan(0)

  await page.getByRole('button', { name: 'RECEIVE MERIT' }).click()
  await expect(page.getByText('MERIT AWARDED', { exact: false })).toBeVisible()
  await expect(page.getByRole('heading', { name: 'The Barricelli Luggage' })).toBeVisible()
})
