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 RetireListingInput {
  listingId: string;
}

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