import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { run } from "./listPurchaseOrderLinesForMatching";

const purchaseOrderLine = {
  id: "purchase-order-line-1",
  purchaseOrderId: "purchase-order-1",
  quantity: "10",
  unitPrice: "100",
  unitId: "unit-1",
  requiresPhysicalReceipt: true,
};

describe("listPurchaseOrderLinesForMatching", () => {
  it("lists purchase order line attributes for matching", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([purchaseOrderLine]);

    const result = await run(mock.db, { purchaseOrderLineIds: [purchaseOrderLine.id] });

    expect(result.ok).toBe(true);
    expect(result.value.purchaseOrderLines).toEqual([purchaseOrderLine]);
    const query = mock.selects[0];
    expect(query.sql).toMatch(/"id" in/);
    expect(query.sql).toMatch(/"unitId"/);
    expect(query.parameters).toContain(purchaseOrderLine.id);
  });

  it("returns empty list without querying when no purchase order line IDs are provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, { purchaseOrderLineIds: [] });

    expect(result.ok).toBe(true);
    expect(result.value.purchaseOrderLines).toEqual([]);
    expect(mock.selects).toHaveLength(0);
  });
});
