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

export type GetUoMCategoryInput = { id: string } | { name: string };

export async function run(db: ReadonlyDB<DB>, input: GetUoMCategoryInput) {
  let query = db.selectFrom("UoMCategory").selectAll();

  if ("id" in input) {
    query = query.where("id", "=", input.id);
  } else {
    query = query.where("name", "=", input.name);
  }

  const uoMCategory = await query.executeTakeFirst();

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