from django.contrib.auth.backends import BaseBackend
from apps.users.models import User

class EmailBackend(BaseBackend):
    def authenticate(self, request, email=None, password=None, **kwargs):
        """Authenticate user using email and password"""
        if email is None or password is None:
            return None
        
        try:
            user = User.objects.get(email__iexact=email)
        except User.DoesNotExist:
            return None 
        
        # Check password and if user is active and email verified
        if user.check_password(password) and user.is_active and user.is_email_verified:
            return user
            
        return None 

    def get_user(self, user_id):
        """Get user by ID"""
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None