# coding=utf-8
import datetime
from rest_framework import serializers
from django.conf import settings
from apps.agent.models import Agent,Store
from utils.booleancharfield import BooleanCharField

class AgentSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)

    class Meta:
        model = Agent
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(AgentSerializer, self).create(validated_data)
        return instance


class AgentComboboxSerializer(serializers.ModelSerializer):
    class Meta:
        model = Agent
        fields = ('id', 'name',)

class StoreComboboxSerializer(serializers.ModelSerializer):
    class Meta:
        model = Store
        fields = ('id', 'name',)

class StoreSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.name', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)
    check_user_text = serializers.CharField(source='check_user.name', read_only=True)
    agent_text = serializers.CharField(source='agent.name', read_only=True)
    enable_text = BooleanCharField(source='enable', read_only=True)

    class Meta:
        model = Store
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        validated_data['agent'] = self.context['request'].user.agent
        validated_data['end_date'] = (datetime.datetime.now() + datetime.timedelta(days=365)).strftime('%Y-%m-%d')
        instance = super(StoreSerializer, self).create(validated_data)
        return instance
