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 { basePriceList, draftPriceList } from "../testing/fixtures";
import { run } from "./getSalesPriceList";

describe("getSalesPriceList", () => {
  it("returns price list when found by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList]);

    const result = await run(mock.db, { id: basePriceList.id });
    const { salesPriceList } = expectOk<{ salesPriceList: unknown }>(result as never);

    expect(salesPriceList).not.toBeNull();
    expect(salesPriceList).toMatchObject({ id: basePriceList.id });
  });

  it("returns inactive price list when requested by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([draftPriceList]);

    const result = await run(mock.db, { id: draftPriceList.id });
    const { salesPriceList } = expectOk<{ salesPriceList: { status: string } }>(result as never);

    expect(salesPriceList.status).toBe("DRAFT");
  });

  it("returns null when price list is not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { id: "nonexistent-id" });
    const { salesPriceList } = expectOk<{ salesPriceList: unknown }>(result as never);

    expect(salesPriceList).toBeNull();
  });
});
