"""One-off script to build cities_by_region.json from dr5hn dataset."""
import json
import urllib.request
from pathlib import Path

DATA_DIR = Path(__file__).resolve().parent / 'data'
URL = (
    'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/'
    'master/json/countries+states+cities.json'
)


def main():
    print('Downloading nested geo data...')
    with urllib.request.urlopen(URL, timeout=300) as response:
        data = json.loads(response.read().decode())

    cities_index = {}
    for country in data:
        cc = country['iso2']
        for state in country.get('states') or []:
            sc = state.get('state_code') or state['name']
            names = sorted(
                {city['name'] for city in (state.get('cities') or []) if city.get('name')},
                key=str.casefold,
            )
            if not names:
                continue
            cities_index[f'{cc}|{sc}'] = names
            cities_index[f'{cc}|{state["name"]}'] = names

    out = DATA_DIR / 'cities_by_region.json'
    out.write_text(json.dumps(cities_index, ensure_ascii=False, separators=(',', ':')), encoding='utf-8')
    print(f'Wrote {len(cities_index)} keys, {out.stat().st_size / 1024 / 1024:.1f} MB')


if __name__ == '__main__':
    main()
