// lib/accounting/core/hash-chain.ts

import { JournalEntry } from './journal-types';
import crypto from 'crypto';

export function computeJournalHash(entry: JournalEntry): string {
  // Deterministic hash of journal entry (excluding currentJournalHash)
  const data = JSON.stringify({
    id: entry.id,
    documentId: entry.documentId,
    postingSequence: entry.postingSequence,
    eventType: entry.eventType,
    previousJournalHash: entry.previousJournalHash,
    baseCurrency: entry.baseCurrency,
    transactionCurrency: entry.transactionCurrency,
    exchangeRateSnapshot: entry.exchangeRateSnapshot,
    currencyPrecision: entry.currencyPrecision,
    lines: entry.lines,
    createdAt: entry.createdAt,
    postingRuleVersion: entry.postingRuleVersion,
  });
  return crypto.createHash('sha256').update(data).digest('hex');
}
