"""
Test upload script for Network Elements
Uploads first 10 records to test the reverse-engineered fix
"""

import os
import sys
from pathlib import Path
from verofy_uploader import VerofyUploader

# Get credentials from environment
EMAIL = os.getenv("VEROFY_USER")
PASSWORD = os.getenv("VEROFY_PASS")

if not EMAIL or not PASSWORD:
    print("❌ Missing environment variables: Please set VEROFY_USER and VEROFY_PASS")
    sys.exit(1)

# Configuration
MAP_ID = 16950
TEMP_DIR = Path("../temp")
LIMIT = 10  # Upload only first 10 records

print("=" * 60)
print("VEROFY API NETWORK ELEMENTS TEST")
print("=" * 60)
print(f"Map ID: {MAP_ID}")
print(f"Temp Directory: {TEMP_DIR}")
print(f"Limit: {LIMIT} records")
print(f"Email: {EMAIL}")
print(f"Fix Applied: Added 'custom': 0 field + debug output")
print("=" * 60)
print()

# Initialize uploader
uploader = VerofyUploader(EMAIL, PASSWORD)

# Authenticate
if not uploader.authenticate():
    print("❌ Authentication failed")
    sys.exit(1)

results = {
    "success": True,
    "uploaded": {},
    "errors": []
}

# Test network elements
print("📤 Testing network_elements.shp...")
shapefile_path = TEMP_DIR / "network_elements.shp"
if shapefile_path.exists():
    try:
        count, errors = uploader._upload_network_elements(shapefile_path, MAP_ID, LIMIT)
        results["uploaded"]["network_elements.shp"] = count
        if errors:
            results["errors"].extend(errors)
        print(f"✅ Uploaded {count} network elements")
    except Exception as e:
        error_msg = f"Error uploading network_elements.shp: {str(e)}"
        print(f"❌ {error_msg}")
        results["errors"].append(error_msg)
        results["success"] = False
else:
    print("⚠️  network_elements.shp not found")

# Display results
print()
print("=" * 60)
print("NETWORK ELEMENTS TEST RESULTS")
print("=" * 60)
print(f"Success: {results['success']}")
print()
print("Uploaded counts:")
for shapefile, count in results.get('uploaded', {}).items():
    print(f"  {shapefile}: {count} records")

if results.get('errors'):
    print()
    print("Errors encountered:")
    for error in results['errors']:
        print(f"  - {error}")

print("=" * 60)
