import { ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core";
import type { DB } from "../generated/kysely-tailordb";

export interface GetPrimaryContactPersonInput {
  partnerId: string;
}

export async function run(db: ReadonlyDB<DB>, input: GetPrimaryContactPersonInput) {
  const contactPerson = await db
    .selectFrom("ContactPerson")
    .selectAll()
    .where("partnerId", "=", input.partnerId)
    .where("isPrimary", "=", true)
    .executeTakeFirst();

  return ok({ contactPerson: contactPerson ?? null });
}
