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

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

  const mockGetItemFound = {
    getItem: () => Promise.resolve(ok({ item: { id: "item-1" } })),
  };

  const mockGetItemNotFound = {
    getItem: () => Promise.resolve(ok({ item: null })),
  };

  it("returns error when item not found", async () => {
    const { db } = createMockDb<Transaction>();

    const result = await run(
      db,
      { lotNumber: "LOT-001", itemId: "nonexistent" },
      ctx,
      mockGetItemNotFound,
    );
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(ItemNotFoundError);
    }
  });

  it("returns error when lot number already exists for item", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActiveLot); // duplicate lot found

    const result = await run(db, { lotNumber: "LOT-001", itemId: "item-1" }, ctx, mockGetItemFound);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(DuplicateLotNumberError);
    }
  });

  it("creates lot with all attributes", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const createdLot = {
      ...baseActiveLot,
      id: "new-lot-id",
      lotNumber: "LOT-NEW",
      manufactureDate: new Date("2024-03-01"),
      expirationDate: new Date("2025-03-01"),
      supplierLotReference: "SUP-123",
    };
    spies.select.mockReturnValueOnce(undefined); // no duplicate
    spies.insert.mockReturnValue(createdLot);

    const result = await run(
      db,
      {
        lotNumber: "LOT-NEW",
        itemId: "item-1",
        manufactureDate: new Date("2024-03-01"),
        expirationDate: new Date("2025-03-01"),
        supplierLotReference: "SUP-123",
      },
      ctx,
      mockGetItemFound,
    );
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.lot.lotNumber).toBe("LOT-NEW");
      expect(result.value.lot.supplierLotReference).toBe("SUP-123");
    }
    expect(spies.insert).toHaveBeenCalled();
  });

  it("creates lot with minimal attributes", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const createdLot = {
      ...baseActiveLot,
      id: "new-lot-id",
      lotNumber: "LOT-MIN",
      manufactureDate: null,
      expirationDate: null,
      supplierLotReference: null,
    };
    spies.select.mockReturnValueOnce(undefined); // no duplicate
    spies.insert.mockReturnValue(createdLot);

    const result = await run(db, { lotNumber: "LOT-MIN", itemId: "item-1" }, ctx, mockGetItemFound);
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.lot.lotNumber).toBe("LOT-MIN");
      expect(result.value.lot.manufactureDate).toBeNull();
      expect(result.value.lot.expirationDate).toBeNull();
      expect(result.value.lot.supplierLotReference).toBeNull();
    }
    expect(spies.insert).toHaveBeenCalled();
  });
});
