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

export interface ReactivateSiteInput {
  siteId: string;
}

/**
 * Function: reactivateSite
 *
 * Transitions a site from an inactive state back to active, making it
 * available for operational assignments again.
 */
export async function run(db: Transaction, input: ReactivateSiteInput) {
  const result = await executeTransition({
    db,
    tableName: "Site",
    statusField: "status",
    id: input.siteId,
    transition: "reactivate",
    lifecycle: siteLifecycle,
    errors: { notFound: SiteNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ site: result.value });
}
