import { ok, err, type CommandContext } from "@tailor-platform/erp-kit/core";
import type { Transaction } from "../generated/kysely-tailordb";
import { BankAccountNotFoundError } from "../lib/errors.generated";

export interface DeletePartnerBankAccountInput {
  id: string;
}

/**
 * Function: deletePartnerBankAccount
 *
 * Permanently removes a bank account from a partner.
 */
export async function run(
  db: Transaction,
  input: DeletePartnerBankAccountInput,
  _ctx: CommandContext,
) {
  const { id } = input;

  // 1. Check account exists
  const account = await db
    .selectFrom("PartnerBankAccount")
    .selectAll()
    .where("id", "=", id)
    .executeTakeFirst();

  if (!account) {
    return err(new BankAccountNotFoundError(id));
  }

  // 2. Delete the account
  await db.deleteFrom("PartnerBankAccount").where("id", "=", id).execute();

  return ok({ deleted: true });
}
