import re
import json
from pathlib import Path

CURRENT_DIR = Path(__file__).parent


def parse_emails(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        content = f.read()

    # Extract all <email> blocks
    email_blocks = re.findall(r"<email>(.*?)</email>", content, re.DOTALL)

    files_dict = {}

    for block in email_blocks:

        timestamp_match = re.search(r"Timestamp:\s*(.+)", block)
        assert timestamp_match is not None, f"Timestamp not found in {block}"

        date_str = timestamp_match.group(1).strip()

        # Format the filename and body
        file_name = "email_" + date_str.replace(":", "_")

        files_dict[file_name] = block

    return files_dict


# Main execution
if __name__ == "__main__":
    with open(f"{CURRENT_DIR}/emails.txt") as f:
        emails = f.read()

    with open(f"{CURRENT_DIR}/emails_output_json.json", "w", encoding="utf-8") as out_file:
        json.dump({"files": emails}, out_file, indent=2)
