from thewind.models import Account
from thewind.models import Article
from thewind.models import ArticleDetail
from thewind.models import Book
from thewind.models import Link
from thewind.models import Motto
from thewind.models import MottoDetail
from thewind.models import Tag
from thewind.models import Category
from rest_framework import serializers


class AccountSerializer(serializers.ModelSerializer):

    class Meta:
        model = Account
        fields = '__all__'


class CategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = Category
        fields = ('category_id', 'name', 'path')


class TagSerializer(serializers.ModelSerializer):

    class Meta:
        model = Tag
        fields = ('tag_id', 'name', 'path')


class ArticleSerializer(serializers.ModelSerializer):

    tags = serializers.SerializerMethodField()
    categories = serializers.SerializerMethodField()

    def get_tags(self, instance):
        items = instance.tags.get_queryset()
        tags = [{'id': item.tag_id, 'name': item.name} for item in items]
        return tags

    def get_categories(self, instance):
        items = instance.categories.get_queryset()
        categories = [{'id': item.category_id, 'name': item.name} for item in items]
        return categories

    class Meta:
        model = Article
        fields = ('article_id', 'title', 'abstract', 'tags', 'categories', 'create_date', 'update_date', 'like_count')


class ArticleBriefSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = ('article_id', 'title', 'create_date', 'update_date')


class ArticleDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = ArticleDetail
        fields = '__all__'


class MottoDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = MottoDetail
        fields = '__all__'



class MottoSerializer(serializers.ModelSerializer):

    class Meta:
        model = Motto
        fields = '__all__'


class BookSerializer(serializers.ModelSerializer):

    class Meta:
        model = Book
        fields = '__all__'


class LinkSerializer(serializers.ModelSerializer):

    class Meta:
        model = Link
        fields = '__all__'