/**
 * Métricas homogéneas post-commit CSV (contrato tipo pedimentos + extras p. ej. facturas).
 */
export function totalSkippedFromCommit(cr: Record<string, unknown> | null | undefined): number {
	if (!cr || typeof cr !== 'object') return 0;
	const n = (k: string) => {
		const v = (cr as Record<string, unknown>)[k];
		if (typeof v === 'number' && Number.isFinite(v)) return v;
		if (typeof v === 'string' && v.trim() !== '') return parseInt(v, 10) || 0;
		return 0;
	};
	return (
		n('skipped_invalid') +
		n('skipped_missing_fk') +
		n('skipped_missing_invoice') +
		n('skipped_duplicate')
	);
}

export function criticalReferenceGaps(cr: Record<string, unknown> | null | undefined): number {
	if (!cr || typeof cr !== 'object') return 0;
	const v = (cr as Record<string, unknown>).critical_reference_gaps;
	if (typeof v === 'number' && Number.isFinite(v)) return v;
	return 0;
}

export function referenceStateReady(cr: Record<string, unknown> | null | undefined): boolean {
	if (!cr || typeof cr !== 'object') return true;
	if ('reference_state_ready' in cr) {
		const v = (cr as Record<string, unknown>).reference_state_ready;
		if (typeof v === 'boolean') return v;
		if (v === 'True' || v === 'true') return true;
		if (v === 'False' || v === 'false') return false;
	}
	return criticalReferenceGaps(cr) === 0;
}
