import os
import random
from datetime import datetime, timedelta

OUTPUT_FOLDER = r"C:\Temp\projectElectronicDiscovery"

os.makedirs(OUTPUT_FOLDER, exist_ok=True)

first_names = [
    "John", "Jane", "Michael", "Sarah", "David",
    "Emily", "Robert", "Jessica", "William", "Ashley",
    "James", "Elizabeth", "Christopher", "Amanda", "Daniel"
]

last_names = [
    "Smith", "Johnson", "Williams", "Brown",
    "Jones", "Garcia", "Miller", "Davis",
    "Wilson", "Moore", "Taylor", "Anderson"
]

domains = [
    "contoso.com",
    "fabrikam.com",
    "adventureworks.com",
    "northwind.com",
    "wingtiptoys.com"
]

subjects = [
    "Quarterly Budget Review",
    "Contract Negotiation",
    "Project Phoenix Status",
    "Personnel Discussion",
    "Confidential Legal Matter",
    "Vendor Assessment",
    "Policy Update",
    "Board Meeting Notes",
    "HR Investigation",
    "Customer Escalation",
    "Data Retention Review",
    "Compliance Audit",
    "Litigation Hold Notice",
    "Discovery Request",
    "Financial Analysis"
]

message_templates = [
    """
Team,

Attached is the latest review package. Please review the
documentation prior to Friday's meeting and provide comments.

Regards
""",
    """
Good Afternoon,

Following our conversation, please see the updated material.
Legal has requested all revisions remain confidential until
the matter is resolved.

Thank you.
""",
    """
Hello,

The project remains on schedule. We anticipate completion
within the next reporting cycle.

Please advise if additional information is needed.

Thanks.
""",
    """
Confidential Communication

This message contains information intended only for the
recipients listed. Please do not distribute without
authorization.

Regards.
""",
    """
This email serves as a follow-up to the previous discussion.
Several action items remain open and should be addressed
during the next committee meeting.

Sincerely.
"""
]

def random_person():
    first = random.choice(first_names)
    last = random.choice(last_names)

    display_name = f"{first} {last}"
    email = f"{first.lower()}.{last.lower()}@{random.choice(domains)}"

    return display_name, email

start_date = datetime(2023, 1, 1)

for i in range(1, 501):

    sender_name, sender_email = random_person()
    recipient_name, recipient_email = random_person()

    msg_date = start_date + timedelta(
        days=random.randint(0, 1200),
        seconds=random.randint(0, 86400)
    )

    subject = random.choice(subjects)

    message_id = (
        f"<MSG-{i:06}-{random.randint(100000,999999)}"
        f"@{random.choice(domains)}>"
    )

    body = random.choice(message_templates)

    email_text = f"""Return-Path: <{sender_email}>
Received: from smtp.{sender_email.split('@')[1]} by mailserver.local
Message-ID: {message_id}
Date: {msg_date.strftime('%a, %d %b %Y %H:%M:%S')}
From: {sender_name} <{sender_email}>
To: {recipient_name} <{recipient_email}>
Subject: {subject}
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-Priority: {random.randint(1,5)}
X-Mailer: Microsoft Outlook

{body}

Case Reference: CASE-{random.randint(1000,9999)}
Document Reference: DOC-{i:06}
Classification: Internal Use Only
"""

    filename = os.path.join(
        OUTPUT_FOLDER,
        f"email_{i:04}.txt"
    )

    with open(filename, "w", encoding="utf-8") as f:
        f.write(email_text)

print(f"Created 500 email files in: {OUTPUT_FOLDER}")