#!/usr/bin/env python3
"""
Registry of Geofabrik download URLs for OSM PBF files.

Geofabrik URL Pattern:
https://download.geofabrik.de/{region}/{subregion}/{name}-latest.osm.pbf

Examples:
- US States: https://download.geofabrik.de/north-america/us/alabama-latest.osm.pbf
- Countries: https://download.geofabrik.de/asia/india-latest.osm.pbf
- Egypt: https://download.geofabrik.de/africa/egypt-latest.osm.pbf
"""

# Registry mapping location names to their Geofabrik paths
GEOFABRIK_REGISTRY = {
    # US States (North America)
    "alabama": "north-america/us/alabama",
    "alaska": "north-america/us/alaska",
    "arizona": "north-america/us/arizona",
    "arkansas": "north-america/us/arkansas",
    "california": "north-america/us/california",
    "colorado": "north-america/us/colorado",
    "connecticut": "north-america/us/connecticut",
    "delaware": "north-america/us/delaware",
    "florida": "north-america/us/florida",
    "georgia": "north-america/us/georgia",
    "hawaii": "north-america/us/hawaii",
    "idaho": "north-america/us/idaho",
    "illinois": "north-america/us/illinois",
    "indiana": "north-america/us/indiana",
    "iowa": "north-america/us/iowa",
    "kansas": "north-america/us/kansas",
    "kentucky": "north-america/us/kentucky",
    "louisiana": "north-america/us/louisiana",
    "maine": "north-america/us/maine",
    "maryland": "north-america/us/maryland",
    "massachusetts": "north-america/us/massachusetts",
    "michigan": "north-america/us/michigan",
    "minnesota": "north-america/us/minnesota",
    "mississippi": "north-america/us/mississippi",
    "missouri": "north-america/us/missouri",
    "montana": "north-america/us/montana",
    "nebraska": "north-america/us/nebraska",
    "nevada": "north-america/us/nevada",
    "new-hampshire": "north-america/us/new-hampshire",
    "new-jersey": "north-america/us/new-jersey",
    "new-mexico": "north-america/us/new-mexico",
    "new-york": "north-america/us/new-york",
    "north-carolina": "north-america/us/north-carolina",
    "north-dakota": "north-america/us/north-dakota",
    "ohio": "north-america/us/ohio",
    "oklahoma": "north-america/us/oklahoma",
    "oregon": "north-america/us/oregon",
    "pennsylvania": "north-america/us/pennsylvania",
    "rhode-island": "north-america/us/rhode-island",
    "south-carolina": "north-america/us/south-carolina",
    "south-dakota": "north-america/us/south-dakota",
    "tennessee": "north-america/us/tennessee",
    "texas": "north-america/us/texas",
    "utah": "north-america/us/utah",
    "vermont": "north-america/us/vermont",
    "virginia": "north-america/us/virginia",
    "washington": "north-america/us/washington",
    "west-virginia": "north-america/us/west-virginia",
    "wisconsin": "north-america/us/wisconsin",
    "wyoming": "north-america/us/wyoming",
    "district-of-columbia": "north-america/us/district-of-columbia",
    
    # Countries - Africa
    "egypt": "africa/egypt",
    "south-africa": "africa/south-africa",
    "kenya": "africa/kenya",
    "nigeria": "africa/nigeria",
    "morocco": "africa/morocco",
    
    # Countries - Asia
    "india": "asia/india",
    "thailand": "asia/thailand",
    "china": "asia/china",
    "japan": "asia/japan",
    "indonesia": "asia/indonesia",
    "vietnam": "asia/vietnam",
    "philippines": "asia/philippines",
    "malaysia": "asia/malaysia",
    "singapore": "asia/singapore",
    "pakistan": "asia/pakistan",
    "bangladesh": "asia/bangladesh",
    "south-korea": "asia/south-korea",
    "taiwan": "asia/taiwan",
    
    # Countries - Europe
    "germany": "europe/germany",
    "france": "europe/france",
    "italy": "europe/italy",
    "spain": "europe/spain",
    "united-kingdom": "europe/great-britain",
    "poland": "europe/poland",
    "netherlands": "europe/netherlands",
    "belgium": "europe/belgium",
    "greece": "europe/greece",
    "portugal": "europe/portugal",
    "sweden": "europe/sweden",
    "norway": "europe/norway",
    "finland": "europe/finland",
    "denmark": "europe/denmark",
    "switzerland": "europe/switzerland",
    "austria": "europe/austria",
    
    # German States (Europe)
    "baden-wuerttemberg": "europe/germany/baden-wuerttemberg",
    "bayern": "europe/germany/bayern",
    "berlin": "europe/germany/berlin",
    "brandenburg": "europe/germany/brandenburg",
    "bremen": "europe/germany/bremen",
    "hamburg": "europe/germany/hamburg",
    "hessen": "europe/germany/hessen",
    "mecklenburg-vorpommern": "europe/germany/mecklenburg-vorpommern",
    "niedersachsen": "europe/germany/niedersachsen",
    "nordrhein-westfalen": "europe/germany/nordrhein-westfalen",
    "rheinland-pfalz": "europe/germany/rheinland-pfalz",
    "saarland": "europe/germany/saarland",
    "sachsen": "europe/germany/sachsen",
    "sachsen-anhalt": "europe/germany/sachsen-anhalt",
    "schleswig-holstein": "europe/germany/schleswig-holstein",
    "thueringen": "europe/germany/thueringen",
    
    # Countries - South America
    "brazil": "south-america/brazil",
    "argentina": "south-america/argentina",
    "colombia": "south-america/colombia",
    "chile": "south-america/chile",
    "peru": "south-america/peru",
    
    # Countries - North America
    "mexico": "north-america/mexico",
    "canada": "north-america/canada",
    
    # Countries - Oceania
    "australia": "australia-oceania/australia",
    "new-zealand": "australia-oceania/new-zealand",
    
    # India zones (328MB, 227MB, 91MB, 206MB, 518MB, 191MB)
    "india-central-zone": "asia/india/central-zone",
    "india-eastern-zone": "asia/india/eastern-zone",
    "india-north-eastern-zone": "asia/india/north-eastern-zone",
    "india-northern-zone": "asia/india/northern-zone",
    "india-southern-zone": "asia/india/southern-zone",
    "india-western-zone": "asia/india/western-zone",
}


