import { executeTransition, ok } from "@tailor-platform/erp-kit/core";
import { itemLifecycle } from "../db/item.lifecycle.generated";
import { Transaction } from "../generated/kysely-tailordb";
import { InvalidStateTransitionError, ItemNotFoundError } from "../lib/errors.generated";

export interface ReactivateItemInput {
  id: string;
}

/**
 * Function: reactivateItem
 *
 * Transitions an item from INACTIVE back to ACTIVE status,
 * restoring a previously discontinued SKU for transactions.
 */
export async function run(db: Transaction, input: ReactivateItemInput) {
  const result = await executeTransition({
    db,
    tableName: "Item",
    statusField: "status",
    id: input.id,
    transition: "reactivate",
    lifecycle: itemLifecycle,
    errors: { notFound: ItemNotFoundError, invalidTransition: InvalidStateTransitionError },
  });
  if (!result.ok) return result;
  return ok({ item: result.value });
}
