import smtplib
import random
import string
from pathlib import Path

from PIL import Image
from pydantic import EmailStr

from app.config import settings
from app.tasks.celery import celery
from app.tasks.email_templates import create_registration_confirmation_template


def generate_confirmation_code(length=6):
    characters = string.ascii_letters + string.digits
    confirmation_code = ''.join(random.choice(characters) for _ in range(length))
    return confirmation_code


@celery.task
def send_registration_confirmation_email(
        username: str,
        email_to: EmailStr,
):
    confirmation_code = generate_confirmation_code()

    msg_content = create_registration_confirmation_template(
        username=username, email_to=email_to, confirmation_code=confirmation_code
    )

    with smtplib.SMTP_SSL(settings.SMTP_HOST, settings.SMTP_PORT) as server:
        server.login(settings.SMTP_USER, settings.SMTP_PASS)
        server.send_message(msg_content)

