import { describe, expect, it } from 'vitest';

import { computeEoiSignatureLayout } from '@/lib/services/documenso-client';

// The EOI moves from the Documenso *template* pathway (Documenso fills the
// AcroForm detail fields and auto-sizes/clips them) to the in-app pathway:
// we fill + flatten the PDF locally, upload it as a Documenso *document*, then
// place ONLY the page-3 signature fields. This layout must match template 8's
// six fields exactly (client: Signature/Name/Place-of-Signing/Date; developer:
// Name/Signature) so the signed EOI looks identical. Coords are percent of page.
describe('computeEoiSignatureLayout', () => {
  const CLIENT = 101;
  const DEV = 102;
  const fields = computeEoiSignatureLayout(CLIENT, DEV);

  it('produces exactly the 6 page-3 EOI signature fields', () => {
    expect(fields).toHaveLength(6);
    expect(fields.every((f) => f.pageNumber === 3)).toBe(true);
  });

  it('maps client recipient to Signature + Name + Place-of-Signing + Date', () => {
    const client = fields.filter((f) => f.recipientId === CLIENT);
    expect(client.map((f) => f.type).sort()).toEqual(['DATE', 'NAME', 'SIGNATURE', 'TEXT']);
  });

  it('maps developer recipient to Name + Signature only', () => {
    const dev = fields.filter((f) => f.recipientId === DEV);
    expect(dev.map((f) => f.type).sort()).toEqual(['NAME', 'SIGNATURE']);
  });

  it('carries the Place-of-Signing label + required so the signer is prompted', () => {
    const place = fields.find((f) => f.recipientId === CLIENT && f.type === 'TEXT');
    expect(place?.fieldMeta?.label).toBe('Place of Signing');
    expect(place?.fieldMeta?.required).toBe(true);
  });

  it('positions fields at template-8 coordinates (page-3 signature block)', () => {
    const sig = fields.find((f) => f.recipientId === CLIENT && f.type === 'SIGNATURE');
    expect(sig?.pageX).toBeCloseTo(39.645, 2);
    expect(sig?.pageY).toBeCloseTo(64.82, 1);
    const devSig = fields.find((f) => f.recipientId === DEV && f.type === 'SIGNATURE');
    expect(devSig?.pageY).toBeCloseTo(72.57, 1);
  });
});
