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

export interface DeletePartnerIdentificationInput {
  id: string;
}

/**
 * Function: deletePartnerIdentification
 *
 * Deletes a partner identification record.
 * Validates the identification exists before deletion.
 */
export async function run(
  db: Transaction,
  input: DeletePartnerIdentificationInput,
  _ctx: CommandContext,
) {
  const { id } = input;

  // 1. Check identification exists
  const existing = await db
    .selectFrom("PartnerIdentification")
    .selectAll()
    .where("id", "=", id)
    .executeTakeFirst();

  if (!existing) {
    return err(new IdentificationNotFoundError(id));
  }

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

  return ok({ id });
}
