import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { costElementLifecycle } from "../db/costElement.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { CostElementNotFoundError, InvalidStateError } from "../lib/errors.generated";

export interface ReactivateCostElementInput {
  costElementId: string;
}

/** Command: reactivateCostElement
 * Transitions a cost element from INACTIVE status back to ACTIVE status.
 */
export async function run(db: Transaction, input: ReactivateCostElementInput, ctx: CommandContext) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "CostElement",
    statusField: "status",
    id: input.costElementId,
    transition: "reactivate",
    lifecycle: costElementLifecycle,
    errors: { notFound: CostElementNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ costElement: result.value });
}
