"""
Google Maps — Geocoding Demo
=============================
A minimal, ready-to-run example of geocoding an address to coordinates.

Get an API key:
    1. Go to https://console.cloud.google.com/ and create a project.
    2. Enable the "Geocoding API" under APIs & Services -> Library.
    3. Under APIs & Services -> Credentials, click Create credentials ->
       API key. (A billing account is required, but there is a free
       monthly credit.)

Setup:
    pip install requests
    export GOOGLE_MAPS_API_KEY="..."

Run:
    python googlemaps_demo.py
"""

import os
import requests


def geocode(address: str) -> tuple:
    """Return (lat, lng) for an address."""
    response = requests.get(
        "https://maps.googleapis.com/maps/api/geocode/json",
        params={"address": address, "key": os.environ.get("GOOGLE_MAPS_API_KEY")},
    )
    response.raise_for_status()
    loc = response.json()["results"][0]["geometry"]["location"]
    return loc["lat"], loc["lng"]


if __name__ == "__main__":
    lat, lng = geocode("Wat Don Chan, Chiang Mai, Thailand")
    print(f"Coordinates: {lat}, {lng}")
