import paramiko
import sys
import os
import time

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
LOCAL_FILE = "public/index.php"
REMOTE_FILE = "/var/www/html/repondeur_mail_grok/public/index.php"

def main():
    print("Waiting 10s for connection cooldown...")
    time.sleep(10)
    
    for attempt in range(3):
        try:
            print(f"Attempt {attempt+1}/3: Connecting to {HOST}...")
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(HOST, username=USER, password=PASS, banner_timeout=30)
            
            sftp = ssh.open_sftp()
            local_path = os.path.abspath(LOCAL_FILE)
            print(f"Uploading {local_path} -> {REMOTE_FILE}")
            sftp.put(local_path, REMOTE_FILE)
            
            ssh.close()
            print("Deployment Complete.")
            return
        except Exception as e:
            print(f"Error: {e}")
            if attempt < 2:
                print("Retrying in 5s...")
                time.sleep(5)
            else:
                sys.exit(1)

if __name__ == "__main__":
    main()
