import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { WorkOrderNotFoundError } from "../lib/errors.generated";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import {
  baseInProgressWorkOrder,
  basePausedWorkOrder,
  baseStartedEvent,
  baseProgressEvent,
} from "../testing/fixtures";
import { run } from "./getWorkOrder";

describe("getWorkOrder", () => {
  it("returns a work order with execution history when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInProgressWorkOrder], [baseStartedEvent, baseProgressEvent]);

    const result = await run(mock.db, { id: baseInProgressWorkOrder.id });
    const { workOrder, executionEvents } = expectOk<{
      workOrder: { id: string; status: string };
      executionEvents: unknown[];
    }>(result as never);

    expect(workOrder.id).toBe(baseInProgressWorkOrder.id);
    expect(workOrder.status).toBe("IN_PROGRESS");
    expect(executionEvents).toHaveLength(2);
    expect(mock.selects).toHaveLength(2);
    expect(mock.selects[0].parameters).toContain(baseInProgressWorkOrder.id);
    expect(mock.selects[0].sql).toMatch(/"id"/);
    expect(mock.selects[1].parameters).toContain(baseInProgressWorkOrder.id);
    expect(mock.selects[1].sql).toMatch(/"workOrderId"/);
    expect(mock.selects[1].sql).toMatch(/order by .*"timestamp" asc/i);
  });

  it("returns paused work orders with prior pause data", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePausedWorkOrder], [baseStartedEvent]);

    const result = await run(mock.db, { id: basePausedWorkOrder.id });
    const { workOrder, executionEvents } = expectOk<{
      workOrder: { id: string; status: string; pauseReason: string | null };
      executionEvents: unknown[];
    }>(result as never);

    expect(workOrder.status).toBe("PAUSED");
    expect(workOrder.pauseReason).toBeDefined();
    expect(executionEvents).toHaveLength(1);
  });

  it("returns a work order with handoff evidence and parent order context", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInProgressWorkOrder], [baseStartedEvent]);

    const result = await run(mock.db, { id: baseInProgressWorkOrder.id });
    const { workOrder } = expectOk<{
      workOrder: {
        id: string;
        productionOrderId: string;
        workCenterId: string;
      };
      executionEvents: unknown[];
    }>(result as never);

    expect(workOrder.productionOrderId).toBe(baseInProgressWorkOrder.productionOrderId);
    expect(workOrder.workCenterId).toBe(baseInProgressWorkOrder.workCenterId);
  });

  it("returns error when the work order does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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