from celery import shared_task
from django.utils import timezone
from datetime import timedelta
from django.core.mail import send_mail
from myapp.models import User_Profile, Notification
from django.conf import settings


@shared_task
def send_birthday_notifications_task():
    today = timezone.now().date()
    target_date = today + timedelta(days=8)
        
    upcoming_birthdays = User_Profile.objects.filter(date_of_birth__month=target_date.month, date_of_birth__day=target_date.day)
    emails = []
    message = ""    
    if upcoming_birthdays:
            
        for birthday_person in upcoming_birthdays:
            bdate = birthday_person.date_of_birth.strftime('%d-%m')
            employees = User_Profile.objects.filter(organisation=birthday_person.organisation)
            message = f"{birthday_person.full_name} birthday is on {bdate}, dont't forget to wish"
            for emp in employees:
                emails.append(emp.email)
            subject = 'Birthday reminder'
            message = message
            from_email = settings.EMAIL_HOST_USER
            recipient_list = emails

            send_mail(subject, message, from_email, recipient_list)
            print("mails sent")  

@shared_task()
def send_birthday_wish_mail():
    today = timezone.now().date()
    birthdays = User_Profile.objects.filter(date_of_birth=today)
    
    message = ""    
    if birthdays:
        
        for birthday_person in birthdays:
            message = f"On behalf of the entire company, I wish you a very happy birthday {birthday_person.full_name} and send you my best wishes for much happiness             in your life. The warmest wishes to a great member of our team. May your special day be full of happiness, fun and cheer! The whole team wishes you the             happiest of birthdays and a great year"
                
            subject = f'Birthday wish from {birthday_person.organisation}'
            message = message
            from_email = settings.EMAIL_HOST_USER
            recipient_list = [birthday_person.email]

            send_mail(subject, message, from_email, recipient_list)
            print("wish mail sent")      