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

export interface ActivatePartnerInput {
  id: string;
}

/**
 * Function: activatePartner
 *
 * Transitions a business partner from DRAFT to ACTIVE status.
 */
export async function run(db: Transaction, input: ActivatePartnerInput) {
  const result = await executeTransition({
    db,
    tableName: "BusinessPartner",
    statusField: "status",
    id: input.id,
    transition: "activatePartner",
    lifecycle: businessPartnerLifecycle,
    errors: { notFound: PartnerNotFoundError, invalidTransition: InvalidStateTransitionError },
  });
  if (!result.ok) return result;
  return ok({ partner: result.value });
}
