import { ok } from "@tailor-platform/erp-kit/core";
import { executeTransition } from "../../../shared/defineLifecycle";
import { productLifecycle } from "../db/product.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { ProductNotFoundError, InvalidStateTransitionError } from "../lib/errors.generated";

export interface ReactivateProductInput {
  id: string;
}

/**
 * Function: reactivateProduct
 *
 * Transitions a product from ARCHIVED to ACTIVE.
 */
export async function run(db: Transaction, input: ReactivateProductInput) {
  const result = await executeTransition({
    db,
    tableName: "Product",
    statusField: "status",
    id: input.id,
    transition: "reactivate",
    lifecycle: productLifecycle,
    errors: { notFound: ProductNotFoundError, invalidTransition: InvalidStateTransitionError },
  });
  if (!result.ok) return result;
  return ok({ product: result.value });
}
