import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { salesPriceRuleLifecycle } from "../db/salesPriceRule.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  InvalidSalespriceruleStatusError,
  SalespriceruleNotFoundError,
} from "../lib/errors.generated";

export interface DeactivateSalesPriceRuleInput {
  salesPriceRuleId: string;
}

export async function run(
  db: Transaction,
  input: DeactivateSalesPriceRuleInput,
  ctx: CommandContext,
) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "SalesPriceRule",
    statusField: "status",
    id: input.salesPriceRuleId,
    transition: "deactivate",
    lifecycle: salesPriceRuleLifecycle,
    errors: {
      notFound: SalespriceruleNotFoundError,
      invalidTransition: InvalidSalespriceruleStatusError,
    },
  });
  if (!result.ok) return result;
  return ok({ salesPriceRule: result.value });
}
