#!/usr/bin/env python3
"""
Monitor backlog processing progress by checking logs and output files.
"""

import time
import os
from pathlib import Path
from datetime import datetime

def check_log_progress():
    """Check progress from log files."""
    log_dir = Path("test_logs/backlog")
    sources = ["Wordpress", "Instagram", "Mailchimp", "Podcast", "Youtube", "Tiktok"]
    
    print(f"\n{'='*60}")
    print(f"BACKLOG PROGRESS CHECK - {datetime.now().strftime('%H:%M:%S')}")
    print(f"{'='*60}")
    
    for source in sources:
        log_file = log_dir / source / f"{source.lower()}.log"
        if log_file.exists():
            # Get file size and recent lines
            size_mb = log_file.stat().st_size / (1024 * 1024)
            
            # Read last 10 lines
            try:
                with open(log_file, 'r', encoding='utf-8') as f:
                    lines = f.readlines()
                    recent_lines = lines[-3:] if len(lines) >= 3 else lines
                    
                print(f"\n{source}:")
                print(f"  Log size: {size_mb:.2f} MB")
                print(f"  Recent activity:")
                for line in recent_lines:
                    print(f"    {line.strip()}")
            except Exception as e:
                print(f"\n{source}: Error reading log - {e}")
        else:
            print(f"\n{source}: No log file yet")

def check_output_files():
    """Check generated markdown files."""
    data_dir = Path("test_data/backlog")
    
    print(f"\n{'='*30}")
    print("GENERATED FILES:")
    print(f"{'='*30}")
    
    if data_dir.exists():
        markdown_files = list(data_dir.glob("*.md"))
        print(f"Total markdown files: {len(markdown_files)}")
        
        for file in sorted(markdown_files):
            size_kb = file.stat().st_size / 1024
            print(f"  {file.name}: {size_kb:.1f} KB")
    else:
        print("No output directory yet")

if __name__ == "__main__":
    try:
        check_log_progress()
        check_output_files()
        print(f"\n{'='*60}")
        print("Monitoring continues... Use Ctrl+C to stop")
        print(f"{'='*60}")
    except KeyboardInterrupt:
        print("\nMonitoring stopped.")
    except Exception as e:
        print(f"Error: {e}")