#!/usr/bin/env python3
"""Send email from Sho'Nuff Brown with rotating title + signature + BCC to Germaine."""
import smtplib, random, importlib.util
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

SHONUFF_PASS = "Catches.bullets1985"

def send(to, subject, body, cc=None):
    # Load random closing
    spec = importlib.util.spec_from_file_location("closings", "/root/.hermes/references/shonuff-closings.py")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    closing = random.choice(mod.SHONUFF_CLOSINGS)

    # Load random title
    spec2 = importlib.util.spec_from_file_location("titles", "/root/.hermes/references/shonuff-titles.py")
    mod2 = importlib.util.module_from_spec(spec2)
    spec2.loader.exec_module(mod2)
    title = random.choice(mod2.SHONUFF_TITLES)

    # Load image and signature
    b64 = open("/root/.hermes/references/shonuff-image-b64.txt").read().strip()
    sig = open("/root/.hermes/references/shonuff-email-signature.html").read()
    sig = sig.replace("%SHONUFF_IMAGE%", b64).replace("%SHONUFF_TITLE%", title)

    msg = MIMEMultipart("alternative")
    msg["From"] = "shonuff@germainebrown.com"
    msg["To"] = to
    msg["Bcc"] = "g@germainebrown.com"
    msg["Subject"] = subject

    text = MIMEText(f"{body}\n\n{closing}\n\n-- \nSho'Nuff Brown\n{title}\nIT Pro Partner\nshonuff@germainebrown.com", "plain")
    html = MIMEText(f"<p>{body.replace(chr(10), '<br>')}</p><p><em>{closing}</em></p>{sig}", "html")
    msg.attach(text)
    msg.attach(html)

    with smtplib.SMTP("mail.germainebrown.com", 2525) as s:
        s.starttls()
        s.login("shonuff@germainebrown.com", SHONUFF_PASS)
        s.send_message(msg)

    print(f"Sent to {to} | closing: {closing} | title: {title}")

if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 4:
        send(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        print("Usage: send-shonuff.py <to> <subject> <body>")
