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

export interface GetSalesCreditNoteInput {
  id: string;
}

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