import { ok, executeTransition } from "@tailor-platform/erp-kit/core";
import { contactPersonLifecycle } from "../db/contactPerson.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { ContactPersonNotFoundError, InvalidStateTransitionError } from "../lib/errors.generated";

export interface ReactivateContactPersonInput {
  id: string;
}

/**
 * Function: reactivateContactPerson
 *
 * Reactivates an INACTIVE contact person by setting status to ACTIVE.
 * Does NOT restore primary designation.
 */
export async function run(db: Transaction, input: ReactivateContactPersonInput) {
  const result = await executeTransition({
    db,
    tableName: "ContactPerson",
    statusField: "status",
    id: input.id,
    transition: "reactivate",
    lifecycle: contactPersonLifecycle,
    errors: {
      notFound: ContactPersonNotFoundError,
      invalidTransition: InvalidStateTransitionError,
    },
  });
  if (!result.ok) return result;
  return ok({ contact: result.value });
}
