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 { baseActiveRouting, baseDraftRouting, baseInactiveRouting } from "../testing/fixtures";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { run } from "./listRoutingsByItem";

describe("listRoutingsByItem", () => {
  it("lists routing revisions for a produced item", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveRouting, baseDraftRouting]);

    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 routing revisions by site when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveRouting]);

    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 routing revisions by status when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveRouting]);

    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("returns all statuses by default", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveRouting, baseDraftRouting, baseInactiveRouting]);

    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 routings 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 routing revisions for the requested company scope", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseActiveRouting]);

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

    expect(items).toHaveLength(1);
    expect(items[0].companyId).toBe("company-1");
  });

  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);
  });
});
