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

describe("listTransferOrders", () => {
  it("lists transfer orders with lines and derived statuses", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [baseOpenTransferOrder, baseDraftTransferOrder],
      [partiallyReceivedTransferOrderLine, baseTransferOrderLine],
    );

    const result = await run(mock.db, { limit: 10 });
    const value = expectOk<{
      items: Array<{
        transferOrderLines: unknown[];
        shippingStatus: string;
        receivingStatus: string;
      }>;
      hasNextPage: boolean;
    }>(result as never);

    expect(value.items).toHaveLength(2);
    expect(value.items[0].shippingStatus).toBe("SHIPPED");
    expect(value.items[0].receivingStatus).toBe("PARTIALLY_RECEIVED");
    expect(value.items[1].shippingStatus).toBe("NOT_SHIPPED");
    expect(value.items[1].receivingStatus).toBe("NOT_RECEIVED");
  });

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

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

    expect(value.items).toEqual([]);
    expect(value.hasNextPage).toBe(false);
  });
});
