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

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

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

vi.mock("@/lib/api-v1/services/quotes-service", () => ({
  createQuote: mocks.createQuote,
  listQuotes: vi.fn(),
}));

describe("POST /api/v1/quotes", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("cria orcamento pela API", async () => {
    mocks.requireApiV1Auth.mockResolvedValue({
      supabase: {},
      user: { id: "user-1" },
    });
    mocks.createQuote.mockResolvedValue({
      ok: true,
      quoteId: "66666666-6666-4666-8666-666666666666",
      quoteNumber: 123,
      total: 10,
      expiresAt: null,
      shareUrl: "",
      orderId: null,
    });

    const response = await POST(
      new Request("http://localhost/api/v1/quotes", {
        method: "POST",
        body: JSON.stringify({
          client_id: "client-1",
          items: [{ title: "Caneca", quantity: 1, unit_price: 10 }],
        }),
      })
    );

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toMatchObject({
      success: true,
      data: {
        quote_id: "66666666-6666-4666-8666-666666666666",
        quote_number: 123,
      },
    });
  });
});
