jest.mock("@/lib/db");

import { prisma } from "@/lib/db";
import { handleQuotationPaymentWebhook } from "@/lib/services/quotation-payments.service";
import type Stripe from "stripe";

function makeSession(
  metadata: Record<string, string>,
): Stripe.Checkout.Session {
  return {
    id: "cs_test_abc",
    payment_intent: "pi_test_123",
    amount_total: 50000,
    currency: "qar",
    metadata,
  } as unknown as Stripe.Checkout.Session;
}

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

  it.skip("processes camelCase quotationId from portal checkout", async () => {
    const session = makeSession({
      type: "quotation_payment",
      quotationId: "q1",
    });
    await handleQuotationPaymentWebhook(session);
    expect(prisma.quotation_payments.create).toHaveBeenCalled();
  });

  it.skip("processes snake_case quotation_id from dashboard checkout", async () => {
    const session = makeSession({
      type: "quotation_payment",
      quotation_id: "q1",
    });
    await handleQuotationPaymentWebhook(session);
    expect(prisma.quotation_payments.create).toHaveBeenCalled();
  });

  it("returns early when no quotation id found in metadata", async () => {
    const session = makeSession({ type: "quotation_payment" });
    await handleQuotationPaymentWebhook(session);
    expect(prisma.quotation_payments.create).not.toHaveBeenCalled();
  });
});
