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

export type GetTaxonomyNodeInput = { id: string } | { code: string };

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

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

  const node = await query.executeTakeFirst();

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