"""Product app documents."""
from django.conf import settings
from django_elasticsearch_dsl import Document, Index, fields
from search_indexes.utils import OBJECT_FIELD_PROPERTIES
from product import models

ProductIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__, 'product'))
ProductIndex.settings(number_of_shards=5, number_of_replicas=2, mapping={'total_fields': {'limit': 3000}})


@ProductIndex.doc_type
class ProductDocument(Document):
    """Product document."""

    description = fields.ObjectField(attr='description_indexing',
                                     properties=OBJECT_FIELD_PROPERTIES)
    product_type = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'index_name': fields.KeywordField(),
            'default_image': fields.KeywordField(attr='default_image_url'),
            'preview_image_url': fields.KeywordField(),
        },
    )
    subtypes = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'index_name': fields.KeywordField(),
            'default_image': fields.KeywordField(attr='default_image_url'),
            'preview_image_url': fields.KeywordField(),
        },
        multi=True
    )
    preview_image_url = fields.KeywordField(attr='preview_image_url')
    establishment = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.KeywordField(),
            'index_name': fields.KeywordField(),
            'slug': fields.KeywordField(),
            'city': fields.ObjectField(
                attr='address.city',
                properties={
                    'id': fields.IntegerField(),
                    'name': fields.KeywordField(),
                    'code': fields.KeywordField(),
                    'country': fields.ObjectField(
                        properties={
                            'id': fields.IntegerField(),
                            'name': fields.ObjectField(attr='name_indexing',
                                                       properties=OBJECT_FIELD_PROPERTIES),
                            'code': fields.KeywordField(),
                            'svg_image': fields.KeywordField(attr='svg_image_indexing')
                        }
                    ),
                }
            ),
            'address': fields.ObjectField(
                properties={
                    'id': fields.IntegerField(),
                    'street_name_1': fields.TextField(
                        fields={'raw': fields.KeywordField()}
                    ),
                    'postal_code': fields.KeywordField(),
                    'coordinates': fields.GeoPointField(attr='location_field_indexing'),
                }
            )
        }
    )
    wine_colors = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True,
    )
    grape_variety = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True,
    )
    wine_origins = fields.ListField(
        fields.ObjectField(
            properties={
                'wine_region': fields.ObjectField(properties={
                    'id': fields.IntegerField(),
                    'name': fields.KeywordField(),
                    'country': fields.ObjectField(properties={
                        'id': fields.IntegerField(),
                        'name': fields.ObjectField(attr='name_indexing',
                                                   properties=OBJECT_FIELD_PROPERTIES),
                        'code': fields.KeywordField(),
                    }),
                    # 'coordinates': fields.GeoPointField(),
                    'description': fields.ObjectField(attr='description_indexing',
                                                      properties=OBJECT_FIELD_PROPERTIES)

                }),
                'wine_sub_region': fields.ObjectField(properties={
                    'id': fields.IntegerField(),
                    'name': fields.KeywordField(),
                })})
    )
    classifications = fields.ObjectField(  # TODO
        properties={
            'classification_type': fields.ObjectField(properties={}),
            'standard': fields.ObjectField(properties={
                'name': fields.KeywordField(),
                'standard_type': fields.IntegerField(),
                # 'coordinates': fields.GeoPointField(),
            }),
            'tags': fields.ObjectField(
                properties={
                    'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
                    'value': fields.KeywordField(),
                },
                multi=True
            ),
        },
        multi=True
    )
    standards = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.KeywordField(),
            'standard_type': fields.IntegerField(),
            # 'coordinates': fields.GeoPointField(),
        },
        multi=True
    )
    wine_village = fields.ObjectField(properties={
        'name': fields.KeywordField(),
    })
    tags = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True
    )
    related_tags = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True
    )
    name = fields.TextField(attr='display_name', analyzer='english')
    name_ru = fields.TextField(attr='display_name', analyzer='russian')
    name_fr = fields.TextField(attr='display_name', analyzer='french')
    favorites_for_users = fields.ListField(field=fields.IntegerField())
    created = fields.DateField(attr='created')  # publishing date (?)

    class Django:
        model = models.Product
        fields = (
            'id',
            'category',
            'available',
            'public_mark',
            'slug',
            'old_id',
            'state',
            'old_unique_key',
            'vintage',
            'average_price',
        )
        related_models = [models.ProductType]

    def get_queryset(self):
        return super().get_queryset().published().with_base_related()
