from rest_framework import permissions

class IsSuperUser(permissions.BasePermission):
    """
    Permission to only allow superusers to access the view.
    """
    def has_permission(self, request, view):
        return request.user.is_authenticated and request.user.is_superuser

class IsDeltaAdmin(permissions.BasePermission):
    """
    Permission to only allow Delta administrators to access the view.
    """
    def has_permission(self, request, view):
        return request.user.is_authenticated and request.user.role == request.user.DELTA_ADMIN

class IsPartnerAdmin(permissions.BasePermission):
    """
    Permission to only allow Partner administrators to access the view.
    """
    def has_permission(self, request, view):
        return request.user.is_authenticated and request.user.role == request.user.PARTNER_ADMIN
    
    def has_object_permission(self, request, view, obj):
        # Check if the object is related to the partner that this admin manages
        if hasattr(obj, 'partner'):
            return obj.partner == request.user.partner_profile.partner
        elif hasattr(obj, 'event') and hasattr(obj.event, 'partner'):
            return obj.event.partner == request.user.partner_profile.partner
        return False

class IsVisitor(permissions.BasePermission):
    """
    Permission to only allow visitors to access the view.
    """
    def has_permission(self, request, view):
        return request.user.is_authenticated and request.user.role == request.user.VISITOR
    
    def has_object_permission(self, request, view, obj):
        # Visitors can only access their own reservations
        if hasattr(obj, 'user'):
            return obj.user == request.user
        return False