import { beforeEach, describe, expect, it, vi } from "vitest";
import { GET, POST } from "./route";

const mocks = vi.hoisted(() => ({
  requireApiV1Auth: vi.fn(),
  resolvePaymentLinksContext: vi.fn(),
  listPaymentLinks: vi.fn(),
  createPaymentLink: vi.fn(),
}));

vi.mock("@/lib/api-v1/auth", () => ({
  requireApiV1Auth: mocks.requireApiV1Auth,
}));

vi.mock("@/lib/api-v1/services/payment-links-service", () => ({
  resolvePaymentLinksContext: mocks.resolvePaymentLinksContext,
  listPaymentLinks: mocks.listPaymentLinks,
  createPaymentLink: mocks.createPaymentLink,
}));

describe("Payment links API v1", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.requireApiV1Auth.mockResolvedValue({
      supabase: {},
      admin: {},
      user: { id: "user-1" },
    });
    mocks.resolvePaymentLinksContext.mockResolvedValue({ ownerId: "owner-1" });
  });

  it("lista links de pagamento no envelope padrao", async () => {
    mocks.listPaymentLinks.mockResolvedValue({
      links: [{ id: "link-1", title: "Caneca Premium" }],
    });

    const response = await GET(new Request("http://localhost/api/v1/payment-links"));
    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toMatchObject({
      success: true,
      data: {
        items: [{ id: "link-1", title: "Caneca Premium" }],
      },
    });
  });

  it("cria link de pagamento no envelope padrao", async () => {
    mocks.createPaymentLink.mockResolvedValue({
      link: { id: "link-2", title: "Assinatura Mensal" },
    });

    const response = await POST(
      new Request("http://localhost/api/v1/payment-links", {
        method: "POST",
        body: JSON.stringify({ title: "Assinatura Mensal", amount: 59.9 }),
      })
    );

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toMatchObject({
      success: true,
      data: {
        id: "link-2",
        title: "Assinatura Mensal",
      },
    });
  });
});
