import { createHash } from 'crypto';
import { logger } from '@/infrastructure/monitoring/logger';

/**
 * dbis.data.evidence-ledger
 * Immutable manifests + hash-chained references (scaffold, in-memory).
 */
export class EvidenceLedgerService {
	private chain: Array<{ txnId: string; hash: string; prevHash?: string; manifest: Record<string, unknown> }> = [];

	sealEvidence(params: { txnId: string; manifest: Record<string, unknown> }): { hash: string } {
		const prevHash = this.chain.length > 0 ? this.chain[this.chain.length - 1].hash : undefined;
		const hash = createHash('sha256')
			.update(JSON.stringify({ txnId: params.txnId, manifest: params.manifest, prevHash: prevHash || '' }))
			.digest('hex');
		this.chain.push({ txnId: params.txnId, hash, prevHash, manifest: params.manifest });
		logger.info('EvidenceLedgerService.sealEvidence', { txnId: params.txnId, hash });
		return { hash };
	}
}

export const evidenceLedgerService = new EvidenceLedgerService();

