import requests


def create_new_corridor_report(
    access_token, start_date, end_date, corridors, granularity, map_version
):
    """Create new INRIX Roadway Analytics report for specified corridor(s).

    Args:
        access_token (str): Get from seperate endpoint
        start_date (str): "YYYY-MM-DD"
        end_date (str): "YYYY-MM-DD"
        corridors (list of dict): Each dict is a corridor definition with the format:
            {
                "name" : <corridor name (string)>,
                "direction" : <corridor direction (single char from N, S, W, E)>,
                "xdSegIds" : <xd segment ids (list of int)>
            }
        granularity (int): 1, 5, 15, or 60
        map_version (str): Version the Inrix XD map segmentation "1902", "2001", etc
    Returns:
        The ID of the newly created report
    More info:
        See Step 2 of the INRIX Roadway Analytics Data Downloader API documentation
    """
    endpoint = "https://roadway-analytics-api.inrix.com/v1/data-downloader"
    auth_header = {"Authorization": f"Bearer {access_token}"}

    # this report definition is similar to reportContents.json found in the 
    # zip file of any downloaded report
    report_def = {
        "unit": "IMPERIAL", 
        "fields": [
            "LOCAL_DATE_TIME", 
            "XDSEGID", 
            "UTC_DATE_TIME", 
            "SPEED", 
            "NAS_SPEED", 
            "REF_SPEED", 
            "TRAVEL_TIME", 
            "CVALUE", 
            "SCORE", 
            "CORRIDOR_REGION_NAME", 
            "CLOSURE"
        ], 
        "corridors" : corridors,
        "timezone": "America/Los_Angeles",
        "dateRanges": [{
            "start": start_date, 
            "end": end_date, 
            "daysOfWeek": [1, 2, 3, 4, 5, 6, 7]
        }], 
        "mapVersion": str(map_version), 
        "reportType": "DATA_DOWNLOAD", 
        "granularity": granularity, 
        "emailAddresses": [],
        "includeClosures": True
    }

    r = requests.post(endpoint, json=report_def, headers=auth_header)
    r.raise_for_status()
    return r.json()["reportId"]