import paramiko
import sys
import os
import time

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

FILES = [
    ("public/action.php", f"{BASE_DIR}/public/action.php"),
    ("public/index.php", f"{BASE_DIR}/public/index.php")
]

def main():
    print("Waiting 3s cooldown...")
    time.sleep(3)
    try:
        print(f"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()
        for local, remote in FILES:
            tmp_remote = "/tmp/" + os.path.basename(remote)
            local_path = os.path.abspath(local)
            print(f"Uploading {local} -> {tmp_remote}")
            sftp.put(local_path, tmp_remote)
            
            print(f"Installing {remote}...")
            ssh.exec_command(f"sudo mv {tmp_remote} {remote}")
            ssh.exec_command(f"sudo chown www-data:www-data {remote}")
        
        sftp.close()
        ssh.close()
        print("Debug Update Deployed.")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()
