import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { expectOk, expectErr } from "../testing/commandTestUtils";
import { basePendingWorkOrder, baseInProgressWorkOrder } from "../testing/fixtures";
import { ProductionOrderReferenceRequiredError } from "../lib/errors.generated";
import { run } from "./listWorkOrdersByProductionOrder";

describe("listWorkOrdersByProductionOrder", () => {
  it("lists work orders for a production order in sequence order", async () => {
    const { db, spies } = createMockDb<DB>();
    const workOrder20 = {
      ...basePendingWorkOrder,
      id: "work-order-20",
      routingOperationSequenceNumber: 20,
    };
    spies.select.mockReturnValueOnce([basePendingWorkOrder, workOrder20]);

    const result = await run(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);
  });

  it("filters work orders by status when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInProgressWorkOrder]);

    const result = await run(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);
  });

  it("filters work orders by work center when provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([basePendingWorkOrder]);

    const result = await run(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);
  });

  it("returns summary fields with sequence and progress data", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseInProgressWorkOrder]);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

    const result = await run(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 { db } = createMockDb<DB>();

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