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

// Ops-gated: requires a live dev server + seeded Neon branch (same harness as e2e/admin.spec.ts).
// Skipped in CI until MOD_CMS_E2E_DB is wired — do NOT fabricate a pass without a real DB.
test.describe.skip('audit-viewer e2e (ops-gated)', () => {
  const ADMIN_EMAIL = 'admin@e2e.test';
  const ADMIN_PASSWORD = 'e2e-admin-pw-9f3a2b1c';
  const ORIGIN = 'http://localhost:4331';

  async function signIn(page: import('@playwright/test').Page) {
    await page.goto('/admin/login');
    await page.getByLabel('Email').fill(ADMIN_EMAIL);
    await page.getByLabel('Password').fill(ADMIN_PASSWORD);
    await page.getByRole('button', { name: 'Sign in' }).click();
    await page.waitForURL('**/admin');
  }

  test('publish writes a content.publish row visible in /admin/audit', async ({ page }) => {
    const stamp = Date.now();
    const slug = `e2e-audit-${stamp}`;

    await signIn(page);

    const createRes = await page.request.post('/api/admin/content', {
      headers: { origin: ORIGIN, 'content-type': 'application/json' },
      data: { type: 'post', slug, title: `Audit ${stamp}`, body: '<p>b</p>' },
    });
    expect(createRes.ok()).toBeTruthy();
    const { id } = (await createRes.json()) as { id: string };

    const pubRes = await page.request.post('/api/admin/publish', {
      headers: { origin: ORIGIN, 'content-type': 'application/json' },
      data: { id },
    });
    expect(pubRes.ok()).toBeTruthy();

    await page.goto('/admin/audit');
    await expect(page.getByRole('heading', { name: 'Audit log', exact: true })).toBeVisible();
    await expect(page.getByText('content.publish')).toBeVisible();
  });
});
