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


EstablishmentIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__,
                                                                  'establishment'))
EstablishmentIndex.settings(number_of_shards=5, number_of_replicas=2)


@EstablishmentIndex.doc_type
class EstablishmentDocument(Document):
    """Establishment document."""

    preview_image = fields.KeywordField(attr='preview_image_url')
    description = fields.ObjectField(attr='description_indexing',
                                     properties=OBJECT_FIELD_PROPERTIES)
    establishment_type = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.ObjectField(attr='name_indexing',
                                       properties=OBJECT_FIELD_PROPERTIES),
            'index_name': fields.KeywordField(attr='index_name'),
            'default_image': fields.KeywordField(attr='default_image_url'),
            'preview_image_url': fields.KeywordField(),
        })
    establishment_subtypes = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'name': fields.ObjectField(attr='name_indexing'),
            'index_name': fields.KeywordField(attr='index_name'),
            'default_image': fields.KeywordField(attr='default_image_url'),
            'preview_image_url': fields.KeywordField(),
        },
        multi=True)
    works_evening = fields.ListField(fields.IntegerField(
        attr='works_evening'
    ))
    works_noon = fields.ListField(fields.IntegerField(
        attr='works_noon'
    ))
    works_at_weekday = fields.ListField(fields.IntegerField(
        attr='works_at_weekday'
    ))
    works_now = fields.BooleanField(attr='works_now')
    tags = fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'label': fields.ObjectField(attr='label_indexing',
                                        properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True)
    restaurant_category = fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'label': fields.ObjectField(attr='label_indexing',
                                        properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True, attr='restaurant_category_indexing')
    restaurant_cuisine = fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'label': fields.ObjectField(attr='label_indexing',
                                        properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True, attr='restaurant_cuisine_indexing')
    artisan_category = fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'label': fields.ObjectField(attr='label_indexing',
                                        properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True, attr='artisan_category_indexing')
    visible_tags = fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'label': fields.ObjectField(attr='label_indexing',
                                        properties=OBJECT_FIELD_PROPERTIES),
            'value': fields.KeywordField(),
        },
        multi=True)
    products = fields.ObjectField(
        properties={
            '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(),
                        }),
                    })),
            'wine_colors': fields.ObjectField(
                properties={
                    'id': fields.IntegerField(),
                    'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES),
                    'value': fields.KeywordField(),
                },
                multi=True,)},
        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(),
                })})
    )
    schedule = fields.ListField(fields.ObjectField(
        properties={
            'id': fields.IntegerField(attr='id'),
            'weekday': fields.IntegerField(attr='weekday'),
            'weekday_display': fields.KeywordField(attr='get_weekday_display'),
            'closed_at': fields.KeywordField(attr='closed_at_str'),
            'opening_at': fields.KeywordField(attr='opening_at_str'),
            'closed_at_indexing': fields.DateField(),
            'opening_at_indexing': fields.DateField(),
        }
    ))
    address = fields.ObjectField(
        properties={
            'id': fields.IntegerField(),
            'street_name_1': fields.TextField(
                fields={'raw': fields.KeywordField()}
            ),
            'street_name_2': fields.TextField(
                fields={'raw': fields.KeywordField()}
            ),
            'number': fields.IntegerField(),
            'postal_code': fields.KeywordField(),
            'coordinates': fields.GeoPointField(attr='location_field_indexing'),
            'city': fields.ObjectField(
                properties={
                    'id': fields.IntegerField(),
                    'name': fields.KeywordField(),
                    'is_island': fields.BooleanField(),
                    'country': fields.ObjectField(
                        properties={
                            'id': fields.IntegerField(),
                            'name': fields.ObjectField(attr='name_indexing',
                                                       properties=OBJECT_FIELD_PROPERTIES),
                            'code': fields.KeywordField(),
                        }
                    ),
                }
            ),
        },
    )
    favorites_for_users = fields.ListField(field=fields.IntegerField())
    tz = fields.KeywordField(attr='timezone_as_str')

    class Django:

        model = models.Establishment
        fields = (
            'id',
            'name',
            'transliterated_name',
            'index_name',
            'is_publish',
            'price_level',
            'toque_number',
            'public_mark',
            'slug',
        )

    def get_queryset(self):
        return super().get_queryset().with_es_related()
