import { expect, test } from './fixtures.js'

test('boots into the shell with the Focus screen', async ({ app, window }) => {
  await app.expectLoaded()
  await expect(window.getByText('Reginald', { exact: true })).toBeVisible()
  await expect(window.getByRole('heading', { name: 'Morning service' })).toBeVisible()
  await app.screenshot('shell-light')
})

test('Focus shows Now/Next/Later cards and the burn-up cone', async ({ window }) => {
  for (const slot of ['Now', 'Next', 'Later']) {
    await expect(window.getByText(slot, { exact: true })).toBeVisible()
  }
  await expect(window.getByText('Fix lifecycle inference on merge events')).toBeVisible()
  await expect(window.getByText(/scope · 42 issues/)).toBeVisible()
})

test('Standup renders the drift / plan / nag letter', async ({ app, window }) => {
  await app.nav('Standup').click()
  await expect(window.getByRole('heading', { name: 'Morning standup' })).toBeVisible()
  await expect(window.getByText('Overnight drift')).toBeVisible()
  await expect(window.getByText('Stephen', { exact: true })).toBeVisible()
  await app.screenshot('standup')
})

test('Board: columns, search filter, and Gantt/Deps tabs', async ({ app, window }) => {
  await app.nav('The pot').click()
  await expect(window.getByRole('heading', { name: 'The pot' })).toBeVisible()
  await expect(window.getByText('Diagnosis', { exact: true })).toBeVisible()
  await expect(window.getByText('Steeping', { exact: true })).toBeVisible()
  await app.screenshot('board')

  await window.getByPlaceholder(/Search the pot/).fill('lifecycle')
  await expect(window.getByText('Fix lifecycle inference on merge events')).toBeVisible()
  await expect(window.getByText('Dark theme: cone fill too faint')).toBeHidden()
  await window.getByPlaceholder(/Search the pot/).fill('')

  await window.getByRole('tab', { name: 'Gantt' }).click()
  await expect(window.getByText(/80% · Mar 3–12/)).toBeVisible()
  await app.screenshot('gantt')

  await window.getByRole('tab', { name: 'Dependencies' }).click()
  await expect(window.getByText('critical path', { exact: true })).toBeVisible()
  await app.screenshot('deps')
})

test('Runway: milestones, capacity, and drill-ins', async ({ app, window }) => {
  await app.nav('Runway').click()
  await expect(window.getByRole('heading', { name: 'Runway' })).toBeVisible()
  await expect(window.getByText('Pilot-ready')).toBeVisible()
  await expect(window.getByText('deadline/hard')).toBeVisible()
  await app.screenshot('runway')

  // milestone drill-in
  await window.getByText('Beta', { exact: true }).click()
  await expect(window.getByRole('heading', { name: 'Beta' })).toBeVisible()
  await expect(window.getByText('42 issues · est 61d')).toBeVisible()
  await app.screenshot('milestone')
  await window.getByRole('main').getByRole('button', { name: 'Runway' }).click()

  // calibration drill-in
  await window.getByRole('button', { name: 'Full report' }).click()
  await expect(window.getByRole('heading', { name: 'Calibration' })).toBeVisible()
  await expect(window.getByText('The shape of hope')).toBeVisible()
  await app.screenshot('calibration')
})

test('Inbox: notifications and mark-all-read', async ({ app, window }) => {
  await app.nav('Inbox').click()
  await expect(window.getByRole('heading', { name: 'Inbox' })).toBeVisible()
  await expect(window.getByText(/window moved/)).toBeVisible()
  await app.screenshot('inbox')
  await window.getByRole('button', { name: 'Mark all read' }).click()
  await expect(window.getByText('all read · nothing here rings twice')).toBeVisible()
})

test('Capture: braindump advances to the interview', async ({ app, window }) => {
  await app.nav('Capture').click()
  await expect(window.getByText("Tell me what you're planning")).toBeVisible()
  await app.screenshot('capture')
  await window.getByRole('button', { name: 'Brew tickets' }).click()
  await expect(window.getByText(/Interview · 1 of 3/)).toBeVisible()
})

test('Directives: consequence diff + append-only ledger', async ({ app, window }) => {
  await app.nav('Directives').click()
  await expect(window.getByRole('heading', { name: 'Directives' })).toBeVisible()
  await expect(window.getByText('Consequence diff')).toBeVisible()
  await expect(window.getByText(/append-only · JSONL in pm-state/)).toBeVisible()
  await app.screenshot('directives')
  // resolving the pending directive moves it into the ledger
  await window.getByRole('button', { name: 'Make it so' }).click()
  await expect(window.getByText('Consequence diff')).toBeHidden()
  await expect(window.getByText(/Nothing awaits your word/)).toBeVisible()
})

test('Settings: connection, schema, and appearance sync with theme', async ({ app, window }) => {
  await app.nav('Settings').click()
  await expect(window.getByRole('heading', { name: 'Settings' })).toBeVisible()
  await expect(window.getByText('Managed repos')).toBeVisible()
  await app.screenshot('settings')
  // the Evening (dark) radio drives the shared theme
  await window.getByText('Evening (dark)').click()
  await expect(window.locator('html')).toHaveAttribute('data-theme', 'dark')
})

test('Onboarding: full-window first-run flow with test gate', async ({ app, window }) => {
  await app.nav('First run').click()
  await expect(window.getByRole('heading', { name: 'Good morning.' })).toBeVisible()
  // full-window: no rail wordmark link, brand only
  await app.screenshot('onboarding')
  await window.getByRole('button', { name: 'Begin' }).click()
  await expect(window.getByRole('heading', { name: 'Your Gitea' })).toBeVisible()
  // Continue is gated until the connection test passes
  await expect(window.getByRole('button', { name: 'Continue' })).toBeDisabled()
  await window.getByRole('button', { name: 'Test connection' }).click()
  await expect(window.getByText(/connected · 3 repos visible/)).toBeVisible()
  await expect(window.getByRole('button', { name: 'Continue' })).toBeEnabled()
})

test('issue drill-in from a Focus card and back-stack of one', async ({ app, window }) => {
  await window.getByRole('link', { name: 'Fix lifecycle inference on merge events' }).click()
  await expect(window.getByText('#87 · stephen/commitea')).toBeVisible()
  await expect(window.getByText('Machine-derived')).toBeVisible()
  await app.screenshot('issue')
  await window.getByRole('button', { name: 'Back' }).click()
  await expect(window.getByRole('heading', { name: 'Morning service' })).toBeVisible()
})

test('Evening service toggle flips the document theme', async ({ app, window }) => {
  await window.getByText('Evening service', { exact: true }).click()
  await expect(window.locator('html')).toHaveAttribute('data-theme', 'dark')
  await app.screenshot('shell-dark')
})

test('offline sim shows the banner and disables the composer', async ({ window }) => {
  await window.getByRole('button', { name: /Connection/ }).click()
  await expect(window.getByText(/Gitea isn.t answering/)).toBeVisible()
  await expect(window.getByRole('textbox')).toBeDisabled()
})

test('exposes the commitea preload API', async ({ app }) => {
  const api = await app.preloadApi()
  expect(api).toBeDefined()
  expect(api).toHaveProperty('platform')
})
