#!/usr/bin/env python3
"""
Diagnostic script to test LyngSat parser connectivity in Docker environment.
Run this inside the container to verify FlareSolver connection.

Usage:
    # Inside Docker container:
    python test_lyngsat_connection.py
    
    # Or with Django environment:
    python manage.py shell < test_lyngsat_connection.py
"""

import os
import sys
import requests
from datetime import datetime

# Add Django project to path
sys.path.insert(0, '/app')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dbapp.settings.production')


def test_connection(url: str, timeout: int = 10) -> dict:
    """Test connection to a given URL"""
    result = {
        "url": url,
        "success": False,
        "status_code": None,
        "error": None,
        "response_time": None
    }
    
    try:
        start_time = datetime.now()
        response = requests.get(url, timeout=timeout)
        end_time = datetime.now()
        
        result["success"] = True
        result["status_code"] = response.status_code
        result["response_time"] = (end_time - start_time).total_seconds()
        
    except requests.exceptions.ConnectionError as e:
        result["error"] = f"Connection Error: {str(e)}"
    except requests.exceptions.Timeout as e:
        result["error"] = f"Timeout Error: {str(e)}"
    except Exception as e:
        result["error"] = f"Unexpected Error: {str(e)}"
    
    return result


def test_flaresolverr(base_url: str) -> dict:
    """Test FlareSolver API endpoint"""
    result = {
        "base_url": base_url,
        "success": False,
        "error": None,
        "response_time": None
    }
    
    try:
        start_time = datetime.now()
        payload = {
            "cmd": "request.get",
            "url": "https://www.lyngsat.com/europe.html",
            "maxTimeout": 60000
        }
        
        response = requests.post(f"{base_url}/v1", json=payload, timeout=30)
        end_time = datetime.now()
        
        result["success"] = response.status_code == 200
        result["status_code"] = response.status_code
        result["response_time"] = (end_time - start_time).total_seconds()
        
        if response.status_code == 200:
            json_response = response.json()
            result["solution_status"] = json_response.get("status")
            result["has_solution"] = "solution" in json_response
        
    except requests.exceptions.ConnectionError as e:
        result["error"] = f"Connection Error: {str(e)}"
    except requests.exceptions.Timeout as e:
        result["error"] = f"Timeout Error: {str(e)}"
    except Exception as e:
        result["error"] = f"Unexpected Error: {str(e)}"
    
    return result


def print_result(title: str, result: dict):
    """Pretty print test result"""
    print(f"\n{'='*60}")
    print(f"  {title}")
    print(f"{'='*60}")
    
    for key, value in result.items():
        if value is not None:
            status_icon = "✓" if (key == "success" and value) else ("✗" if key == "success" else "→")
            print(f"{status_icon} {key}: {value}")


def main():
    print("\n" + "="*60)
    print("  LyngSat Parser Connection Diagnostic")
    print("="*60)
    print(f"Timestamp: {datetime.now().isoformat()}")
    print(f"Python: {sys.version}")
    
    # Environment info
    print("\n" + "-"*60)
    print("Environment Variables:")
    print("-"*60)
    env_vars = [
        "DJANGO_SETTINGS_MODULE",
        "CELERY_BROKER_URL", 
        "REDIS_URL",
        "FLARESOLVERR_URL",
        "DB_HOST",
        "DB_NAME"
    ]
    for key in env_vars:
        value = os.environ.get(key, "NOT SET")
        print(f"  {key}: {value}")
    
    # Try to load Django settings
    print("\n" + "-"*60)
    print("Django Settings:")
    print("-"*60)
    try:
        import django
        django.setup()
        from django.conf import settings
        print(f"  FLARESOLVERR_URL from settings: {getattr(settings, 'FLARESOLVERR_URL', 'NOT SET')}")
        print(f"  CELERY_BROKER_URL from settings: {getattr(settings, 'CELERY_BROKER_URL', 'NOT SET')}")
        print(f"  DEBUG: {settings.DEBUG}")
    except Exception as e:
        print(f"  ✗ Cannot load Django settings: {str(e)}")
    
    # Test different FlareSolver URLs
    test_urls = [
        ("localhost:8191", "http://localhost:8191"),
        ("flaresolverr:8191 (Docker service)", "http://flaresolverr:8191"),
        ("127.0.0.1:8191", "http://127.0.0.1:8191"),
    ]
    
    print("\n" + "-"*60)
    print("Testing Basic Connectivity:")
    print("-"*60)
    
    for name, url in test_urls:
        result = test_connection(url)
        print_result(f"Connection Test: {name}", result)
    
    # Test FlareSolver API
    print("\n" + "-"*60)
    print("Testing FlareSolver API:")
    print("-"*60)
    
    api_urls = [
        ("localhost", "http://localhost:8191"),
        ("flaresolverr (Docker)", "http://flaresolverr:8191"),
    ]
    
    for name, url in api_urls:
        result = test_flaresolverr(url)
        print_result(f"FlareSolver API Test: {name}", result)
    
    # Test with actual parser
    print("\n" + "-"*60)
    print("Testing LyngSat Parser:")
    print("-"*60)
    
    try:
        from lyngsatapp.parser import LyngSatParser
        from django.conf import settings
        
        # Get URL from settings
        settings_url = getattr(settings, 'FLARESOLVERR_URL', 'http://flaresolverr:8191/v1')
        print(f"\nUsing FLARESOLVERR_URL from settings: {settings_url}")
        
        # Test with different URLs
        test_parser_urls = [
            ("Settings URL", settings_url),
            ("localhost", "http://localhost:8191/v1"),
            ("flaresolverr", "http://flaresolverr:8191/v1"),
        ]
        
        for name, url in test_parser_urls:
            print(f"\nTesting parser with {name} ({url})...")
            try:
                parser = LyngSatParser(flaresolver_url=url, regions=["europe"], target_sats=["express-am6"])
                print(f"  ✓ Parser initialized")
                
                # Try to get one region page (with timeout)
                print(f"  → Fetching region page (this may take 10-30 seconds)...")
                html_pages = parser.get_region_pages(["europe"])
                if html_pages and html_pages[0]:
                    print(f"  ✓ Successfully fetched page (length: {len(html_pages[0])} chars)")
                    # Check if it contains expected content
                    if "lyngsat" in html_pages[0].lower():
                        print(f"  ✓ Page contains expected LyngSat content")
                    break  # Success, no need to test other URLs
                else:
                    print(f"  ✗ Failed to fetch page (empty response)")
                    
            except Exception as e:
                print(f"  ✗ Parser error: {str(e)}")
                import traceback
                print(f"  Traceback: {traceback.format_exc()}")
                
    except ImportError as e:
        print(f"  ✗ Cannot import parser: {str(e)}")
    except Exception as e:
        print(f"  ✗ Unexpected error: {str(e)}")
    
    # Recommendations
    print("\n" + "="*60)
    print("  Recommendations:")
    print("="*60)
    print("""
1. If 'flaresolverr:8191' works but 'localhost:8191' doesn't:
   → Update parser to use 'flaresolverr:8191' in Docker environment
   
2. If none work:
   → Check if FlareSolver container is running: docker ps | grep flaresolverr
   → Check Docker network: docker network inspect <network_name>
   → Check FlareSolver logs: docker logs flaresolverr
   
3. To fix in code:
   → Use environment variable for FlareSolver URL
   → Default to 'flaresolverr:8191' in production
   → Use 'localhost:8191' only in local development
    """)
    
    print("\n" + "="*60)
    print("  Diagnostic Complete")
    print("="*60 + "\n")


if __name__ == "__main__":
    main()
