import paramiko
import datetime

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

def main():
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        today = datetime.date.today().strftime('%Y-%m-%d')
        log_file = f"{REMOTE_BASE}/data/logs/app_{today}.log"
        
        print(f"🔍 Reading log file: {log_file}")
        
        stdin, stdout, stderr = ssh.exec_command(f"tail -n 50 {log_file}")
        log_content = stdout.read().decode()
        error = stderr.read().decode()
        
        if error and "No such file" in error:
            print("❌ Log file not found!")
            # Try listing the directory
            stdin, stdout, stderr = ssh.exec_command(f"ls -la {REMOTE_BASE}/data/logs/")
            print("Logs directory content:\n" + stdout.read().decode())
        else:
            print("\n--- LOG CONTENT (Last 50 lines) ---\n")
            print(log_content)
            
        ssh.close()
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    main()
