#!/usr/bin/env python3
"""
Enhance Property Locations with Detailed Geographic Information
Adds: country, state, district, county, municipality, postcode, etc.
"""
import json
import requests
import time
from pathlib import Path

def get_detailed_location(lat, lon):
    """
    Use Nominatim reverse geocoding to get detailed address components

    Returns dict with:
    - country
    - country_code
    - state
    - district
    - county
    - municipality
    - village/town/city
    - postcode
    - display_name (full address)
    """
    url = "https://nominatim.openstreetmap.org/reverse"
    params = {
        'lat': lat,
        'lon': lon,
        'format': 'json',
        'addressdetails': 1,
        'zoom': 18
    }
    headers = {
        'User-Agent': 'FarmMatch/1.0 (Property Analysis Tool)'
    }

    try:
        response = requests.get(url, params=params, headers=headers, timeout=10)

        if response.status_code == 200:
            data = response.json()
            address = data.get('address', {})

            # Extract all relevant location components
            location_details = {
                # Primary identifiers
                'country': address.get('country', ''),
                'country_code': address.get('country_code', '').upper(),

                # Administrative levels
                'state': address.get('state', ''),
                'region': address.get('region', ''),
                'province': address.get('province', ''),
                'district': address.get('district', ''),
                'county': address.get('county', ''),
                'municipality': address.get('municipality', ''),

                # Locality
                'city': address.get('city', ''),
                'town': address.get('town', ''),
                'village': address.get('village', ''),
                'hamlet': address.get('hamlet', ''),
                'suburb': address.get('suburb', ''),

                # Other
                'postcode': address.get('postcode', ''),
                'display_name': data.get('display_name', ''),

                # OSM metadata
                'osm_type': data.get('osm_type', ''),
                'osm_id': data.get('osm_id', ''),
                'place_id': data.get('place_id', '')
            }

            # Determine locality name (use first available)
            locality = (
                location_details.get('city') or
                location_details.get('town') or
                location_details.get('village') or
                location_details.get('hamlet') or
                location_details.get('suburb') or
                ''
            )
            location_details['locality'] = locality

            # Determine admin_level_1 (state/province/region)
            admin1 = (
                location_details.get('state') or
                location_details.get('province') or
                location_details.get('region') or
                ''
            )
            location_details['admin_level_1'] = admin1

            # Determine admin_level_2 (district/county)
            admin2 = (
                location_details.get('district') or
                location_details.get('county') or
                location_details.get('municipality') or
                ''
            )
            location_details['admin_level_2'] = admin2

            return location_details
        else:
            print(f"   ⚠️  HTTP {response.status_code}")
            return None

    except Exception as e:
        print(f"   ❌ Error: {e}")
        return None

def enhance_property_locations():
    """
    Read enriched_data.json and add detailed location information
    for all properties with coordinates
    """
    enriched_file = Path("enriched_data.json")

    if not enriched_file.exists():
        print("❌ enriched_data.json not found!")
        return

    print("🌍 Enhancing Property Locations with Detailed Geographic Data")
    print("=" * 70)

    # Load properties
    with open(enriched_file, 'r', encoding='utf-8') as f:
        properties = json.load(f)

    total = len(properties)
    enhanced = 0
    already_detailed = 0
    no_coords = 0
    failed = 0

    for i, prop in enumerate(properties, 1):
        url = prop.get('url', 'unknown')
        location = prop.get('location', 'Unknown')
        lat = prop.get('lat')
        lon = prop.get('lon')

        print(f"\n[{i}/{total}] {location}")
        print(f"   URL: {url}")

        # Check if already has detailed location data
        if prop.get('country') and prop.get('admin_level_1'):
            print(f"   ✓ Already has detailed location data")
            already_detailed += 1
            continue

        # Check coordinates
        if not lat or not lon:
            print(f"   ⚠️  No coordinates - skipping")
            no_coords += 1
            continue

        print(f"   📍 Coordinates: {lat}, {lon}")
        print(f"   🔍 Fetching detailed location...")

        # Get detailed location
        details = get_detailed_location(lat, lon)

        if details:
            # Add all location details to property
            prop.update(details)

            enhanced += 1
            print(f"   ✅ Enhanced:")
            print(f"      Country: {details.get('country', 'N/A')}")
            print(f"      State/Region: {details.get('admin_level_1', 'N/A')}")
            print(f"      District: {details.get('admin_level_2', 'N/A')}")
            print(f"      Locality: {details.get('locality', 'N/A')}")
            if details.get('postcode'):
                print(f"      Postcode: {details['postcode']}")

            # Save progress every 10 properties
            if (i % 10 == 0):
                with open(enriched_file, 'w', encoding='utf-8') as f:
                    json.dump(properties, f, indent=2, ensure_ascii=False)
                print(f"\n   💾 Progress saved ({i}/{total})")
        else:
            failed += 1
            print(f"   ❌ Failed to get location details")

        # Rate limiting - Nominatim requires 1 request per second
        if i < total:
            time.sleep(1.1)

    # Final save
    print("\n" + "=" * 70)
    print("💾 Saving final results...")
    with open(enriched_file, 'w', encoding='utf-8') as f:
        json.dump(properties, f, indent=2, ensure_ascii=False)

    # Summary
    print("\n" + "=" * 70)
    print("📊 ENHANCEMENT SUMMARY")
    print("=" * 70)
    print(f"Total properties: {total}")
    print(f"✅ Enhanced: {enhanced}")
    print(f"✓  Already detailed: {already_detailed}")
    print(f"⚠️  No coordinates: {no_coords}")
    print(f"❌ Failed: {failed}")
    print("\n✨ Location enhancement complete!")

    # Show example of enhanced data
    if enhanced > 0:
        print("\n" + "=" * 70)
        print("📋 EXAMPLE ENHANCED PROPERTY")
        print("=" * 70)

        for prop in properties:
            if prop.get('country') and prop.get('locality'):
                print(f"Location: {prop.get('location', 'Unknown')}")
                print(f"Country: {prop.get('country', 'N/A')} ({prop.get('country_code', 'N/A')})")
                print(f"State/Region: {prop.get('admin_level_1', 'N/A')}")
                print(f"District: {prop.get('admin_level_2', 'N/A')}")
                print(f"Locality: {prop.get('locality', 'N/A')}")
                if prop.get('postcode'):
                    print(f"Postcode: {prop['postcode']}")
                print(f"\nFull address: {prop.get('display_name', 'N/A')}")
                break

if __name__ == "__main__":
    enhance_property_locations()
