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

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

/**
 * Real-API end-to-end test for the Documenso documenso-template pathway.
 *
 * This spec exercises the SEND side of the integration only - it creates a
 * fully-loaded interest, fires generate-and-sign, and asserts both
 *   (a) the CRM persisted a documensoId on the documents row, and
 *   (b) Documenso itself returns 200 for the freshly-created document.
 *
 * The receive side (webhook → state update) is validated separately by
 * triggering a real signing event in the Documenso UI and watching
 * /tmp/dev-server.log; it isn't automated here because Documenso has no
 * machine-friendly "sign on behalf of recipient" endpoint we can drive
 * deterministically from CI.
 *
 * Requires DOCUMENSO_API_URL + DOCUMENSO_API_KEY + DOCUMENSO_TEMPLATE_ID_EOI
 * in the environment (loaded via dotenv).
 */

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

test.describe('Documenso real-API: documenso-template 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('generate-and-sign creates a real Documenso document', async ({ page }) => {
    const stamp = Date.now();
    const headers = await apiHeaders(page);

    // ─── 1. Seed client (with a contact email so Documenso has a recipient) ──
    const clientRes = await page.request.post('/api/v1/clients', {
      headers,
      data: {
        fullName: `Documenso E2E Client ${stamp}`,
        contacts: [
          { channel: 'email', value: `documenso-e2e-${stamp}@example.test`, isPrimary: true },
        ],
      },
    });
    expect(clientRes.ok(), `client create: ${clientRes.status()} ${await clientRes.text()}`).toBe(
      true,
    );
    const clientId = (await clientRes.json()).data.id as string;

    // ─── 2. Seed yacht owned by that client ──────────────────────────────────
    const yachtRes = await page.request.post('/api/v1/yachts', {
      headers,
      data: {
        name: `E2E Yacht ${stamp}`,
        owner: { type: 'client', id: clientId },
      },
    });
    expect(yachtRes.ok(), `yacht create: ${yachtRes.status()} ${await yachtRes.text()}`).toBe(true);
    const yachtId = (await yachtRes.json()).data.id as string;

    // ─── 3. Seed berth ───────────────────────────────────────────────────────
    const berthRes = await page.request.post('/api/v1/berths', {
      headers,
      data: {
        mooringNumber: `E2E-${stamp}`,
        area: 'Test Area',
      },
    });
    expect(berthRes.ok(), `berth create: ${berthRes.status()} ${await berthRes.text()}`).toBe(true);
    const berthId = (await berthRes.json()).data.id as string;

    // ─── 4. Create the interest linking all three ────────────────────────────
    const interestRes = await page.request.post('/api/v1/interests', {
      headers,
      data: { clientId, yachtId, berthId, pipelineStage: 'open' },
    });
    expect(
      interestRes.ok(),
      `interest create: ${interestRes.status()} ${await interestRes.text()}`,
    ).toBe(true);
    const interestId = (await interestRes.json()).data.id as string;

    // ─── 5. Fire generate-and-sign through the documenso-template pathway ────
    const signRes = await page.request.post(
      '/api/v1/document-templates/documenso-template/generate-and-sign',
      {
        headers,
        data: {
          interestId,
          clientId,
          berthId,
          pathway: 'documenso-template',
          signers: [],
        },
      },
    );
    expect(signRes.ok(), `generate-and-sign: ${signRes.status()} ${await signRes.text()}`).toBe(
      true,
    );

    const body = (await signRes.json()) as {
      data: { document: { id: string; documensoId: string | null; status: string } };
    };
    expect(body.data.document.documensoId, 'documensoId on response').toBeTruthy();
    expect(body.data.document.status).toBe('sent');

    const documensoId = body.data.document.documensoId!;

    // ─── 6. Verify the document exists on the Documenso side ─────────────────
    const documensoRes = await page.request.get(
      `${DOCUMENSO_BASE}/api/v1/documents/${documensoId}`,
      {
        headers: { Authorization: `Bearer ${DOCUMENSO_API_KEY}` },
      },
    );
    expect(
      documensoRes.ok(),
      `documenso GET: ${documensoRes.status()} ${await documensoRes.text()}`,
    ).toBe(true);

    const documensoDoc = (await documensoRes.json()) as {
      id: number;
      status: string;
      recipients?: Array<{ email: string; signingStatus: string }>;
    };

    expect(String(documensoDoc.id)).toBe(documensoId);
    // Freshly-sent doc should be PENDING (or DRAFT if Documenso queued it).
    expect(['PENDING', 'DRAFT'], `unexpected status ${documensoDoc.status}`).toContain(
      documensoDoc.status,
    );
    // The template is configured with ≥1 recipient role, so we should have
    // at least the client's email populated.
    expect(documensoDoc.recipients?.length ?? 0).toBeGreaterThan(0);
  });
});
