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

export interface GetSalesOrderInput {
  id: string;
}

export async function run(db: ReadonlyDB<DB>, input: GetSalesOrderInput) {
  const aggregate = await loadSalesOrderAggregate(db, input.id);
  return ok({
    salesOrder: aggregate
      ? {
          ...aggregate.salesOrder,
          lines: aggregate.lines,
          fulfillmentTotals: aggregate.fulfillmentTotals,
        }
      : null,
  });
}
