from django.contrib.contenttypes.models import ContentType

from account.models import CustomUser
from main.models import Like


def add_like(obj, user):
    obj_type = ContentType.objects.get_for_model(obj)
    like, is_created = Like.objects.get_or_create(content_type=obj_type, object_id=obj.id, user=user)
    return like


def remove_like(obj, user):
    obj_type = ContentType.objects.get_for_model(obj)
    Like.objects.filter(content_type=obj_type, object_id=obj.id, user=user).delete()


def is_user(obj, user):
    obj_type = ContentType.objects.get_for_model(obj)
    likes = Like.objects.filter(content_type=obj_type, object_id=obj.id, user=user)
    return likes.exists()


def get_users(obj):
    obj_type = ContentType.objects.get_for_model(obj)
    return CustomUser.objects.filter(likes__content_type=obj_type, likes__object_id=obj.id)