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

describe("listWorkOrdersByProductionOrder", () => {
  it("lists work orders for a production order in sequence order", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const workOrder20 = {
      ...basePendingWorkOrder,
      id: "work-order-20",
      routingOperationSequenceNumber: 20,
    };
    mock.enqueueResults([basePendingWorkOrder, workOrder20]);

    const result = await run(mock.db, {
      productionOrderId: "production-order-2",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(2);
    expect(items[0]).toEqual(basePendingWorkOrder);
    expect(items[1]).toEqual(workOrder20);

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"productionOrderId"/);
    expect(mock.selects[0].parameters).toContain("production-order-2");
    expect(mock.selects[0].sql).toMatch(/order by .*"routingOperationSequenceNumber" asc/i);
  });

  it("filters work orders by status when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInProgressWorkOrder]);

    const result = await run(mock.db, {
      productionOrderId: "production-order-2",
      status: "IN_PROGRESS",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(baseInProgressWorkOrder);

    expect(mock.selects[0].sql).toMatch(/"status"/);
    expect(mock.selects[0].parameters).toContain("IN_PROGRESS");
  });

  it("filters work orders by work center when provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePendingWorkOrder]);

    const result = await run(mock.db, {
      productionOrderId: "production-order-2",
      workCenterId: "work-center-2",
    });
    const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);

    expect(items).toHaveLength(1);
    expect(items[0]).toEqual(basePendingWorkOrder);

    expect(mock.selects[0].sql).toMatch(/"workCenterId"/);
    expect(mock.selects[0].parameters).toContain("work-center-2");
  });

  it("returns summary fields with sequence and progress data", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseInProgressWorkOrder]);

    const result = await run(mock.db, {
      productionOrderId: "production-order-2",
    });
    const { items } = expectOk<{
      items: {
        routingOperationSequenceNumber: number;
        completedQuantity: number;
        plannedQuantity: number;
      }[];
      hasNextPage: boolean;
    }>(result as never);

    expect(items[0].routingOperationSequenceNumber).toBe(10);
    expect(items[0].completedQuantity).toBe(50);
    expect(items[0].plannedQuantity).toBe(100);
  });

  it("returns an empty list when no work orders exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, {
      productionOrderId: "production-order-2",
    });
    const { items, hasNextPage } = expectOk<{
      items: unknown[];
      hasNextPage: boolean;
    }>(result as never);

    expect(items).toHaveLength(0);
    expect(hasNextPage).toBe(false);
  });

  it("returns error when the production-order reference is missing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {
      productionOrderId: "",
    });
    expectErr(result as never, ProductionOrderReferenceRequiredError);
  });
});
