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

describe("listSalesPriceLists", () => {
  it("lists sales price lists by company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePriceList, draftPriceList]);

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

    expect(items).toHaveLength(2);
  });

  it("filters price lists by lifecycle status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePriceList]);

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

    expect(items).toHaveLength(1);
  });

  it("throws when company scope is missing", async () => {
    const { db } = createMockDb<DB>();

    const result = await run(db, { companyId: "" });
    expectErr(result as never, CompanyScopeRequiredError);
  });
});
