import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { expectOk } from "../testing/commandTestUtils";
import { baseStockLevel, baseStockReservation, emptyStockLevel } from "../testing/fixtures";
import { run } from "./getStockLevel";

describe("getStockLevel", () => {
  it("returns stock level when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseStockLevel]);

    const result = await run(mock.db, {
      itemId: "item-1",
      storageLocationId: "location-1",
    });
    const { stockLevel } = expectOk<{ stockLevel: unknown }>(result as never);

    expect(stockLevel).not.toBeNull();
    expect(stockLevel).toMatchObject({
      itemId: "item-1",
      storageLocationId: "location-1",
    });
  });

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

    const result = await run(mock.db, {
      itemId: "non-existent",
      storageLocationId: "location-1",
    });
    const { stockLevel } = expectOk<{ stockLevel: null }>(result as never);

    expect(stockLevel).toBeNull();
  });

  it("correctly computes available quantity", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseStockLevel], [baseStockReservation]);

    const result = await run(mock.db, {
      itemId: "item-1",
      storageLocationId: "location-1",
    });
    const { stockLevel } = expectOk<{
      stockLevel: { availableQuantity: string };
    }>(result as never);

    expect(stockLevel.availableQuantity).toBe("75");
  });

  it("sums multiple open reservations for the location", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [baseStockLevel],
      [
        { ...baseStockReservation, storageLocationId: "location-1", reservedQuantity: "10" },
        {
          ...baseStockReservation,
          id: "stock-reservation-2",
          storageLocationId: "location-1",
          reservedQuantity: "5",
          consumedQuantity: "2",
        },
      ],
    );

    const result = await run(mock.db, {
      itemId: "item-1",
      storageLocationId: "location-1",
    });
    const { stockLevel } = expectOk<{
      stockLevel: { reserved: string; availableQuantity: string };
    }>(result as never);

    expect(stockLevel.reserved).toBe("13");
    expect(stockLevel.availableQuantity).toBe("72");
  });

  it("available equals AVAILABLE stock type minus location-specific open reservation quantity", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([emptyStockLevel]);

    const result = await run(mock.db, {
      itemId: "item-2",
      storageLocationId: "location-1",
    });
    const { stockLevel } = expectOk<{
      stockLevel: { availableQuantity: string };
    }>(result as never);

    expect(stockLevel.availableQuantity).toBe("0");
  });
});
