from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser


class UserManager(BaseUserManager):
    def create_or_update_by_email(self, user_dict: dict) -> tuple[AbstractUser, bool]:
        # create or sync user with OpenID Data
        user, created = self.model.objects.get_or_create(
            sso_id=user_dict["oid"],
            defaults={
                "email": user_dict["email"],
                "username": user_dict["email"],
                "first_name": user_dict["first_name"],
                "last_name": user_dict["last_name"],
            },
        )

        if not created:
            user.email = user_dict["email"]
            user.username = user_dict["email"]
            user.first_name = user_dict["first_name"]
            user.last_name = user_dict["last_name"]
            user.save()

        return user, created
