jest.mock("@react-pdf/renderer", () => ({
  Document: ({ children }: { children: React.ReactNode }) => children,
  Page: ({ children }: { children: React.ReactNode }) => children,
  View: ({ children }: { children: React.ReactNode }) => children,
  Text: ({ children }: { children: React.ReactNode }) => children,
  StyleSheet: { create: (s: Record<string, unknown>) => s },
  renderToBuffer: jest.fn().mockResolvedValue(Buffer.from("MOCK_PDF_DATA")),
}));

import React from "react";
import { renderToBuffer } from "@react-pdf/renderer";
import { generateQuotationPdf } from "@/lib/services/quotation-pdf.service";

const mockQuotation = {
  quotationNumber: "Q-20260226-0001",
  title: "Website Design",
  createdAt: new Date("2026-02-26"),
  dueDate: new Date("2026-03-12"),
  total: 5000,
  currency: "QAR",
  taxRate: 10,
  discountRate: 0,
  portalToken: "token-abc",
  customer: {
    name: "Ahmed Al-Farsi",
    email: "ahmed@example.com",
    phone: "+97450000001",
  },
  items: [
    {
      description: "Homepage design",
      quantity: 1,
      unitPrice: 3000,
      total: 3000,
    },
    {
      description: "Inner pages (x4)",
      quantity: 4,
      unitPrice: 500,
      total: 2000,
    },
  ],
};

describe("generateQuotationPdf", () => {
  beforeEach(() => jest.clearAllMocks());

  it("returns a non-empty Buffer", async () => {
    const result = await generateQuotationPdf(mockQuotation);
    expect(Buffer.isBuffer(result)).toBe(true);
    expect(result.length).toBeGreaterThan(0);
  });

  it("calls renderToBuffer once", async () => {
    await generateQuotationPdf(mockQuotation);
    expect(renderToBuffer).toHaveBeenCalledTimes(1);
  });

  it("works when customer is null", async () => {
    const result = await generateQuotationPdf({
      ...mockQuotation,
      customer: null,
    });
    expect(Buffer.isBuffer(result)).toBe(true);
  });

  it("works when items is empty", async () => {
    const result = await generateQuotationPdf({ ...mockQuotation, items: [] });
    expect(Buffer.isBuffer(result)).toBe(true);
  });
});
