import { ok, executeTransition, type CommandContext } from "@tailor-platform/erp-kit/core";
import { channelLifecycle } from "../db/channel.lifecycle.generated";
import type { Transaction } from "../generated/kysely-tailordb";
import { ChannelNotFoundError, InvalidChannelStatusError } from "../lib/errors.generated";

export interface DeactivateChannelInput {
  channelId: string;
}

export async function run(db: Transaction, input: DeactivateChannelInput, ctx: CommandContext) {
  void ctx;
  const result = await executeTransition({
    db,
    tableName: "Channel",
    statusField: "status",
    id: input.channelId,
    transition: "deactivate",
    lifecycle: channelLifecycle,
    errors: { notFound: ChannelNotFoundError, invalidTransition: InvalidChannelStatusError },
  });
  if (!result.ok) return result;
  return ok({ channel: result.value });
}
