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

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

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

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

  const attribute = await query.executeTakeFirst();

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