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

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

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

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

  const category = await query.executeTakeFirst();

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