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

export interface GetSalesInvoiceInput {
  id: string;
}

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