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

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

  it("returns error when lot not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined); // lot lookup fails

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseFullyConsumedLot); // fully consumed lot

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    const newExpiration = new Date("2026-06-01");
    const updatedLot = { ...baseActiveLot, expirationDate: newExpiration };
    spies.select.mockReturnValueOnce(baseActiveLot); // existing lot
    spies.update.mockReturnValue(updatedLot);

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    const updatedLot = { ...baseActiveLot, supplierLotReference: "NEW-SUP-REF" };
    spies.select.mockReturnValueOnce(baseActiveLot); // existing lot
    spies.update.mockReturnValue(updatedLot);

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    const updatedLot = { ...baseActiveLot }; // lotNumber remains the same
    spies.select.mockReturnValueOnce(baseActiveLot); // existing lot
    spies.update.mockReturnValue(updatedLot);

    // Pass a lotNumber in the input — it should be ignored since
    // UpdateLotInput does not include lotNumber
    const result = await run(
      db,
      // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
      { 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(spies.set).toHaveBeenCalledWith(
      // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
      expect.not.objectContaining({ lotNumber: expect.anything() }),
    );
  });
});
