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

export type GetItemInput = { id: string } | { sku: string } | { barcode: string };

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

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

  const item = await query.executeTakeFirst();

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