import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { listingLifecycle } from "../db/listing.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { InvalidListingStatusError, ListingNotFoundError } from "../lib/errors.generated";

export interface PauseListingInput {
  listingId: string;
}

export async function run(db: Transaction, input: PauseListingInput, ctx: CommandContext) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "Listing",
    statusField: "status",
    id: input.listingId,
    transition: "pause",
    lifecycle: listingLifecycle,
    errors: { notFound: ListingNotFoundError, invalidTransition: InvalidListingStatusError },
  });
  if (!result.ok) return result;
  return ok({ listing: result.value });
}
