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

export interface GetPurchasePriceListInput {
  id: string;
}

export async function run(db: ReadonlyDB<DB>, input: GetPurchasePriceListInput) {
  const purchasePriceList = await db
    .selectFrom("PurchasePriceList")
    .selectAll()
    .where("id", "=", input.id)
    .executeTakeFirst();
  if (!purchasePriceList) {
    return ok({ purchasePriceList: null });
  }

  const rules = await db
    .selectFrom("PurchasePriceRule")
    .selectAll()
    .where("priceListId", "=", input.id)
    .execute();

  return ok({
    purchasePriceList: {
      ...purchasePriceList,
      rules,
    },
  });
}
