#!/usr/bin/env python3
"""Send email notification from agents via SMTP.

Usage:
    python3 scripts/notify.py "subject" "body"
    python3 scripts/notify.py "subject" --file /path/to/body.md
"""

import smtplib
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587
FROM_EMAIL = "notify@marcus-law.co.il"
FROM_PASS = "vuva jwed lbuz xjds"
TO_EMAIL = "chaim+paperclip@marcus-law.co.il"


def send(subject: str, body: str) -> bool:
    msg = MIMEMultipart("alternative")
    msg["Subject"] = subject
    msg["From"] = f"עוזר משפטי <{FROM_EMAIL}>"
    msg["To"] = TO_EMAIL
    msg.attach(MIMEText(body, "plain", "utf-8"))

    try:
        with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
            server.starttls()
            server.login(FROM_EMAIL, FROM_PASS)
            server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
        return True
    except Exception as e:
        print(f"Email failed: {e}", file=sys.stderr)
        return False


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: notify.py 'subject' 'body' OR notify.py 'subject' --file path")
        sys.exit(1)

    subject = sys.argv[1]
    if sys.argv[2] == "--file":
        body = open(sys.argv[3], encoding="utf-8").read()
    else:
        body = sys.argv[2]

    if send(subject, body):
        print(f"Sent to {TO_EMAIL}")
    else:
        sys.exit(1)
