#!/usr/bin/env python3
"""
Real-time log viewer for Viral Velocity
This script helps you monitor the logs while testing the system
"""

import os
import time
import subprocess
import sys

def view_logs(log_file, follow=True):
    """View logs in real-time"""
    if not os.path.exists(log_file):
        print(f"❌ Log file not found: {log_file}")
        return
    
    print(f"📋 Viewing logs from: {log_file}")
    print("Press Ctrl+C to stop viewing logs")
    print("-" * 50)
    
    try:
        if follow:
            # Use tail -f for real-time following
            subprocess.run(['tail', '-f', log_file])
        else:
            # Just show the last 50 lines
            subprocess.run(['tail', '-n', '50', log_file])
    except KeyboardInterrupt:
        print("\n👋 Stopped viewing logs")
    except FileNotFoundError:
        print("❌ 'tail' command not found. Please install it or use a different method.")

def main():
    """Main function to choose which logs to view"""
    print("🔍 Viral Velocity Log Viewer")
    print("=" * 30)
    print("1. View main scorer logs (viral_velocity.log)")
    print("2. View API logs (api.log)")
    print("3. View test logs (test.log)")
    print("4. View all logs in real-time")
    print("5. View last 50 lines of all logs")
    print("6. Exit")
    
    while True:
        try:
            choice = input("\nChoose an option (1-6): ").strip()
            
            if choice == "1":
                view_logs("viral_velocity.log")
            elif choice == "2":
                view_logs("api.log")
            elif choice == "3":
                view_logs("test.log")
            elif choice == "4":
                print("📋 Viewing all logs in real-time...")
                print("Press Ctrl+C to stop")
                subprocess.run(['tail', '-f', 'viral_velocity.log', 'api.log', 'test.log'])
            elif choice == "5":
                print("📋 Last 50 lines of all logs:")
                for log_file in ['viral_velocity.log', 'api.log', 'test.log']:
                    if os.path.exists(log_file):
                        print(f"\n--- {log_file} ---")
                        subprocess.run(['tail', '-n', '50', log_file])
                    else:
                        print(f"\n--- {log_file} (not found) ---")
            elif choice == "6":
                print("👋 Goodbye!")
                break
            else:
                print("❌ Invalid choice. Please enter 1-6.")
                
        except KeyboardInterrupt:
            print("\n👋 Goodbye!")
            break
        except Exception as e:
            print(f"❌ Error: {e}")

if __name__ == "__main__":
    main()
