"""Send the SANEF Qualys V3 xlsx to khalid.moutaouakil-ext@sanef.com via Gmail SMTP.

App Password lu depuis variable d'env GMAIL_APP_PASSWORD (jamais en dur dans le script).
"""
import os
import smtplib
import ssl
from email.message import EmailMessage
from pathlib import Path

SENDER = "kalid.moutaouakil@gmail.com"
RECIPIENT = "khalid.moutaouakil-ext@sanef.com"
ATTACHMENT = Path(r"C:\Claude\sanef\QL\docs\SANEF_Qualys_Tags_V3_RuleTypes_v2.xlsx")

password = os.environ.get("GMAIL_APP_PASSWORD")
if not password:
    raise SystemExit("GMAIL_APP_PASSWORD non defini dans l'environnement")

password = password.replace(" ", "")  # Gmail App Password sans espaces

msg = EmailMessage()
msg["From"] = SENDER
msg["To"] = RECIPIENT
msg["Subject"] = "SANEF Qualys - Tags V3 (taxonomie complete + descriptions)"
msg.set_content(
    "Bonjour Khalid,\n\n"
    "En piece jointe le tableau de reference de la taxonomie Qualys V3 :\n"
    "  - Feuille 1 : 23 tags dynamiques (OS, EQT, ENV, POS, NOM-LEGACY, TAG-EMV, TAG-OBS) avec couleurs, descriptions et requetes QQL\n"
    "  - Feuille 2 : 7 tags statiques (TAG-SED, TAG-SEI, TAG-ELS, TAG-DEC, TAG-INT, TAG-SIC, TAG-SIA)\n"
    "  - Feuille 3 : prefixes a creer manuellement (APP, BDD, VRF, MID, VULN)\n\n"
    "Mises a jour de cette version :\n"
    "  - Renommage TYP -> EQT (Equipement)\n"
    "  - Descriptions OS-* et EQT-* harmonisees\n"
    "  - ENV-TST corrige : exclusion des postes Superviseur Peage (svp*)\n"
    "  - EQT-SRV idem (and not asset.name:svp*)\n\n"
    "Cordialement,\n"
    "SECOPS\n"
)

if not ATTACHMENT.exists():
    raise SystemExit(f"Fichier introuvable : {ATTACHMENT}")

with ATTACHMENT.open("rb") as f:
    msg.add_attachment(
        f.read(),
        maintype="application",
        subtype="vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        filename=ATTACHMENT.name,
    )

ctx = ssl.create_default_context()
with smtplib.SMTP("smtp.gmail.com", 587, timeout=30) as s:
    s.ehlo()
    s.starttls(context=ctx)
    s.ehlo()
    s.login(SENDER, password)
    s.send_message(msg)

print(f"Mail envoye a {RECIPIENT} avec piece jointe {ATTACHMENT.name} ({ATTACHMENT.stat().st_size} bytes)")
