"""
Test upload script for Splicing
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 SPLICING 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"Fixes Applied:")
print(f"  - Changed 'name' field to 'aka' field")
print(f"  - Fixed endpoint: /map-splice/create")
print(f"  - Added 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 splicing
print("📤 Testing splicing.shp...")
shapefile_path = TEMP_DIR / "splicing.shp"
if shapefile_path.exists():
    try:
        count, errors = uploader._upload_splicing(shapefile_path, MAP_ID, LIMIT)
        results["uploaded"]["splicing.shp"] = count
        if errors:
            results["errors"].extend(errors)
        print(f"✅ Uploaded {count} splicing points")
    except Exception as e:
        error_msg = f"Error uploading splicing.shp: {str(e)}"
        print(f"❌ {error_msg}")
        results["errors"].append(error_msg)
        results["success"] = False
else:
    print("⚠️  splicing.shp not found")

# Display results
print()
print("=" * 60)
print("SPLICING 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)
