#!/usr/bin/env python

from evennia.utils import logger

import requests

def location(character):
    """
    Return a tuple of physical location information about
    the player playing `character`.
    """
    session = character.sessions.get()[0]
    address = session.address
    if address == "127.0.0.1":
        return ("Localhost", None, None, address)

    response = requests.get(f'https://ipapi.co/{address}/json/',
                            timeout=1).json()
    logger.info(f"User location: {response}")
    city = response.get("city") or "Unknown City"
    region = response.get("region") or "Unknown Region"
    country = response.get("country_name") or "Unknown Country"
    return (city, region, country, address)
