#!/usr/bin/env python3
"""
Basic tests for Sky Ceiling Projector.
"""

import pytest
import sys
import os

# Add the project root to the path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

def test_imports():
    """Test that all main modules can be imported."""
    try:
        import sky_projector
        assert sky_projector.__version__
        print(f"✅ Successfully imported sky_projector v{sky_projector.__version__}")
    except ImportError as e:
        pytest.fail(f"Failed to import sky_projector: {e}")

def test_geocoding():
    """Test the geocoding functionality."""
    from sky_projector import geocode_location
    
    try:
        lat, lon, tz, name = geocode_location("Paris, France")
        assert isinstance(lat, float)
        assert isinstance(lon, float)
        assert isinstance(tz, str)
        assert isinstance(name, str)
        assert -90 <= lat <= 90
        assert -180 <= lon <= 180
        print(f"✅ Geocoding test passed: {name} at {lat:.2f}, {lon:.2f} ({tz})")
    except Exception as e:
        # Geocoding might fail due to network issues, so we'll warn but not fail
        print(f"⚠️ Geocoding test skipped due to: {e}")

def test_classes_exist():
    """Test that main classes can be instantiated."""
    from sky_projector import (
        Star, Planet, DetailedCloud, EnhancedShootingStar,
        Satellite, MilkyWay, BrightFlare, SimpleParticle,
        SimpleLightning
    )
    
    # Test Star creation
    star = Star(100, 100, 0.8, 2.0)
    assert star.x == 100
    assert star.y == 100
    assert star.base_brightness == 0.8
    print("✅ Star class works")
    
    # Test Planet creation
    planet = Planet("Venus", 200, 200)
    assert planet.type == "Venus"
    assert planet.x == 200
    assert planet.y == 200
    print("✅ Planet class works")
    
    # Test other classes (without pygame dependencies)
    try:
        # These require screen dimensions
        cloud = DetailedCloud(50, 50, 100, 20, 0.8, 1920, 1080)
        shooting_star = EnhancedShootingStar(1920, 1080)
        satellite = Satellite(1920, 1080)
        milky_way = MilkyWay(1920, 1080)
        flare = BrightFlare(1920, 1080)
        particle = SimpleParticle("rain", 1920, 1080)
        lightning = SimpleLightning(1920, 1080)
        print("✅ All celestial object classes work")
    except Exception as e:
        print(f"⚠️ Some classes require pygame: {e}")

def test_simulator_creation():
    """Test that SkySimulator can be created without running."""
    try:
        # Import pygame first to see if it's available
        import pygame
        
        from sky_projector import SkySimulator
        
        # Test simulator creation (without calling run())
        simulator = SkySimulator(
            latitude=40.7128,
            longitude=-74.0060,
            timezone="America/New_York",
            location_name="Test Location",
            screen_width=800,
            screen_height=600,
            fullscreen=False
        )
        
        assert simulator.current_latitude == 40.7128
        assert simulator.current_longitude == -74.0060
        assert simulator.current_timezone == "America/New_York"
        assert simulator.current_location_name == "Test Location"
        assert simulator.screen_width == 800
        assert simulator.screen_height == 600
        
        # Clean up
        pygame.quit()
        print("✅ SkySimulator creation test passed")
        
    except ImportError:
        print("⚠️ Pygame not available, skipping SkySimulator test")
    except Exception as e:
        print(f"⚠️ SkySimulator test failed: {e}")

def test_version():
    """Test version string format."""
    import sky_projector
    version = sky_projector.__version__
    
    # Check it's a string and has expected format (x.y.z)
    assert isinstance(version, str)
    parts = version.split('.')
    assert len(parts) >= 2  # At least major.minor
    assert all(part.isdigit() or part.replace('a', '').replace('b', '').replace('rc', '').isdigit() 
              for part in parts)  # Allow alpha/beta/rc versions
    print(f"✅ Version format is valid: {version}")

if __name__ == "__main__":
    """Run tests when executed directly."""
    print("🧪 Running Sky Ceiling Projector Tests...")
    
    test_imports()
    test_geocoding()
    test_classes_exist()
    test_simulator_creation()
    test_version()
    
    print("\n✅ All tests completed!")