import { createHash } from "crypto";
import type { DigitalSignatureType } from "@/types";

export function hashDigitalSignature(input: {
  signerName: string;
  signatureType: DigitalSignatureType;
  electionId: string;
  voterRollId: string;
  drawnPath?: string | null;
  consentAccepted: boolean;
}): string {
  const salt = process.env.IP_HASH_SALT ?? "surveystronghold-dev-only";
  return createHash("sha256")
    .update(
      [
        salt,
        input.electionId,
        input.voterRollId,
        input.signerName.trim().toLowerCase(),
        input.signatureType,
        input.drawnPath ?? "",
        input.consentAccepted ? "1" : "0",
      ].join("|")
    )
    .digest("hex");
}
