#!/usr/bin/env python3
"""
Test script for the AWS-integrated configuration.
This demonstrates how the configuration works in different scenarios.
"""

import os
import sys

# Add the parent directory to the path so we can import the config
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def test_config_without_aws():
    """Test configuration fallback when AWS is not available."""
    print("🧪 Testing configuration without AWS dependencies...")
    
    # Mock the AWS environment to test fallback
    os.environ['DB_HOST'] = 'localhost'
    os.environ['DB_NAME'] = 'auto_clicker_test'
    os.environ['DB_USER'] = 'test_user'
    os.environ['DB_PASSWORD'] = 'test_password'
    os.environ['DB_PORT'] = '3306'
    
    try:
        # Change to the mysql directory to import config properly
        original_path = sys.path.copy()
        mysql_dir = os.path.dirname(os.path.abspath(__file__))
        sys.path.insert(0, mysql_dir)
        
        from config import MYSQL_CONFIG, validate_config
        
        print("✅ Configuration loaded successfully!")
        print(f"📊 Database: {MYSQL_CONFIG['database']}")
        print(f"🏠 Host: {MYSQL_CONFIG['host']}")
        print(f"👤 User: {MYSQL_CONFIG['user']}")
        print(f"🔌 Port: {MYSQL_CONFIG['port']}")
        
        # Test validation
        validate_config()
        print("✅ Configuration validation passed!")
        
        # Restore original path
        sys.path = original_path
        
    except Exception as e:
        print(f"❌ Configuration error: {e}")
        # Restore original path even on error
        if 'original_path' in locals():
            sys.path = original_path
        return False
    
    return True

def test_aws_environment_setup():
    """Test AWS environment configuration."""
    print("\n🌥️ Testing AWS environment setup...")
    
    environments = ['dev', 'qa']
    
    for env in environments:
        print(f"\n📂 Environment: {env}")
        
        # Set environment
        os.environ['ENV'] = env
        
        # The actual AWS configuration would be in the main config file
        # Here we just demonstrate the environment-specific settings
        
        configs = {
            'dev': {
                'secret_name': "rds!cluster-91f2520d-663f-4a7b-9f72-cc0878bdf354",
                'ssm_param_name': "rdsDetails"
            },
            'qa': {
                'secret_name': "rds!cluster-252d62f4-d31e-4fe7-a51e-7f96a6a82e3f",
                'ssm_param_name': "rdsDetailsQA"
            }
        }
        
        config = configs.get(env)
        print(f"  🔐 Secret Name: {config['secret_name']}")
        print(f"  📋 SSM Parameter: {config['ssm_param_name']}")

if __name__ == "__main__":
    print("🚀 Auto Clicker Configuration Test")
    print("=" * 50)
    
    # Test fallback configuration
    success = test_config_without_aws()
    
    # Test AWS environment setup
    test_aws_environment_setup()
    
    print("\n" + "=" * 50)
    if success:
        print("✅ All tests passed! Configuration is ready for deployment.")
        print("\n📋 Next steps for AWS deployment:")
        print("   1. Install dependencies: pip install boto3 python-dotenv")
        print("   2. Configure IAM role on EC2 instance")
        print("   3. Set ENV environment variable (dev/qa)")
        print("   4. Verify AWS Parameter Store and Secrets Manager access")
    else:
        print("❌ Some tests failed. Please check the configuration.")
