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

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

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

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

  const product = await query.executeTakeFirst();

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