import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

import { _electron as electron, expect, test } from '@playwright/test'

const here = dirname(fileURLToPath(import.meta.url))
const MAIN = join(here, '..', 'out', 'main', 'index.js')

// Opt-in (GITEA_LIVE=1 + COMMITEA_MODEL_LIVE=1 + a local model + the commitea-pm-state
// repo). Reginald records a standing directive; it's appended to the real pm-state
// ledger. The append is idempotent-ish (a fresh line each run); this only reads back
// that a directive was consulted, and leaves the ledger intact.
test.describe('live record_directive', () => {
  test('logs a standing directive to the pm-state ledger', async () => {
    test.skip(!process.env.GITEA_LIVE || !process.env.COMMITEA_MODEL_LIVE, 'live model test — opt-in')
    test.setTimeout(300_000)
    const app = await electron.launch({ args: [MAIN], env: { ...process.env } })
    const win = await app.firstWindow()
    await win.waitForLoadState('domcontentloaded')
    await expect(win.getByText(/· local$/)).toBeVisible({ timeout: 20000 })

    const composer = win.getByPlaceholder(/Tell me what to do/)
    await composer.fill('Record a standing directive: freeze scope for beta, pilots come first.')
    await composer.press('Enter')

    // the agent logged it to the ledger (record_directive), not applied a change
    await expect(win.getByText(/consulted the directive ledger/)).toBeVisible({ timeout: 240_000 })
    await win.screenshot({ path: join(here, '.artifacts', 'screens', 'live-directive.png'), fullPage: true, animations: 'disabled' })

    await app.close()
  })
})
