"""Back account serializers"""
from rest_framework import serializers

from account import models
from account.models import User
from account.serializers import RoleBaseSerializer, subscriptions_handler
from main.models import SiteSettings


class _SiteSettingsSerializer(serializers.ModelSerializer):
    class Meta:
        model = SiteSettings
        fields = (
            'id',
            'subdomain',
            'country_code',
        )


class BackUserSerializer(serializers.ModelSerializer):
    last_country = _SiteSettingsSerializer(read_only=True)
    roles = RoleBaseSerializer(many=True, read_only=True)
    subscriptions = serializers.ListField(
        source='subscription_types',
        allow_null=True,
        allow_empty=True,
        child=serializers.IntegerField(min_value=1),
        required=False,
        help_text='list of subscription_types id',
    )

    class Meta:
        model = User
        fields = (
            'id',
            'last_login',
            'is_superuser',
            'username',
            'last_name',
            'first_name',
            'is_active',
            'date_joined',
            'image_url',
            'cropped_image_url',
            'email',
            'email_confirmed',
            'unconfirmed_email',
            'email_confirmed',
            'newsletter',
            'password',
            'city',
            'locale',
            'last_ip',
            'last_country',
            'roles',
            'subscriptions',
        )
        extra_kwargs = {
            'password': {'write_only': True},
        }
        read_only_fields = (
            'old_password',
            'last_login',
            'date_joined',
            'city',
            'locale',
            'last_ip',
            'last_country',
        )

    def create(self, validated_data):
        subscriptions_list = []
        if 'subscription_types' in validated_data:
            subscriptions_list = validated_data.pop('subscription_types')

        user = super().create(validated_data)
        user.set_password(validated_data['password'])
        user.save()

        subscriptions_handler(subscriptions_list, user)
        return user


class BackDetailUserSerializer(BackUserSerializer):
    class Meta:
        model = User
        fields = (
            'id',
            'last_country',
            'roles',
            'last_login',
            'is_superuser',
            'first_name',
            'last_name',
            'is_staff',
            'is_active',
            'date_joined',
            'username',
            'image_url',
            'cropped_image_url',
            'email',
            'unconfirmed_email',
            'email_confirmed',
            'newsletter',
            'old_id',
            'locale',
            'city',
            'last_ip',
            'groups',
            'user_permissions',
            'subscriptions',
        )
        read_only_fields = (
            'old_password',
            'last_login',
            'date_joined',
            'last_ip',
            'last_country',
        )

    def create(self, validated_data):
        subscriptions_list = []
        if 'subscription_types' in validated_data:
            subscriptions_list = validated_data.pop('subscription_types')

        user = super().create(validated_data)
        user.set_password(validated_data['password'])
        user.save()

        subscriptions_handler(subscriptions_list, user)
        return user

    def update(self, instance, validated_data):
        subscriptions_list = []
        if 'subscription_types' in validated_data:
            subscriptions_list = validated_data.pop('subscription_types')

        instance = super().update(instance, validated_data)
        subscriptions_handler(subscriptions_list, instance)
        return instance


class UserRoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserRole
        fields = [
            'role',
            'user',
            'establishment'
        ]


class RoleTabRetrieveSerializer(serializers.Serializer):
    """Serializer for BackOffice role tab."""
    role_name = serializers.CharField()
    role_counter = serializers.IntegerField()
