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

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


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


class BackUserSerializer(UserSerializer):
    last_country = _SiteSettingsSerializer(read_only=True)
    roles = RoleBaseSerializer(many=True)

    class Meta(UserSerializer.Meta):
        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',
            'phone',
            'phone_as_international',
        )
        extra_kwargs = {
            'password': {'write_only': True},
            'phone': {'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(BackUserSerializer.Meta):
        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',
            'phone',
            'phone_as_international',
        )
        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')

        if 'roles' in validated_data:
            roles_ids = [role['id'] for role in validated_data.pop('roles') if 'id' in role]
            instance.set_roles(roles_ids)

        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 UserCSVSerializer(serializers.ModelSerializer):
    created_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True, source='date_joined')
    last_seen_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True, source='last_login')
    country = serializers.CharField(allow_blank=True, allow_null=True, source='country_name')

    class Meta:
        model = models.User
        fields = [
            'id',
            'username',
            'email',
            'locale',
            'country',
            # 'last_country',
            'created_at',
            'last_seen_at',
            'last_ip',
            'roles',
        ]
