"""Permissions for the supplier portal."""

from rest_framework.permissions import BasePermission


class IsAdminRole(BasePermission):
    """Allows access only to users with role='admin'."""

    message = "Admin access required."

    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.is_authenticated and user.role == "admin")


class IsSupplierRole(BasePermission):
    """Allows access only to users with role='supplier'."""

    message = "Supplier access required."

    def has_permission(self, request, view):
        user = request.user
        return bool(user and user.is_authenticated and user.role == "supplier")


class IsLinkedSupplier(BasePermission):
    """Allows access only to authenticated suppliers with a SupplierProfile."""

    message = "Your account is not linked to a supplier."

    def has_permission(self, request, view):
        user = request.user
        return bool(
            user
            and user.is_authenticated
            and user.role == "supplier"
            and hasattr(user, "supplier_profile")
        )
