import re
import os

def filter_minecraft_log(input_file, output_file):
    """Filter a Minecraft log file to keep only relevant chat messages and join/leave events"""
    
    chat_patterns = [
        # Chat messages
        r'\[(\d{2}:\d{2}:\d{2})\] \[Async Chat Thread.*?\]: (.+?) : (.+)',
        # Join/leave messages
        r'\[(\d{2}:\d{2}:\d{2})\] \[Server thread/INFO\]: (.+?) joined the game',
        r'\[(\d{2}:\d{2}:\d{2})\] \[Server thread/INFO\]: (.+?) left the game',
    ]
    
    filtered_lines = []
    
    with open(input_file, 'r', encoding='utf-8', errors='ignore') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
                
            # Check chat messages
            chat_match = re.search(chat_patterns[0], line)
            if chat_match:
                time = chat_match.group(1)
                player = chat_match.group(2)
                message = chat_match.group(3)
                filtered_lines.append(f"[{time}] {player}: {message}")
                continue
            
            # Check join messages
            join_match = re.search(chat_patterns[1], line)
            if join_match:
                time = join_match.group(1)
                player = join_match.group(2)
                filtered_lines.append(f"[{time}]  : {player} joined the game")
                continue
                
            # Check leave messages
            leave_match = re.search(chat_patterns[2], line)
            if leave_match:
                time = leave_match.group(1)
                player = leave_match.group(2)
                filtered_lines.append(f"[{time}]  : {player} left the game")
                continue
    
    # Write filtered content
    with open(output_file, 'w', encoding='utf-8') as f:
        for line in filtered_lines:
            f.write(line + '\n')
    
    print(f"Processed {input_file} -> {output_file}")
    print(f"Found {len(filtered_lines)} relevant lines")

# Process the two missing THESUR-1 logs
raw_logs_dir = "raw logs/THESUR-1"
chat_logs_dir = "chat-logs"

# Process 17. August 2020
filter_minecraft_log(
    os.path.join(raw_logs_dir, "2020-08-17-1.log"),
    os.path.join(chat_logs_dir, "thesur-1-2020-08-17-1-filtered.txt")
)

# Process 31. August 2020
filter_minecraft_log(
    os.path.join(raw_logs_dir, "2020-08-31-1.log"),
    os.path.join(chat_logs_dir, "thesur-1-2020-08-31-1-filtered.txt")
)
