#!/usr/bin/env python3
"""
Test script pro ověření stahování dat z GeoNames
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from geonames_to_sheets import GeoNamesDownloader
import json

def test_download():
    """Test download funkcionality"""
    print("🧪 Testing GeoNames data download...")
    
    downloader = GeoNamesDownloader()
    
    try:
        # Download data
        data = downloader.download_country_data()
        
        print(f"✅ Downloaded {len(data)} countries")
        
        # Show sample data
        if data:
            print("\n📋 Sample data (first 3 countries):")
            for i, country in enumerate(data[:3]):
                print(f"\n{i+1}. {country['country_name']} ({country['iso_alpha2']})")
                print(f"   Capital: {country['capital']}")
                print(f"   Population: {country['population']}")
                print(f"   Area: {country['area_km2']} km²")
                print(f"   Continent: {country['continent']}")
        
        # Save sample to JSON for inspection
        sample_file = "sample_countries.json"
        with open(sample_file, 'w', encoding='utf-8') as f:
            json.dump(data[:10], f, indent=2, ensure_ascii=False)
        print(f"\n💾 First 10 countries saved to {sample_file}")
        
        # Save CSV
        csv_file = downloader.save_to_csv("test_countries.csv")
        print(f"💾 Full data saved to {csv_file}")
        
        return True
        
    except Exception as e:
        print(f"❌ Test failed: {e}")
        return False

if __name__ == "__main__":
    test_download()