import paramiko
import os

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
REMOTE_BASE = "/var/www/html/repondeur_mail_grok"

def main():
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        # Upload test script
        sftp = ssh.open_sftp()
        local_path = os.path.join(os.getcwd(), "scripts/test_email_credentials.php")
        remote_path = f"{REMOTE_BASE}/scripts/test_email_credentials.php"
        tmp_remote = f"/tmp/test_credentials_{os.getpid()}.php"
        
        print("📤 Upload du script de test...")
        sftp.put(local_path, tmp_remote)
        ssh.exec_command(f"sudo mv {tmp_remote} {remote_path}")
        ssh.exec_command(f"sudo chmod 755 {remote_path}")
        
        sftp.close()
        
        # Run test
        print("\n🧪 Exécution du test sur le VPS...\n")
        print("=" * 60)
        
        stdin, stdout, stderr = ssh.exec_command(f"cd {REMOTE_BASE} && php scripts/test_email_credentials.php")
        
        output = stdout.read().decode()
        error = stderr.read().decode()
        
        if output:
            print(output)
        
        if error:
            # Filter out common warnings
            errors = [e for e in error.split('\n') if e and 'Undefined array key' not in e and 'Warning' not in e]
            if errors:
                print("\n⚠️  ERREURS:")
                for err in errors:
                    print(err)
        
        ssh.close()
        
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()
