import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseSupplyPlan } from "../testing/fixtures";
import { run } from "./getInventorySupplyPlan";

describe("getInventorySupplyPlan", () => {
  it("returns supply plan by id", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, { id: "supply-plan-1" });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan?.id).toBe("supply-plan-1");
  });

  it("returns supply plan by source line", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, {
      sourceType: "PURCHASE_ORDER",
      sourceLineId: "po-line-1",
    });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan?.id).toBe("supply-plan-1");
    expect(mock.selects[0].parameters).toContain("PURCHASE_ORDER");
    expect(mock.selects[0].parameters).toContain("po-line-1");
    expect(mock.selects[0].sql).toMatch(/"sourceType"/);
    expect(mock.selects[0].sql).toMatch(/"sourceLineId"/);
  });

  it("returns null when not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { id: "missing" });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan).toBeNull();
  });

  it("computes open quantity", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([
      {
        ...baseSupplyPlan,
        expectedQuantity: "100",
        receivedQuantity: "25",
      },
    ]);

    const result = await run(mock.db, { id: "supply-plan-1" });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan?.openQuantity).toBe("75");
  });

  it("returns zero open quantity when received quantity exceeds expected quantity", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([
      {
        ...baseSupplyPlan,
        expectedQuantity: "80",
        receivedQuantity: "100",
      },
    ]);

    const result = await run(mock.db, { id: "supply-plan-1" });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan?.openQuantity).toBe("0");
  });

  it("returns closed supply plan with zero open quantity", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([
      {
        ...baseSupplyPlan,
        expectedQuantity: "100",
        receivedQuantity: "25",
        status: "CLOSED",
      },
    ]);

    const result = await run(mock.db, { id: "supply-plan-1" });
    expect(result.ok).toBe(true);
    const { supplyPlan } = result.value;

    expect(supplyPlan?.status).toBe("CLOSED");
    expect(supplyPlan?.openQuantity).toBe("0");
  });
});