def get_geofabrik_url(location: str) -> str:
    """
    Get the Geofabrik download URL for a location.
    
    Args:
        location: Location name (e.g., 'ohio', 'egypt', 'india')
        
    Returns:
        Full download URL for the PBF file
        
    Raises:
        ValueError: If location not found in registry
    """
    location = location.lower()
    
    if location not in GEOFABRIK_REGISTRY:
        available = ", ".join(sorted(GEOFABRIK_REGISTRY.keys())[:10])
        raise ValueError(
            f"Location '{location}' not found in registry.\n"
            f"Available locations include: {available}...\n"
            f"Total locations: {len(GEOFABRIK_REGISTRY)}"
        )
    
    path = GEOFABRIK_REGISTRY[location]
    return f"https://download.geofabrik.de/{path}-latest.osm.pbf"


def list_available_locations():
    """Print all available locations in the registry."""
    print("\n📍 Available Locations in Geofabrik Registry:")
    print("=" * 60)
    
    # Group by region
    regions = {}
    for location, path in sorted(GEOFABRIK_REGISTRY.items()):
        region = path.split('/')[0]
        if region not in regions:
            regions[region] = []
        regions[region].append(location)
    
    for region, locations in sorted(regions.items()):
        print(f"\n{region.upper().replace('-', ' ')}:")
        for loc in locations:
            print(f"  - {loc}")
    
    print(f"\n📊 Total: {len(GEOFABRIK_REGISTRY)} locations")
    print("=" * 60)


if __name__ == "__main__":
    import sys
    
    if len(sys.argv) > 1:
        if sys.argv[1] == "list":
            list_available_locations()
        else:
            location = sys.argv[1]
            try:
                url = get_geofabrik_url(location)
                print(f"URL for {location}: {url}")
            except ValueError as e:
                print(f"Error: {e}")
    else:
        print("Usage:")
        print("  python -m scripts.geofabrik_registry list")
        print("  python -m scripts.geofabrik_registry <location>")
        list_available_locations()
