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

export type GetUnitInput = { id: string } | { symbol: string };

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

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

  const unit = await query.executeTakeFirst();

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