"""Send Nur Falah App Store DOCX to multiple recipients via email."""
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os, sys

DOCX_PATH = "/Users/wanjauhari24/Desktop/Projects/nur-muslim-companion/Nur-Falah-App-Store-Submission.docx"
TO_LIST = ["wanjauhari@gmail.com", "wanjauhari@me.com"]
FROM = "wanjauhari@gmail.com"
SUBJECT = "Nur Falah — App Store Submission DOCX"
BODY = "Here is the complete App Store Connect submission document for Nur Falah Muslim Companion.\n\nIncludes full description, keywords, screenshot plan, privacy policy, and ASO recommendations."

def build_msg(to_addr):
    msg = MIMEMultipart()
    msg['From'] = FROM
    msg['To'] = to_addr
    msg['Subject'] = SUBJECT
    msg.attach(MIMEText(BODY, 'plain'))
    with open(DOCX_PATH, 'rb') as f:
        part = MIMEBase('application', 'vnd.openxmlformats-officedocument.wordprocessingml.document')
        part.set_payload(f.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(DOCX_PATH))
        msg.attach(part)
    return msg

# Try sendmail first (local MTA) - send one per recipient
def try_sendmail():
    if os.path.exists('/usr/sbin/sendmail'):
        import subprocess
        for to_addr in TO_LIST:
            msg = build_msg(to_addr)
            p = subprocess.run(
                ['/usr/sbin/sendmail', '-t'],
                input=msg.as_bytes(),
                capture_output=True,
                timeout=15
            )
            if p.returncode == 0:
                print(f"✅ Sent via sendmail → {to_addr}")
            else:
                print(f"⚠️ sendmail failed for {to_addr}: {p.stderr.decode()[:200]}")
                return False
        return True
    return False

if __name__ == '__main__':
    if try_sendmail():
        sys.exit(0)
    print("❌ sendmail not available.")
    sys.exit(1)
