# SPDX-FileCopyrightText: 2026 Adafruit Industries
# SPDX-License-Identifier: MIT

"""CPython example of creating and deleting an Adafruit IO+ Weather location.

This feature is only available to Adafruit IO+ subscribers.
"""

import time
from os import getenv

import requests

from adafruit_io.adafruit_io import IO_HTTP

# Adafruit IO keys (set as environment variables)
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")

if not aio_username or not aio_key:
    raise RuntimeError("Missing ADAFRUIT_AIO_USERNAME or ADAFRUIT_AIO_KEY environment variables.")

io = IO_HTTP(aio_username, aio_key, requests)

# Location: Godrevy Lighthouse, Cornwall, UK
LOCATION = "50.2423591, -5.4001148"
NAME = "Godrevy Lighthouse, Cornwall" + time.strftime(" %Y-%m-%d %H:%M:%S")
PROVIDER = "open_meteo"

print("Creating weather location...")
created = io.create_weather(LOCATION, name=NAME, provider=PROVIDER)
location_id = created["id"]
print(f"Created weather location '{NAME}' with ID: {location_id}")

try:
    print("\nFetching weather data...")
    data = io.receive_weather(location_id)
    print("Available keys:", list(data.keys()))
    try:
        print('\n"last_requested_at":', data["last_requested_at"])
        print('"current":', data["current"])
    except KeyError:
        pass

    print("\nListing weather locations...")
    locations = io.get_weather()
    for loc in locations:
        print(loc["id"], loc["name"], loc["location"])
    found = any(loc.get("id") == location_id for loc in locations)
    print(f"\n\nCreated location present in list: {found}")
finally:
    print("\nDeleting weather location in 3s...")
    time.sleep(3)
    io.delete_weather(location_id)
    print("Deleted weather location.")
