from django.db import models

from konova.models import UuidModel, DeletableObjectMixin
from konova.utils.mailer import Mailer
from user.enums import UserNotificationEnum
from user.models import UserActionLogEntry


class Team(UuidModel, DeletableObjectMixin):
    """ Groups users in self managed teams. Can be used for multi-sharing of data

    """
    name = models.CharField(max_length=500, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    users = models.ManyToManyField("user.User", blank=True, related_name="teams")
    admins = models.ManyToManyField("user.User", blank=True, related_name="+")

    def __str__(self):
        return self.name

    def mark_as_deleted(self, user):
        """ Creates an UserAction entry and stores it in the correct field

        Args:
            user (User): The performing user

        Returns:

        """
        delete_action = UserActionLogEntry.get_deleted_action(user, "Team deleted")
        self.deleted = delete_action
        self.save()

    def send_mail_shared_access_given_team(self, obj, municipals_names):
        """ Sends a mail to the team members in case of given shared access

        Args:
            obj (str): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_GAINED.value]
        )
        mailer.send_mail_shared_access_given_team(obj, self, users_to_notify, municipals_names)

    def send_mail_shared_access_removed(self, obj, municipals_names):
        """ Sends a mail to the team members in case of removed shared access

        Args:
            obj (): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_REMOVED.value]
        )
        mailer.send_mail_shared_access_removed_team(obj, self, users_to_notify, municipals_names)

    def send_mail_shared_data_unrecorded(self, obj, municipals_names):
        """ Sends a mail to the team members in case of unrecorded data

        Args:
            obj (): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value]
        )
        mailer.send_mail_shared_data_unrecorded_team(obj, self, users_to_notify, municipals_names)

    def send_mail_shared_data_recorded(self, obj, municipals_names):
        """ Sends a mail to the team members in case of unrecorded data

        Args:
            obj (): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value]
        )
        mailer.send_mail_shared_data_recorded_team(obj, self, users_to_notify, municipals_names)

    def send_mail_shared_data_checked(self, obj, municipals_names):
        """ Sends a mail to the team members in case of checked data

        Args:
            obj (): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_CHECKED.value]
        )
        mailer.send_mail_shared_data_checked_team(obj, self, users_to_notify, municipals_names)

    def send_mail_deduction_changed(self, obj, data_changes):
        """ Sends a mail to the team members in case of changed deduction values

        Args:
            obj (): The main object
            data_changes (dict): Contains the old|new changes of the deduction changes

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES.value]
        )
        mailer.send_mail_deduction_changed_team(obj, self, data_changes, users_to_notify)

    def send_mail_shared_data_deleted(self, obj, municipals_names):
        """ Sends a mail to the team members in case of deleted data

        Args:
            obj (): The entry
            municipals_names (iterable): List of municipals for this entry

        Returns:

        """
        mailer = Mailer()
        users_to_notify = self.users.filter(
            notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_DELETED.value]
        )
        mailer.send_mail_shared_data_deleted_team(obj, self, users_to_notify, municipals_names)

    def remove_user(self, user):
        """ Removes a user from the team

        Args:
            user (User): The user to be removed

        Returns:

        """
        self.users.remove(user)
        self.admins.remove(user)
        self.save()

    def is_user_admin(self, user) -> bool:
        """ Returns whether a given user is an admin of the team

        Args:
            user (User): The user

        Returns:
            user_is_admin (bool): Whether the user is an admin or not
        """
        user_is_admin = self.admins.filter(
            id=user.id
        ).exists()
        return user_is_admin