import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { salesPaymentTermLifecycle } from "../db/salesPaymentTerm.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  InvalidSalespaymenttermStatusError,
  SalespaymenttermNotFoundError,
} from "../lib/errors.generated";

export interface DeactivateSalesPaymentTermInput {
  salesPaymentTermId: string;
}

export async function run(
  db: Transaction,
  input: DeactivateSalesPaymentTermInput,
  ctx: CommandContext,
) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "SalesPaymentTerm",
    statusField: "status",
    id: input.salesPaymentTermId,
    transition: "deactivate",
    lifecycle: salesPaymentTermLifecycle,
    errors: {
      notFound: SalespaymenttermNotFoundError,
      invalidTransition: InvalidSalespaymenttermStatusError,
    },
  });
  if (!result.ok) return result;
  return ok({ salesPaymentTerm: result.value });
}
