import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { CompanyScopeRequiredError, ParentItemRequiredError } from "../lib/errors.generated";
import { baseActiveBom, baseDraftBom, baseInactiveBom } from "../testing/fixtures";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { run } from "./listBillOfMaterialsByItem";

describe("listBillOfMaterialsByItem", () => {
  it("lists BOM versions for a manufactured item", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveBom, baseDraftBom]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("filters BOM versions by site when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveBom]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
      siteId: "site-1",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("filters BOM versions by status when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveBom]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
      status: "ACTIVE",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("filters BOM versions by bomType when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveBom]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
      bomType: "MANUFACTURE",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("returns all statuses by default", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveBom, baseDraftBom, baseInactiveBom]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("returns an empty list when no BOM versions exist", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "item-1",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

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

  it("returns error when the parent item reference is missing", async () => {
    const { db } = createMockDb<DB>();

    const result = await run(db, {
      companyId: "company-1",
      parentItemId: "",
    });
    expectErr(result as never, ParentItemRequiredError);
  });

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

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