"""
Create and monitor a gSquare estimate using a point
"""
import json
import os
import sys
import requests
import time

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/")

# Ensure this point is on a rooftop!
LOCATION = [-93.18736857022286, 44.713507124491315]


def create_estimate(lon: float, lat: float) -> dict:
    """
    Use a longitude and latitude to create a request for a
    gSquare estimate.

    The longitude and latitude should be on a roof top.

    """

    url = f"{API_HOME}/gsquare/estimate"
    headers = {
        "Authorization": f"Api-Key {GEOSPAN_API_KEY}",
    }

    data = {
        "wkt": f"POINT({lon} {lat})",
        "includeImagery": False,
        "includeWeather": True,
    }

    res = requests.post(url, headers=headers, json=data)

    # Accept both 200 (OK) and 201 (Created) as valid responses
    if res.status_code not in (200, 201):
        raise ValueError(f"Failed to create estimate. Status code: {res.status_code}, Response: {res.text}")
    
    try:
        return res.json()
    except json.JSONDecodeError:
        raise ValueError(f"Response is not valid JSON. Status code: {res.status_code}, Response: {res.text}")


def check_estimate(query_key: str):
    """
    Check the query status

    Expected reuslts:

    {
        "queryKey": "sqm-abcdef12345"
    }

    """

    url = f"{API_HOME}/gsquare/query/{query_key}"
    headers = {
        "Authorization": f"Api-Key {GEOSPAN_API_KEY}",
    }

    res = requests.get(url, headers=headers)
    return res.json()


def run_example(silent=False):
    """
    Execute a gSquare query with a point.

    Expected results:

    {
      "state": "SUCCESS",
      "results": {
        "computedFootprint": "POLYGON ((-93.187349 44.713629, -93.187347 44.713569, -93.187328 44.713569, -93.187322 44.71344, -93.187381 44.713439, -93.187381 44.713425, -93.187413 44.713425, -93.187414 44.713441, -93.187441 44.713441, -93.18745 44.713627, -93.187349 44.713629))",
        "totalArea": {
          "area": 197.8059923890073,
          "units": "sqm"
        },
        "pitchResult": {
          "primaryPitch": 3,
          "deviation": 1
        },
        "confidence": 6,
        "imagery": [],
        "weather": [
          {
            "datecode": "231024",
            "hailSize": 1.0,
            "distance": 9.760984757545051
          },
          {
            "datecode": "240803",
            "hailSize": 1.0,
            "distance": 7.845437799202324
          }
        ]
      }
    }
    """

    # get the lon-lat for the address
    # start the gsquare estimate
    query_key = create_estimate(LOCATION[0], LOCATION[1])["queryKey"]

    # monitor the estimate until it is ready then pretty-print the results
    max_polls = 20
    while True:
        estimate = check_estimate(query_key)
        if estimate["state"] != "PENDING":
            if not silent:
                print(json.dumps(estimate, indent=2))
            if estimate["state"] != "SUCCESS":
                return 1
            return 0
        max_polls -= 1
        if max_polls == 0:
            return 1
        time.sleep(2)


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())
