import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { salesPriceListLifecycle } from "../db/salesPriceList.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  InvalidSalespricelistStatusError,
  SalespricelistNotFoundError,
} from "../lib/errors.generated";

export interface DeactivateSalesPriceListInput {
  salesPriceListId: string;
}

export async function run(
  db: Transaction,
  input: DeactivateSalesPriceListInput,
  ctx: CommandContext,
) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "SalesPriceList",
    statusField: "status",
    id: input.salesPriceListId,
    transition: "deactivate",
    lifecycle: salesPriceListLifecycle,
    errors: {
      notFound: SalespricelistNotFoundError,
      invalidTransition: InvalidSalespricelistStatusError,
    },
  });
  if (!result.ok) return result;
  return ok({ salesPriceList: result.value });
}
