#!/usr/bin/env python3
"""Stop cron and run test scan on 5 emails"""
import subprocess
import sys

# 1. Stop cron
print("🛑 Stopping cron scan...")
stop_cron = subprocess.run([
    'ssh', '-o', 'ConnectTimeout=10',
    'ubuntu@135.125.102.180',
    'crontab -l | grep -v scan.php | crontab -'
], capture_output=True, text=True)

if stop_cron.returncode == 0:
    print("✅ Cron scan stopped!")
else:
    print(f"⚠️ Cron stop: {stop_cron.stderr}")

# 2. Run test scan on 5 emails
print("\n🧪 Running test scan on 5 emails...")
test_scan = subprocess.run([
    'ssh', '-o', 'ConnectTimeout=10',
    'ubuntu@135.125.102.180',
    'cd /var/www/html/repondeur_mail_grok && /usr/bin/php scripts/scan.php 5 2>&1 | tail -100'
], capture_output=True, text=True, timeout=120)

print("\n" + "="*80)
print("SCAN OUTPUT:")
print("="*80)
print(test_scan.stdout)
if test_scan.stderr:
    print("\nERROR OUTPUT:")
    print(test_scan.stderr)

sys.exit(test_scan.returncode)
