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

export interface ReactivateDepartmentInput {
  departmentId: string;
}

/**
 * Function: reactivateDepartment
 *
 * Re-enables a previously deactivated department, making it available
 * for new assignments.
 */
export async function run(db: Transaction, input: ReactivateDepartmentInput) {
  const result = await executeTransition({
    db,
    tableName: "Department",
    statusField: "status",
    id: input.departmentId,
    transition: "reactivate",
    lifecycle: departmentLifecycle,
    errors: { notFound: DepartmentNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ department: result.value });
}
