"""Back account serializers"""
from rest_framework import serializers
from account import models
from account.models import User


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = [
            'role',
            'country'
        ]


class BackUserSerializer(serializers.ModelSerializer):
    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',
            'roles',
            'password',
            'city',
            'locale',
        )
        extra_kwargs = {
            'password': {'write_only': True},
        }
        read_only_fields = ('old_password', 'last_login', 'date_joined', 'city', 'locale')

    def create(self, validated_data):
        user = super().create(validated_data)
        user.set_password(validated_data['password'])
        user.save()
        return user


class BackDetailUserSerializer(BackUserSerializer):
    class Meta:
        model = User
        exclude = ('password',)
        read_only_fields = ('old_password', 'last_login', 'date_joined')

    def create(self, validated_data):
        user = super().create(validated_data)
        user.set_password(validated_data['password'])
        user.save()
        return user


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