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

import { login, apiHeaders } from '../smoke/helpers';

/**
 * Real-API spec for the cancel flow (Phase A PR2 + PR5).
 *
 * Generates a real Documenso document, then calls POST
 * /api/v1/documents/[id]/cancel and asserts the local DB flips to cancelled.
 * Per PR2 review, voidDocument treats transient remote failures as
 * recoverable so the local cancel succeeds even if Documenso flakes.
 *
 * Skips when Documenso env not present.
 */

const DOCUMENSO_BASE = process.env.DOCUMENSO_API_URL;
const DOCUMENSO_API_KEY = process.env.DOCUMENSO_API_KEY;

test.describe('Documenso cancel pathway', () => {
  test.skip(!DOCUMENSO_BASE || !DOCUMENSO_API_KEY, 'DOCUMENSO_API_URL / DOCUMENSO_API_KEY not set');

  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('cancel an in-flight document flips status to cancelled', async ({ page }) => {
    const stamp = Date.now();
    const headers = await apiHeaders(page);

    // Seed a minimal client to ensure a doc can be created. Real cancel
    // testing assumes either an existing in-flight doc or the wizard flow
    // has already produced one. We probe the hub for an in-flight doc and
    // skip if none - this lets the spec run as a smoke check rather than
    // a fixture-dependent integration.
    const list = await page.request.get(
      '/api/v1/documents?tab=awaiting_them&signatureOnly=true&limit=1',
      { headers },
    );
    expect(list.ok()).toBe(true);
    const body = (await list.json()) as { data: Array<{ id: string; status: string }> };
    test.skip(body.data.length === 0, 'no in-flight documents to cancel');

    const docId = body.data[0]!.id;
    const cancelRes = await page.request.post(`/api/v1/documents/${docId}/cancel`, {
      headers,
      data: { _stamp: stamp },
    });
    expect(cancelRes.ok(), `cancel: ${cancelRes.status()}`).toBe(true);

    // Verify status flipped
    const after = await page.request.get(`/api/v1/documents/${docId}`, { headers });
    const afterBody = (await after.json()) as { data: { status: string } };
    expect(afterBody.data.status).toBe('cancelled');
  });
});
