#!/usr/bin/env python3
"""
Test GPS extraction from T-Embed files

Demonstrates automatic GPS population from filenames
"""

import sys
from pathlib import Path

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.parser.gps_extractor import GPSFilenameExtractor


def main():
    """Test GPS extraction on T-Embed files"""

    print("=" * 80)
    print("GPS EXTRACTION TEST - T-Embed Files")
    print("=" * 80)
    print()

    # Find T-Embed files
    tembed_dir = Path(__file__).parent.parent / 'signatures' / 't-embed-rf'

    if not tembed_dir.exists():
        print(f"❌ T-Embed directory not found: {tembed_dir}")
        return 1

    sub_files = list(tembed_dir.glob('*.sub'))
    print(f"Found {len(sub_files)} .sub files")
    print()

    # Test extraction
    extractor = GPSFilenameExtractor()

    with_gps = []
    without_gps = []

    for sub_file in sorted(sub_files):
        coords = extractor.extract(sub_file.name)

        if coords:
            with_gps.append((sub_file.name, coords))
            print(f"✅ {sub_file.name}")
            print(f"   📍 Latitude:  {coords.latitude:.6f}")
            print(f"   📍 Longitude: {coords.longitude:.6f}")
            print(f"   📋 Source:    {coords.source}")
        else:
            without_gps.append(sub_file.name)
            print(f"⏭️  {sub_file.name} - No GPS in filename")

        print()

    # Summary
    print("=" * 80)
    print("SUMMARY")
    print("=" * 80)
    print(f"Total files:         {len(sub_files)}")
    print(f"With GPS coords:     {len(with_gps)} ({len(with_gps)/len(sub_files)*100:.1f}%)")
    print(f"Without GPS coords:  {len(without_gps)} ({len(without_gps)/len(sub_files)*100:.1f}%)")
    print()

    if with_gps:
        print("Files with GPS coordinates:")
        for filename, coords in with_gps:
            print(f"  - {filename}")
            print(f"    {coords.latitude:.6f}, {coords.longitude:.6f}")

    print()

    if without_gps:
        print("Files without GPS (would need manual entry or JSON companion):")
        for filename in without_gps:
            print(f"  - {filename}")

    print()
    print("=" * 80)
    print("NEXT STEPS")
    print("=" * 80)
    print("1. Files with GPS in filename → Auto-populate coordinates")
    print("2. Files without GPS → Look for companion JSON files")
    print("3. If no JSON found → Require manual GPS entry during upload")
    print("=" * 80)

    return 0


if __name__ == '__main__':
    sys.exit(main())
