#!/usr/bin/env python3
"""
Add location names through reverse geocoding
Takes coordinates and converts them to human-readable city names
"""

import json
from geopy.geocoders import Nominatim
import time

def reverse_geocode(lat, lon):
    """Convert coordinates to city/region name"""
    try:
        geolocator = Nominatim(user_agent="farmmatch_reverse_geocoder")
        location = geolocator.reverse(f"{lat}, {lon}", language='en', timeout=10)

        if location and location.raw and 'address' in location.raw:
            address = location.raw['address']

            # Extract city name (try different fields)
            city = (address.get('city') or
                   address.get('town') or
                   address.get('village') or
                   address.get('municipality') or
                   address.get('county'))

            # Get country for context
            country = address.get('country', '')

            if city and country:
                return f"{city}, {country}"
            elif city:
                return city
            elif country:
                return country

        return None
    except Exception as e:
        print(f"    ⚠️ Reverse geocode error: {str(e)[:50]}")
        return None

def main():
    print("="*70)
    print("📍 Adding Location Names via Reverse Geocoding")
    print("="*70)

    # Load data
    with open('enriched_data.json', 'r') as f:
        data = json.load(f)

    # Find properties with coordinates but no location name
    need_names = [p for p in data if p.get('lat') and p.get('lon') and
                  (not p.get('location') or p.get('location') == 'Unknown')]

    print(f"\n📊 Properties needing location names: {len(need_names)}/{len(data)}")

    if not need_names:
        print("✅ All properties already have location names!")
        return

    # Reverse geocode each
    success = 0
    failed = 0

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

        print(f"\n[{i}/{len(need_names)}] {url.split('/')[-1]}")
        print(f"   Coords: {lat}, {lon}")

        location_name = reverse_geocode(lat, lon)

        if location_name:
            print(f"   ✅ {location_name}")
            # Update in original data list
            for p in data:
                if p.get('url') == url:
                    p['location'] = location_name
                    p['extracted_location'] = location_name.split(',')[0]  # Just city
                    break
            success += 1
        else:
            print(f"   ❌ Could not determine location name")
            failed += 1

        # Save progress every 10
        if i % 10 == 0:
            with open('enriched_data.json', 'w') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
            print(f"\n   💾 Progress saved ({success} successful)")

        time.sleep(1)  # Rate limiting

    # Final save
    with open('enriched_data.json', 'w') as f:
        json.dump(data, f, indent=2, ensure_ascii=False)

    print("\n" + "="*70)
    print("✅ REVERSE GEOCODING COMPLETE")
    print("="*70)
    print(f"Success: {success}")
    print(f"Failed: {failed}")
    print(f"\n🔄 Refresh the map viewer to see location names in list view!")

if __name__ == "__main__":
    main()
