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

export interface TechnicallyCompleteInternalOrderInput {
  internalOrderId: string;
}

export async function run(
  db: Transaction,
  input: TechnicallyCompleteInternalOrderInput,
  ctx: CommandContext,
) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "InternalOrder",
    statusField: "status",
    id: input.internalOrderId,
    transition: "technicallyComplete",
    lifecycle: internalOrderLifecycle,
    errors: { notFound: InternalOrderNotFoundError, invalidTransition: InvalidStateError },
  });
  if (!result.ok) return result;
  return ok({ internalOrder: result.value });
}
