import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { baseGoodsReceiptExecution, baseGoodsReceiptExecutionLine } from "../testing/fixtures";
import { run } from "./listGoodsReceipts";

describe("listGoodsReceipts", () => {
  it("lists goods receipt executions with lines", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseGoodsReceiptExecution], [baseGoodsReceiptExecutionLine]);

    const result = await run(mock.db, {});
    const { items, hasNextPage } = expectOk<{
      items: { inventoryExecutionLines: unknown[] }[];
      hasNextPage: boolean;
    }>(result as never);

    expect(hasNextPage).toBe(false);
    expect(items).toHaveLength(1);
    expect(items[0].inventoryExecutionLines).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("GOODS_RECEIPT");
    expect(mock.selects[0].sql).toMatch(/"executionType"/);
  });

  it("returns empty list when no matches", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { status: "POSTED" });
    const { items, hasNextPage } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(
      result as never,
    );

    expect(items).toHaveLength(0);
    expect(hasNextPage).toBe(false);
  });
});
