#!/usr/bin/env python3
import paramiko
import time

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, username=USER, password=PASS)

print("🔄 Launching NEW manual scan...")
print("=" * 80)

# Launch scan
stdin, stdout, stderr = ssh.exec_command("cd /var/www/html/repondeur_mail_grok && php scripts/scan.php 2>&1")

# Wait and show output in real-time
while True:
    line = stdout.readline()
    if not line:
        break
    print(line.rstrip())

exit_code = stdout.channel.recv_exit_status()
print("=" * 80)
print(f"✅ Scan finished with exit code: {exit_code}")

# Check how many marked as read
print("\n📊 Checking for 'Marked UID' messages...")
stdin2, stdout2, stderr2 = ssh.exec_command("grep 'Marked UID' /var/www/html/repondeur_mail_grok/data/logs/scan.log | tail -50")
marked = stdout2.read().decode()
if marked:
    print(marked)
    count = len([l for l in marked.split('\n') if 'Marked UID' in l])
    print(f"\n✅ {count} emails marked as READ!")
else:
    print("No emails were marked as read")

ssh.close()
