import paramiko
import sys
import os

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
REMOTE_BASE = "/var/www/html/repondeur_mail_grok"
LOCAL_FILE = "scripts/test_final_tagging.php"
REMOTE_FILE = f"{REMOTE_BASE}/scripts/test_final_tagging.php"

def main():
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        # Upload
        sftp = ssh.open_sftp()
        print(f"Uploading {LOCAL_FILE}...")
        sftp.put(os.path.abspath(LOCAL_FILE), REMOTE_FILE)
            
        # Execute
        print("Running test (This will take 60s+ due to pauses)...")
        # -u for unbuffered python, but here we run PHP.
        # We want to see output live if possible, or wait.
        stdin, stdout, stderr = ssh.exec_command(f"php {REMOTE_FILE}")
        
        # Stream output
        while True:
            line = stdout.readline()
            if not line: break
            print(line, end="")
            
        print(stderr.read().decode())
        
        ssh.close()
    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()
