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

export interface ReactivateCompanyInput {
  companyId: string;
}

/**
 * Function: reactivateCompany
 *
 * Transitions a company from INACTIVE back to ACTIVE status, re-enabling it
 * for use in new transactions.
 */
export async function run(db: Transaction, input: ReactivateCompanyInput) {
  const result = await executeTransition({
    db,
    tableName: "Company",
    statusField: "status",
    id: input.companyId,
    transition: "reactivate",
    lifecycle: companyLifecycle,
    errors: { notFound: CompanyNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ company: result.value });
}
