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 DeactivateSiteInput {
  siteId: string;
}

/**
 * Function: deactivateSite
 *
 * Transitions a site from an active state to inactive, preventing it from
 * being used in new operational assignments.
 */
export async function run(db: Transaction, input: DeactivateSiteInput) {
  const result = await executeTransition({
    db,
    tableName: "Site",
    statusField: "status",
    id: input.siteId,
    transition: "deactivate",
    lifecycle: siteLifecycle,
    errors: { notFound: SiteNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ site: result.value });
}
