from django.core.mail import send_mail
from django.urls import reverse
from .models import EmailConfirmation
import uuid
from django.conf import settings
from django.utils.timezone import now


def send_confirmation_email(email):
    confirmation, created = EmailConfirmation.objects.get_or_create(email=email)

    if not created:
        confirmation.uuid = uuid.uuid4()
        confirmation.created_at = now()
        confirmation.save()

    confirmation_link = f"http://127.0.0.1:8000{reverse('confirm_email', args=[confirmation.uuid])}"

    send_mail(
        subject="Confirm your e-mail address",
        message=f"Please click on the link to confirm your e-mail address: {confirmation_link}",
        from_email= settings.EMAIL_HOST_USER,
        recipient_list=[email]
    )