import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseActiveWorkCenter, baseInactiveWorkCenter } from "../testing/fixtures";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { WorkCenterNotFoundError } from "../lib/errors.generated";
import { run } from "./getWorkCenter";

describe("getWorkCenter", () => {
  it("returns a work center when found", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseActiveWorkCenter);

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

    expect(workCenter).toMatchObject({ id: baseActiveWorkCenter.id });
  });

  it("returns inactive work centers when requested", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(baseInactiveWorkCenter);

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

    expect(workCenter.status).toBe("INACTIVE");
  });

  it("returns error when the work center does not exist", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, { id: "nonexistent-id" });
    expectErr(result as never, WorkCenterNotFoundError);
  });
});
