import type { CommandContext } from "@tailor-platform/erp-kit/core";
import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { LotNotFoundError, LotFullyConsumedError } from "../lib/errors.generated";
import { baseActiveLot, baseFullyConsumedLot } from "../testing/fixtures";
import { run } from "./updateLot";

describe("updateLot", () => {
  const ctx: CommandContext = {
    actorId: "test-actor",
    permissions: ["inventory:inventory:updateLot"],
  };

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

    const result = await mock.withTx((trx) => run(trx, { id: "nonexistent" }, ctx));
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(LotNotFoundError);
    }
  });

  it("returns error when lot is FULLY_CONSUMED", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseFullyConsumedLot]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: "lot-2", expirationDate: new Date("2026-01-01") }, ctx),
    );
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(LotFullyConsumedError);
    }
  });

  it("updates expiration date", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const newExpiration = new Date("2026-06-01");
    const updatedLot = { ...baseActiveLot, expirationDate: newExpiration };
    mock.enqueueResults([baseActiveLot], [updatedLot]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: "lot-1", expirationDate: newExpiration }, ctx),
    );
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.lot.expirationDate).toEqual(newExpiration);
    }
  });

  it("updates supplier lot reference", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedLot = { ...baseActiveLot, supplierLotReference: "NEW-SUP-REF" };
    mock.enqueueResults([baseActiveLot], [updatedLot]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: "lot-1", supplierLotReference: "NEW-SUP-REF" }, ctx),
    );
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.lot.supplierLotReference).toBe("NEW-SUP-REF");
    }
  });

  it("lot number is not part of the update input", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedLot = { ...baseActiveLot }; // lotNumber remains the same
    mock.enqueueResults([baseActiveLot], [updatedLot]);

    // Pass a lotNumber in the input — it should be ignored since
    // UpdateLotInput does not include lotNumber
    const result = await mock.withTx((trx) =>
      // oxlint-disable-next-line typescript/no-explicit-any, typescript/no-unsafe-argument -- intentionally passing extra fields to test they're ignored
      run(trx, { id: "lot-1", supplierLotReference: "test" } as any, ctx),
    );
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.lot.lotNumber).toBe("LOT-001");
    }
    // Verify that the set call does not include lotNumber
    expect(mock.updates[0].updateValues()).toEqual(
      // oxlint-disable-next-line typescript/no-unsafe-assignment -- vitest matcher type
      expect.not.objectContaining({ lotNumber: expect.anything() }),
    );
  });
});
