import type { Finding } from "../../../schema/src/records/finding.js";

export type Certainty = Finding["certainty"];

export type TruthSource =
  | "universal"
  | "confirmed"
  | "inferred"
  | "observed"
  | "legacy";

export type ProvenancedEvidence = {
  truthSource: TruthSource;
  payload: unknown;
  visionOnly?: boolean;
};

export type DeriveCertaintyOptions = {
  hasActiveContract?: boolean;
};

export function deriveCertainty(
  evidence: ProvenancedEvidence[],
  options: DeriveCertaintyOptions = {},
): Certainty {
  if (evidence.length === 0) {
    throw new Error("deriveCertainty requires at least one provenanced evidence item");
  }

  if (evidence.some((item) => item.truthSource === "universal")) {
    return "proven";
  }

  if (options.hasActiveContract === true) {
    return "confirmed-contract";
  }

  if (evidence.every((item) => item.visionOnly === true)) {
    return "visual-advisory";
  }

  return "inferred";
}
