from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework_simplejwt.exceptions import AuthenticationFailed
from rest_framework.permissions import BasePermission


class SuperAdminJWTAuthentication(JWTAuthentication):
    """
    JWT Authentication for HRNexus Platform SuperAdmin.

    Required JWT:

        type = "superadmin"
        user_id = superadmin ID
    """

    def get_user(self, validated_token):

        # =====================================================
        # TOKEN TYPE CHECK
        # =====================================================

        token_type = validated_token.get("type")

        if token_type != "superadmin":

            raise AuthenticationFailed({
                "status": "error",
                "code": "invalid_token_type",
                "message": "Invalid SuperAdmin token.",
                "data": None,
            })

        # =====================================================
        # SUPERADMIN ID
        # =====================================================

        superadmin_id = validated_token.get("user_id")

        if not superadmin_id:

            raise AuthenticationFailed({
                "status": "error",
                "code": "superadmin_id_missing",
                "message": "SuperAdmin ID missing from token.",
                "data": None,
            })

        # =====================================================
        # IMPORT MODEL
        # (additive fix: the real model is SuperAdminProfile,
        # not "SuperAdmin")
        # =====================================================

        from .models import SuperAdminProfile

        # =====================================================
        # GET SUPERADMIN
        # =====================================================

        try:

            superadmin = SuperAdminProfile.objects.select_related(
                "user"
            ).get(
                id=superadmin_id
            )

        except SuperAdminProfile.DoesNotExist:

            raise AuthenticationFailed({
                "status": "error",
                "code": "superadmin_not_found",
                "message": "SuperAdmin not found.",
                "data": None,
            })

        # =====================================================
        # ACTIVE CHECK
        # =====================================================

        if hasattr(superadmin, "is_active"):

            if not superadmin.is_active:

                raise AuthenticationFailed({
                    "status": "error",
                    "code": "superadmin_disabled",
                    "message": "SuperAdmin account is inactive.",
                    "data": None,
                })

        # =====================================================
        # RETURN SUPERADMIN
        # =====================================================

        return superadmin


class IsSuperAdminAuthenticated(BasePermission):
    """
    Allows only platform SuperAdmin.
    """

    def has_permission(self, request, view):

        from .models import SuperAdminProfile

        return isinstance(
            request.user,
            SuperAdminProfile
        )