import {
  computeBundleHash,
  DECISION_BUNDLE_VERSION,
  normalizeDecisionBundle,
  type BundleDecision,
  type DecisionBundle,
} from "../../../schema/src/decisions/bundle.js";

export type ReportRef = {
  id: string;
  revision: string;
  findingSetHash: string;
};

export type ExportBundleInput = {
  projectIdentity: string;
  configHash: string;
  report: ReportRef;
  decisions: BundleDecision[];
  nonce: string;
  expiresAt: string;
};

export function exportBundle(input: ExportBundleInput): DecisionBundle {
  const body = normalizeDecisionBundle({
    schemaVersion: DECISION_BUNDLE_VERSION,
    projectIdentity: input.projectIdentity,
    configHash: input.configHash,
    report: input.report,
    nonce: input.nonce,
    expiresAt: input.expiresAt,
    decisions: input.decisions,
  });

  return {
    ...body,
    bundleHash: computeBundleHash(body),
  };
}
