from authentication.models import User,Role
from django.contrib.auth.decorators import user_passes_test

def is_admin(user):
    if user.is_authenticated:
        return Role.ADMIN in user.role.values_list('id', flat=True)
    return False

def is_sales(user):
    if user.is_authenticated:
        return Role.SALES in user.role.values_list('id', flat=True)
    return False

def is_technician(user):
    if user.is_authenticated:
        return Role.TECHNICIAN in user.role.values_list('id', flat=True)
    return False

def is_customer(user):
    # return Role.CUSTOMER in user.role.values_list('id', flat=True)
    if user.is_authenticated:
        return Role.CUSTOMER in user.role.values_list('id', flat=True)
    return False
    

def is_superuser(user):
    if user.is_authenticated:
        return user.is_superuser
    return False

def is_supplier(user):
    if user.is_authenticated:
        return Role.SUPPLIER in user.role.values_list('id', flat=True)
    return False


def admin_or_sales_required(view_func):
    def test_func(user):
        return Role.ADMIN in user.role.values_list('id', flat=True) or Role.SALES in user.role.values_list('id', flat=True)

    return user_passes_test(test_func)(view_func)