from typing import List
from typing import Tuple
import json


def validate(result_range: List[Tuple[str, int]]) -> List[Tuple[int, int]]:
    """
    Given a directory containing only JSON files produced by an allrepos crawl, this function
    validates that there are no holes in the crawled data

    Args:
    result_range
        Contiguous list of results as returned by an ordered_crawl

    Returns: List of (start_id, max_id) pairs that are missing from the given range
    """
    if len(result_range) <= 1:
        return []

    missing_ranges: List[Tuple[int, int]] = []

    for i, pair in enumerate(result_range[:-1]):
        result_file, this_id = pair
        with open(result_file, 'r') as ifp:
            result = json.load(ifp)
        _, next_id = result_range[i+1]
        if not result.get('data'):
            missing_ranges.append((this_id, next_id))
            continue
        max_id = result['data'][-1].get('id', -1)
        if max_id != next_id:
            missing_ranges.append((max_id, next_id))

    return missing_ranges