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

export interface ReactivateProfitCenterInput {
  profitCenterId: string;
}

export async function run(
  db: Transaction,
  input: ReactivateProfitCenterInput,
  ctx: CommandContext,
) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "ProfitCenter",
    statusField: "status",
    id: input.profitCenterId,
    transition: "reactivate",
    lifecycle: profitCenterLifecycle,
    errors: { notFound: ProfitCenterNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ profitCenter: result.value });
}
