"""
Get GeoJSON from gPro API and query the Spatial API using a calculated bounding box
"""
import requests
import json
import os
import sys

GEOSPAN_API_KEY = os.getenv("GEOSPAN_API_KEY") or input("Please enter your API key: ")
API_HOME = os.getenv("GEOSPAN_API_HOME", "https://api.geospan.com/remote4d/v1/api/")

# Define the run_example function
def run_example(silent=False):
    min_lon, min_lat, max_lon, max_lat = [
        -93.18814705349445,
        44.71487409726919,
        -93.18663294650553,
        44.715445901318304,
    ]
    bounds = f"{min_lon},{min_lat},{max_lon},{max_lat}"

    # Step 3: Query the Spatial API using the calculated bounding box
    url = f"{API_HOME}/spatial/footprints"
    params = {"bounds": bounds}
    headers = {
        "Authorization": f"Api-Key {GEOSPAN_API_KEY}",
    }
    response = requests.get(url, params=params, headers=headers)
    response.raise_for_status()
    footprints_data = response.json()

    if not silent:
        # Output the footprints data
        print("Footprints data:")
        print(json.dumps(footprints_data, indent=2))

    return 0  # Return 0 to indicate success


if __name__ == "__main__":
    if not GEOSPAN_API_KEY:
        print("API key is required.")
        sys.exit(1)  # Exit with non-zero exit code for failure

    sys.exit(run_example())
