import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { basePriceList, draftPriceList } from "../testing/fixtures";
import { expectOk } from "../testing/commandTestUtils";
import { run } from "./getSalesPriceList";

describe("getSalesPriceList", () => {
  it("returns price list when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(basePriceList);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(draftPriceList);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

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

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