'''
Created on 23 janv. 2026

@author: denis
'''
from celery import shared_task
from django.core.mail import EmailMultiAlternatives
from .process import MqttProcess, mqttc


@shared_task
def async_send_mail(subject, body, from_email, to=[], bcc=[], cc=[], html=None, fail_silently=False, attachments=[],  connection=None):
    msg = EmailMultiAlternatives(subject, body, from_email, to, bcc=bcc, cc=cc, connection=connection)
    if html:
        msg.attach_alternative(html, 'text/html')
    for f in attachments:
        msg.attach_file(f)
    msg.send(fail_silently)


@shared_task(bind=True)
def start_mqtt_worker(self):
    mqttc.task = MqttProcess()
    mqttc.task.start()
    return {"status": "success", "message": f"MQTT Worker démarré."}


@shared_task(bind=True)
def stop_mqtt_worker(self):
    if mqttc.task:
        mqttc.task.stop()
    return {"status": "success", "message": f"MQTT Worker arrêté."}


