from typing import Dict
from typing import Any


def get_stats_with_delta(stats: Dict[str, Any],
                         reference_stats: Dict[str, Any],
                         reference_type: str) -> Dict[str, Any]:
    """Given two stats dicts provides a stats dict with deltas."""
    reference_commit = reference_stats.get('commit')
    delta = dict(
        commit=stats.get('commit'),
        reference_type=reference_type,
        reference_commit=reference_commit
    )

    for key in ['files', 'suites', 'duration',
                'tests', 'tests_succ', 'tests_skip', 'tests_fail', 'tests_error',
                'runs', 'runs_succ', 'runs_skip', 'runs_fail', 'runs_error']:
        if key in stats and stats[key] is not None:
            if key == 'duration':
                val = dict(duration=stats[key])
            else:
                val = dict(number=stats[key])
            if key in reference_stats and reference_stats[key] is not None:
                val['delta'] = stats[key] - reference_stats[key]
            delta[key] = val

    return delta