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

import { login, apiHeaders } from '../smoke/helpers';
import { seedFullyLoadedDeal, changeStage, readStage } from '../fixtures/sales-journey';

/**
 * End-to-end sales journey — Documenso-dependent half.
 *
 * Covers the signing legs of the pipeline that need a live Documenso
 * instance: generating the EOI through the documenso-template pathway
 * and confirming the document is created remotely + persisted locally,
 * then walking the interest forward through the post-signing stages.
 *
 * Skips cleanly when the Documenso env is absent (same guard as
 * `tests/e2e/realapi/documenso-real-api.spec.ts`), so this spec is a
 * no-op in CI runs that don't opt into the real-API project.
 *
 * The deterministic stage/UI assertions for the journey live in
 * `tests/e2e/exhaustive/10-sales-journey.spec.ts`; this file only adds
 * the legs that genuinely require the external signer.
 */

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

test.describe('realapi: sales journey — Documenso signing legs', () => {
  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 EOI then advance the deal through signing stages', async ({ page }) => {
    const headers = await apiHeaders(page);
    const deal = await seedFullyLoadedDeal(page, 'Documenso Journey');

    // ─── 1. Qualify + move into the EOI stage ─────────────────────────────
    await changeStage(page.request, headers, deal.interestId, 'qualified');
    await changeStage(page.request, headers, deal.interestId, 'eoi');
    expect(await readStage(page.request, headers, deal.interestId)).toBe('eoi');

    // ─── 2. 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: deal.interestId,
          clientId: deal.clientId,
          berthId: deal.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 persisted locally').toBeTruthy();
    expect(body.data.document.status).toBe('sent');

    // ─── 3. Confirm the document exists on the Documenso side ─────────────
    const documensoId = body.data.document.documensoId!;
    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 };
    expect(String(documensoDoc.id)).toBe(documensoId);
    expect(['PENDING', 'DRAFT'], `unexpected status ${documensoDoc.status}`).toContain(
      documensoDoc.status,
    );

    // ─── 4. Walk the post-EOI legs of the pipeline ────────────────────────
    // The webhook-driven signed → reservation → contract advances happen
    // on real signing events (which Documenso has no machine-driveable
    // "sign on behalf of recipient" endpoint for). We exercise the same
    // forward path the webhook would, recording the deposit en route so
    // the deposit_paid gate is met realistically.
    await changeStage(page.request, headers, deal.interestId, 'reservation');

    const depositRes = await page.request.post(`/api/v1/interests/${deal.interestId}/payments`, {
      headers,
      data: {
        interestId: deal.interestId,
        paymentType: 'deposit',
        amount: '25000',
        currency: 'EUR',
        receivedAt: new Date().toISOString(),
      },
    });
    expect(depositRes.ok(), `deposit: ${depositRes.status()} ${await depositRes.text()}`).toBe(
      true,
    );

    await changeStage(page.request, headers, deal.interestId, 'deposit_paid');
    await changeStage(page.request, headers, deal.interestId, 'contract');
    expect(await readStage(page.request, headers, deal.interestId)).toBe('contract');

    // ─── 5. Complete: tenancy + berth sold ────────────────────────────────
    const tenancyRes = await page.request.post(`/api/v1/berths/${deal.berthId}/tenancies`, {
      headers,
      data: {
        berthId: deal.berthId,
        clientId: deal.clientId,
        yachtId: deal.yachtId,
        interestId: deal.interestId,
        startDate: new Date().toISOString(),
        tenureType: 'permanent',
      },
    });
    expect(
      tenancyRes.ok(),
      `create tenancy: ${tenancyRes.status()} ${await tenancyRes.text()}`,
    ).toBe(true);

    const statusRes = await page.request.patch(`/api/v1/berths/${deal.berthId}/status`, {
      headers,
      data: { status: 'sold', reason: 'Contract signed', interestId: deal.interestId },
    });
    expect(statusRes.ok(), `berth → sold: ${statusRes.status()}`).toBe(true);
    expect(((await statusRes.json()) as { data: { status: string } }).data.status).toBe('sold');
  });
});
