"""This module contains the Redis models for the ``accounts`` app."""

from limpyd import model

from mtp.apps.common.limpyd_base import DjangoBackedRedisModel

from .models import MTPUser as DjangoMTPUser


class MTPUser(DjangoBackedRedisModel):
    """Redis model for the ``MTPUser`` django model.

    Attributes
    ----------
    potential_duplicates_ids: HashField
        A list of key/value pair of the potential duplicates for the current user.
        The key is the id, the hash is the reason (on of the POTENTIAL_DUPLICATES_REASONS).

    """

    django_model = DjangoMTPUser

    potential_duplicates_ids = model.HashField()

    def flag_user_as_potential_duplicate(self, user, reason):
        """Flag the given user as a potential duplicate of the current user

        Parameters
        ----------
        user: MTPUser
            The user we want to flag a potential duplicate
        reason: One of the constants in ``mtp.apps.accounts.constants.PotentialDuplicateReasons``
            The reason we want to flag the user as a potential duplicate

        Raises
        ------
        ValueError
            If the user is already flagged as a potential duplicate of the current user

        """

        if self.potential_duplicates_ids.hexists(user.id):
            raise ValueError(
                'The user with id "%s" is already flagged as a potential duplicate' % user.id
            )

        self.potential_duplicates_ids.hset(user.id, reason)

    def remove_user_as_potential_duplicate(self, user):
        """Remove the given user as a potential duplicate of the current user

        Parameters
        ----------
        user: MTPUser
            The user we want to remove as a potential duplicate

        """

        self.potential_duplicates_ids.hdel(user.id)
