import os
import re

# Define paths
large_paste_path = os.path.join("Mailbox", "Large_Paste.txt")
calendar_path = os.path.join("Deliverables", "Transition_Calendar.html")
brief_path = os.path.join("Deliverables", "Temporary_Brief.html")
output_summary = os.path.join("Mailbox", "v1_summary.txt")

print("Python script starting parsing...")

# Read Large_Paste.txt
large_paste_content = ""
if os.path.exists(large_paste_path):
    with open(large_paste_path, 'r', encoding='utf-8', errors='ignore') as f:
        large_paste_content = f.read()
    print(f"Successfully read Large_Paste.txt ({len(large_paste_content)} characters)")
else:
    print("Warning: Large_Paste.txt not found")

# Read Transition_Calendar.html
calendar_content = ""
if os.path.exists(calendar_path):
    with open(calendar_path, 'r', encoding='utf-8', errors='ignore') as f:
        calendar_content = f.read()
    print(f"Successfully read Transition_Calendar.html ({len(calendar_content)} characters)")
else:
    print("Warning: Transition_Calendar.html not found")

# Read Temporary_Brief.html
brief_content = ""
if os.path.exists(brief_path):
    with open(brief_path, 'r', encoding='utf-8', errors='ignore') as f:
        brief_content = f.read()
    print(f"Successfully read Temporary_Brief.html ({len(brief_content)} characters)")
else:
    print("Warning: Temporary_Brief.html not found")

# Parse details and write summary
with open(output_summary, 'w', encoding='utf-8') as f:
    f.write("=== WORKSPACE VERIFICATION ===\n")
    f.write(f"Large_Paste.txt Size: {len(large_paste_content)} chars\n")
    f.write(f"Transition_Calendar.html Size: {len(calendar_content)} chars\n")
    f.write(f"Temporary_Brief.html Size: {len(brief_content)} chars\n\n")

    # Extract key elements from files
    f.write("=== HTML FILE METADATA ===\n")
    cal_titles = re.findall(r'<title>(.*?)</title>', calendar_content, re.IGNORECASE)
    f.write(f"Calendar Title: {cal_titles[0] if cal_titles else 'N/A'}\n")
    brief_titles = re.findall(r'<title>(.*?)</title>', brief_content, re.IGNORECASE)
    f.write(f"Brief Title: {brief_titles[0] if brief_titles else 'N/A'}\n\n")

    # Extract tables or schedule details from Large_Paste.txt
    f.write("=== PREVIOUS SESSION PROJECTS OUTLINE ===\n")
    # Search for markdown tables
    tables = re.findall(r'(\|.*?\|.*?[\r\n]+(?:\|[-:| ]+[\r\n]+)?(?:\|.*?\|.*?[\r\n]+)+)', large_paste_content)
    if tables:
        f.write(f"Found {len(tables)} tables in the paste. Listing first 3:\n\n")
        for idx, table in enumerate(tables[:3]):
            f.write(f"--- Table {idx+1} ---\n{table}\n")
    else:
        f.write("No markdown tables found in Large_Paste.txt.\n")

    # Write a clean chunk of Large_Paste.txt containing task/schedule keywords
    f.write("\n=== DETECTED SCHEDULING CONTEXT ===\n")
    lines = large_paste_content.splitlines()
    context_lines = []
    for line in lines:
        if any(keyword in line.lower() for keyword in ["phase", "timeline", "consecutive", "schedule", "calendar", "draft", "version", "project", "assignee", "day"]):
            context_lines.append(line)
    
    f.write(f"Found {len(context_lines)} scheduling-related lines. Top 50 lines:\n")
    for line in context_lines[:50]:
        f.write(f"{line}\n")

    f.write("\n=== FIRST 8000 CHARACTERS OF LARGE PASTE ===\n")
    f.write(large_paste_content[:8000])

print("Processing complete. Summary written to", output_summary)
