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 { baseOpenTransferOrder, partiallyReceivedTransferOrderLine } from "../testing/fixtures";
import { run } from "./getTransferOrder";

describe("getTransferOrder", () => {
  it("returns transfer order with lines and derived statuses", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseOpenTransferOrder], [partiallyReceivedTransferOrderLine]);

    const result = await run(mock.db, { id: "transfer-order-3" });
    const value = expectOk<{
      transferOrder: unknown;
      transferOrderLines: unknown[];
      shippingStatus: string;
      receivingStatus: string;
    }>(result as never);

    expect(value.transferOrder).toEqual(baseOpenTransferOrder);
    expect(value.transferOrderLines).toHaveLength(1);
    expect(value.shippingStatus).toBe("SHIPPED");
    expect(value.receivingStatus).toBe("PARTIALLY_RECEIVED");
  });

  it("returns null when transfer order does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, { id: "missing" });
    const value = expectOk<{ transferOrder: unknown; transferOrderLines: unknown[] }>(
      result as never,
    );

    expect(value.transferOrder).toBeNull();
    expect(value.transferOrderLines).toEqual([]);
  });
});
