# tasks.py
from celery import shared_task
from osinacore.models import *
from django.db.models import Q

@shared_task
def set_offline():
    print('Hello')
    all_staff_profiles = StaffProfile.objects.all()

    staff_without_working_points = all_staff_profiles.exclude(
        Q(task__point__status='Working On')
    )

    users_without_working_points = [staff_profile.user for staff_profile in staff_without_working_points]
    print(users_without_working_points)

    # Create a new connection record for each user with status set to 'Offline'
    for user in users_without_working_points:
        last_connection = Connection.objects.filter(user=user).order_by('-date').first()
        if last_connection.status != 'Offline':
            Connection.objects.create(
                status='Offline',
                date=timezone.now(),
                user=user
            )
            new_status = Status.objects.create(
                    text='I am now offline!',
                    date=datetime.now().date(),  # Getting the current date
                    time=datetime.now().strftime('%I:%M %p'), 
                    staff=user.staffprofile
                )