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

describe("listInventorySupplyPlans", () => {
  it("lists supply plans by item", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, { itemId: "item-1" });
    const page = expectOk<{ items: { itemId: string; openQuantity: string }[] }>(result as never);

    expect(page.items).toHaveLength(1);
    expect(page.items[0].itemId).toBe("item-1");
    expect(page.items[0].openQuantity).toBe("100");
    expect(mock.selects[0].parameters).toContain("OPEN");
    expect(mock.selects[0].sql).toMatch(/"status"/);
  });

  it("filters to OPEN supply plans", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, { itemId: "item-1" });
    const page = expectOk<{ items: { status: string }[] }>(result as never);

    expect(page.items).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("OPEN");
    expect(mock.selects[0].sql).toMatch(/"status"/);
  });

  it("lists supply plans by site", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, { siteId: "site-1" });
    const page = expectOk<{ items: { siteId: string }[] }>(result as never);

    expect(page.items).toHaveLength(1);
    expect(page.items[0].siteId).toBe("site-1");
  });

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

    const result = await run(mock.db, {
      sourceType: "PURCHASE_ORDER",
      sourceId: "po-1",
    });
    const page = expectOk<{ items: { sourceId: string }[] }>(result as never);

    expect(page.items[0].sourceId).toBe("po-1");
  });

  it("lists supply plans by expected date range", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseSupplyPlan]);

    const result = await run(mock.db, {
      expectedFrom: new Date("2026-01-01T00:00:00.000Z"),
      expectedTo: new Date("2026-12-31T00:00:00.000Z"),
    });
    const page = expectOk<{ items: { expectedDate: Date | null }[] }>(result as never);

    expect(page.items[0].expectedDate).toEqual(baseSupplyPlan.expectedDate);
  });

  it("returns error when no filter is provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {});

    expectErr(result as never, NoFilterProvidedError);
  });

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

    const result = await run(mock.db, { itemId: "item-1" });
    const page = expectOk<{ items: { openQuantity: string }[] }>(result as never);

    expect(page.items[0].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, { itemId: "item-1" });
    const page = expectOk<{ items: { openQuantity: string }[] }>(result as never);

    expect(page.items[0].openQuantity).toBe("0");
  });
});
