import paramiko
import os

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

FILES_TO_UPLOAD = [
    ("core/PrestaShop.php", "core/PrestaShop.php"),
    ("public/action.php", "public/action.php"),
    ("public/search.php", "public/search.php"),
    ("public/includes/nav.php", "public/includes/nav.php"),
    ("public/includes/header.php", "public/includes/header.php"),
    ("core/AiEngine.php", "core/AiEngine.php"),
    ("core/SecurityScanner.php", "core/SecurityScanner.php"),
    ("scripts/scan.php", "scripts/scan.php"),
    ("scripts/recheck_queue.php", "scripts/recheck_queue.php"),
    ("public/index.php", "public/index.php")
]

def main():
    try:
        print(f"🔌 Connecting to {HOST}...")
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        sftp = ssh.open_sftp()
        print("✅ Connected.")

        print("\n📤 Uploading Search Feature files...")
        
        for local_rel, remote_rel in FILES_TO_UPLOAD:
            # Local path
            local_path = f"/home/dan/Documents🤮/🔴2026🔴/🔴----ROBOT-WHATSAPP-MARCHE-GroK----🔴/✍️ REPONDEUR_MAIL_GROK/{local_rel}"
            # Remote tmp path
            filename = os.path.basename(local_rel)
            remote_tmp = f"/tmp/{filename}"
            remote_dest = f"{REMOTE_BASE}/{remote_rel}"
            
            print(f"   - {local_rel} -> {remote_dest}")
            sftp.put(local_path, remote_tmp)
            
            # Sudo move and perm
            cmd = f"echo '{PASS}' | sudo -S cp {remote_tmp} {remote_dest} && echo '{PASS}' | sudo -S chmod 644 {remote_dest} && echo '{PASS}' | sudo -S chown www-data:www-data {remote_dest}"
            stdin, stdout, stderr = ssh.exec_command(cmd)
            err = stderr.read().decode()
            if err:
                 print(f"     ⚠️ {err}")

        print("✅ Upload complete.")
        ssh.close()
        print("\n🚀 DEPLOYMENT SUCCESSFUL!")

    except Exception as e:
        print(f"\n❌ Error: {e}")

if __name__ == "__main__":
    main()
