import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  StockMovementNotFoundError,
  StockMovementAlreadyDoneError,
  StockMovementAlreadyCancelledError,
} from "../lib/errors.generated";
import {
  baseDraftMovement,
  baseConfirmedMovement,
  baseDoneMovement,
  baseCancelledMovement,
} from "../testing/fixtures";
import { run } from "./cancelStockMovement";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

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

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

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

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

  it("returns error when DONE", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseDoneMovement);

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

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

  it("returns error when already cancelled", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseCancelledMovement);

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

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

  it("cancels DRAFT movement", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const cancelled = { ...baseDraftMovement, status: "CANCELLED", updatedAt: new Date() };
    spies.select.mockReturnValueOnce(baseDraftMovement);
    spies.update.mockReturnValue(cancelled);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.stockMovement.status).toBe("CANCELLED");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("cancels CONFIRMED movement", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const cancelled = { ...baseConfirmedMovement, status: "CANCELLED", updatedAt: new Date() };
    spies.select.mockReturnValueOnce(baseConfirmedMovement);
    spies.update.mockReturnValue(cancelled);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.stockMovement.status).toBe("CANCELLED");
    }
  });

  it("does not affect stock levels", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const cancelled = { ...baseDraftMovement, status: "CANCELLED", updatedAt: new Date() };
    spies.select.mockReturnValueOnce(baseDraftMovement);
    spies.update.mockReturnValue(cancelled);

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

    expect(result.ok).toBe(true);
    // Only one update call (the movement status), no stock level updates
    expect(spies.update).toHaveBeenCalledTimes(1);
    expect(spies.insert).not.toHaveBeenCalled();
  });
});
