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

export type GetProductVariantInput = { id: string } | { productId: string; axisValueKey: string };

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

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

  const variant = await query.executeTakeFirst();

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