import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { WorkCenterNotFoundError } from "../lib/errors.generated";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { baseActiveWorkCenter, baseInactiveWorkCenter } from "../testing/fixtures";
import { run } from "./getWorkCenter";

describe("getWorkCenter", () => {
  it("returns a work center when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveWorkCenter]);

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

    expect(workCenter).toMatchObject({ id: baseActiveWorkCenter.id });
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(baseActiveWorkCenter.id);
    expect(mock.selects[0].sql).toMatch(/"id"/);
  });

  it("returns inactive work centers when requested", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInactiveWorkCenter]);

    const result = await run(mock.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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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