"""Serializers for app advertisements"""
from rest_framework import serializers

from advertisement import models
from main.models import SiteSettings
from main.serializers import PageTypeBaseSerializer
from translation.models import Language


class AdvertisementBaseSerializer(serializers.ModelSerializer):
    """Base serializer for model Advertisement."""
    page_type_detail = PageTypeBaseSerializer(read_only=True,
                                              source='page_type')
    target_languages = serializers.PrimaryKeyRelatedField(
        queryset=Language.objects.all(),
        many=True,
        write_only=True
    )
    target_sites = serializers.PrimaryKeyRelatedField(
        queryset=SiteSettings.objects.all(),
        many=True,
        write_only=True,
        source='sites'
    )

    class Meta:
        model = models.Advertisement
        fields = [
            'id',
            'uuid',
            'url',
            'block_level',
            'target_languages',
            'target_sites',
            'start',
            'end',
            'page_type',
            'page_type_detail',
        ]
        extra_kwargs = {
            'page_type': {'required': True, 'write_only': True}
        }


class AdvertisementSerializer(AdvertisementBaseSerializer):
    """Serializer for model Advertisement."""
    class Meta(AdvertisementBaseSerializer.Meta):
        """Meta class."""
        fields = AdvertisementBaseSerializer.Meta.fields.copy()
        fields.pop(fields.index('page_type_detail'))
