from datetime import date
from django.conf import settings
from apps.companies.models import Company
from utils.threads.email_thread import send_mail


def send_new_user_email(user, company_ids, generated_password, subject = None):
    """ Sends registration email when a new user is created """

    companies = Company.objects.filter(id__in=company_ids)
    company_names = ", ".join(c.name for c in companies)
    dealership_list = [c.name for c in companies]

    data = {
        'email': user.email,
        'username': user.username,
        'password': generated_password,
        'firstname': user.profile.first_name,
        'lastname': user.profile.last_name,
        'companyName': company_names,
        'dealership_list':dealership_list,
        'button': settings.REACT_DOMAIN,
        'year': date.today().year
    }

    subject = subject
    template = "auth/new_userRegister.html"
    recipient = [user.email]

    send_mail(
        subject=subject,
        html_content=template,
        recipient_list=recipient,
        key=data,
    )
