import paramiko
import sys
import os
import time
from datetime import datetime

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
LOG_DIR = "/var/www/html/repondeur_mail_grok/data/logs"

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, banner_timeout=30)
        
        # Determine log filename for today
        today = datetime.now().strftime("%Y-%m-%d")
        log_file = f"{LOG_DIR}/app_{today}.log"
        
        print(f"Reading tail of {log_file}...")
        stdin, stdout, stderr = ssh.exec_command(f"tail -n 50 {log_file}")
        
        lines = stdout.read().decode()
        if lines:
            print("--- LOG START ---")
            print(lines)
            print("--- LOG END ---")
        else:
            # Try yesterday just in case of timezone weirdness
            print(f"No output. Checking previous log if exists...")
            err = stderr.read().decode()
            print(f"STDERR: {err}")
            
        ssh.close()
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()
