import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { WarehouseNotFoundError, DuplicateWarehouseNameError } from "../lib/errors.generated";
import { baseActiveWarehouse } from "../testing/fixtures";
import { run } from "./updateWarehouse";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

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

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

    const result = await run(db, { id: "nonexistent", name: "New Name" }, ctx);

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(WarehouseNotFoundError);
    }
  });

  it("returns error when new name conflicts", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseActiveWarehouse) // warehouse found
      .mockReturnValueOnce({
        ...baseActiveWarehouse,
        id: "warehouse-other",
        name: "Existing Name",
      }); // duplicate found

    const result = await run(db, { id: baseActiveWarehouse.id, name: "Existing Name" }, ctx);

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(DuplicateWarehouseNameError);
    }
  });

  it("updates warehouse name", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedWarehouse = { ...baseActiveWarehouse, name: "Renamed Warehouse" };
    spies.select
      .mockReturnValueOnce(baseActiveWarehouse) // warehouse found
      .mockReturnValueOnce(undefined); // no duplicate
    spies.update.mockReturnValue(updatedWarehouse);

    const result = await run(db, { id: baseActiveWarehouse.id, name: "Renamed Warehouse" }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.warehouse.name).toBe("Renamed Warehouse");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("updates storage condition", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedWarehouse = { ...baseActiveWarehouse, storageCondition: "cold" };
    spies.select.mockReturnValueOnce(baseActiveWarehouse); // warehouse found
    spies.update.mockReturnValue(updatedWarehouse);

    const result = await run(db, { id: baseActiveWarehouse.id, storageCondition: "cold" }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.warehouse.storageCondition).toBe("cold");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("updates capacity", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedWarehouse = { ...baseActiveWarehouse, capacity: 5000 };
    spies.select.mockReturnValueOnce(baseActiveWarehouse); // warehouse found
    spies.update.mockReturnValue(updatedWarehouse);

    const result = await run(db, { id: baseActiveWarehouse.id, capacity: 5000 }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.warehouse.capacity).toBe(5000);
    }
    expect(spies.update).toHaveBeenCalled();
  });
});
