"""Establishment models."""
from datetime import datetime
from functools import reduce
from operator import or_
from typing import List

import elasticsearch_dsl
from django.conf import settings
from django.contrib.contenttypes import fields as generic
from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance as DistanceMeasure
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q, Prefetch
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from timezone_field import TimeZoneField

from location.models import Address
from timetable.models import Timetable
from location.models import WineOriginAddressMixin
from main.models import Award, Currency
from review.models import Review
from tag.models import Tag
from utils.methods import transform_into_readable_str
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
                          TranslatedFieldsMixin, BaseAttributes, GalleryMixin,
                          IntermediateGalleryModelMixin, HasTagsMixin,
                          FavoritesMixin, TypeDefaultImageMixin)


# todo: establishment type&subtypes check
class EstablishmentType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
    """Establishment type model."""

    STR_FIELD_NAME = 'name'

    # EXAMPLE OF INDEX NAME CHOICES
    RESTAURANT = 'restaurant'
    ARTISAN = 'artisan'
    PRODUCER = 'producer'

    name = TJSONField(blank=True, null=True, default=None, verbose_name=_('Description'),
                      help_text='{"en-GB":"some text"}')
    index_name = models.CharField(max_length=50, unique=True, db_index=True,
                                  verbose_name=_('Index name'))
    use_subtypes = models.BooleanField(_('Use subtypes'), default=True)
    tag_categories = models.ManyToManyField('tag.TagCategory',
                                            blank=True,
                                            related_name='establishment_types',
                                            verbose_name=_('Tag categories'))
    default_image = models.ForeignKey('gallery.Image', on_delete=models.SET_NULL,
                                      related_name='establishment_types',
                                      blank=True, null=True, default=None,
                                      verbose_name='default image')

    class Meta:
        """Meta class."""

        verbose_name = _('Establishment type')
        verbose_name_plural = _('Establishment types')

    @property
    def label(self):
        return transform_into_readable_str(self.index_name)


class EstablishmentSubTypeManager(models.Manager):
    """Extended manager for establishment subtype."""

    def make(self, name, establishment_type):
        obj = self.model(name=name, establishment_type=establishment_type)
        obj.full_clean()
        obj.save()
        return obj


class EstablishmentSubType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
    """Establishment type model."""

    # EXAMPLE OF INDEX NAME CHOICES
    WINERY = 'winery'

    name = TJSONField(blank=True, null=True, default=None, verbose_name=_('Description'),
                      help_text='{"en-GB":"some text"}')
    index_name = models.CharField(max_length=50, unique=True, db_index=True,
                                  verbose_name=_('Index name'))
    establishment_type = models.ForeignKey(EstablishmentType,
                                           on_delete=models.CASCADE,
                                           verbose_name=_('Type'))
    tag_categories = models.ManyToManyField('tag.TagCategory',
                                            blank=True,
                                            related_name='establishment_subtypes',
                                            verbose_name=_('Tag categories'))
    default_image = models.ForeignKey('gallery.Image', on_delete=models.SET_NULL,
                                      related_name='establishment_sub_types',
                                      blank=True, null=True, default=None,
                                      verbose_name='default image')

    objects = EstablishmentSubTypeManager()

    class Meta:
        """Meta class."""

        verbose_name = _('Establishment subtype')
        verbose_name_plural = _('Establishment subtypes')

    def clean_fields(self, exclude=None):
        if not self.establishment_type.use_subtypes:
            raise ValidationError(_('Establishment type is not use subtypes.'))


class EstablishmentQuerySet(models.QuerySet):
    """Extended queryset for Establishment model."""

    def with_base_related(self):
        """Return qs with related objects."""
        return self.select_related('address', 'establishment_type'). \
            prefetch_related('tags', 'tags__translation')

    def with_schedule(self):
        """Return qs with related schedule."""
        return self.prefetch_related('schedule')

    def with_currency_related(self):
        """Return qs with related """
        return self.prefetch_related('currency')

    def with_extended_address_related(self):
        """Return qs with deeply related address models."""
        return self.select_related('address__city', 'address__city__region',
                                   'address__city__region__country',
                                   'address__city__country')

    def with_extended_related(self):
        return self.with_extended_address_related().select_related('establishment_type'). \
            prefetch_related('establishment_subtypes', 'awards', 'schedule',
                             'phones', 'gallery', 'menu_set', 'menu_set__plate_set',
                             'menu_set__plate_set__currency', 'currency'). \
            prefetch_actual_employees()

    def with_type_related(self):
        return self.prefetch_related('establishment_subtypes')

    def with_es_related(self):
        """Return qs with related for ES indexing objects."""
        return self.select_related('address', 'establishment_type', 'address__city',
                                   'address__city__country'). \
            prefetch_related('tags', 'schedule')

    def search(self, value, locale=None):
        """Search text in JSON fields."""
        if locale is not None:
            filters = [
                {f'name__icontains': value},
                {f'description__{locale}__icontains': value}
            ]
            return self.filter(reduce(lambda x, y: x | y, [models.Q(**i) for i in filters]))
        else:
            return self.none()

    def es_search(self, value, locale=None):
        """Search text via ElasticSearch."""
        from search_indexes.documents import EstablishmentDocument
        search = EstablishmentDocument.search().filter(
            elasticsearch_dsl.Q('match', name=value) |
            elasticsearch_dsl.Q('match', **{f'description.{locale}': value})
        ).execute()
        ids = [result.meta.id for result in search]
        return self.filter(id__in=ids)

    def by_country(self, country):
        """Return establishments by country code"""
        return self.filter(address__city__country=country)

    def by_country_code(self, code):
        """Return establishments by country code"""
        return self.filter(address__city__country__code=code)

    def published(self):
        """
        Return QuerySet with published establishments.
        """
        return self.filter(is_publish=True)

    def has_published_reviews(self):
        """
        Return QuerySet establishments with published reviews.
        """
        return self.filter(reviews__status=Review.READY, )

    def annotate_distance(self, point: Point = None):
        """
        Return QuerySet with annotated field - distance
        Description:

        """
        return self.annotate(distance=Distance('address__coordinates', point,
                                               srid=settings.GEO_DEFAULT_SRID))

    def annotate_intermediate_public_mark(self):
        """
        Return QuerySet with annotated field - intermediate_public_mark.
        Description:
        If establishments in collection POP and its mark is null, then
        intermediate_mark is set to 10;
        """
        from collection.models import Collection

        return self.annotate(intermediate_public_mark=Case(
            When(
                collections__collection_type=Collection.POP,
                public_mark__isnull=True,
                then=settings.DEFAULT_ESTABLISHMENT_PUBLIC_MARK
            ),
            default='public_mark',
            output_field=models.FloatField()))

    def annotate_mark_similarity(self, mark):
        """
        Return a QuerySet with annotated field - mark_similarity
        Description:
        Similarity mark determined by comparison with compared establishment mark
        """
        return self.annotate(mark_similarity=ExpressionWrapper(
            mark - F('intermediate_public_mark'),
            output_field=models.FloatField(default=0)
        ))

    def has_location(self):
        """Return objects with geo location."""
        return self.filter(address__coordinates__isnull=False)

    def similar_base(self, establishment):
        """
        Return filtered QuerySet by base filters.
        Filters including:
        1 Filter by type (and subtype) establishment.
        2 With annotated distance.
        3 By country
        """
        filters = {
            'establishment_type': establishment.establishment_type,
            'address__city__country': establishment.address.city.country
        }
        if establishment.establishment_subtypes.exists():
            filters.update(
                {'establishment_subtypes__in': establishment.establishment_subtypes.all()})

        return self.exclude(id=establishment.id) \
            .filter(**filters) \
            .annotate_distance(point=establishment.location)

    def similar_base_subquery(self, establishment, filters: dict) -> Subquery:
        """
        Return filtered Subquery object by filters.
        Filters including:
        1 Filter by transmitted filters.
        2 With ordering by distance.
        """
        return Subquery(
            self.similar_base(establishment)
                .filter(**filters)
                .order_by('distance')
                .distinct()
                .values_list('id', flat=True)[:settings.LIMITING_QUERY_OBJECTS]
        )

    def similar_restaurants(self, restaurant):
        """
        Return QuerySet with objects that similar to Restaurant.
        :param restaurant: Establishment instance.
        """

        ids_by_subquery = self.similar_base_subquery(
            establishment=restaurant,
            filters={
                'reviews__status': Review.READY,
                'public_mark__gte': 10,
                'establishment_gallery__is_main': True,
            }
        )
        # todo: fix this - replace ids_by_subquery.queryset on ids_by_subquery
        return self.filter(id__in=ids_by_subquery.queryset) \
            .annotate_intermediate_public_mark() \
            .annotate_mark_similarity(mark=restaurant.public_mark) \
            .order_by('mark_similarity') \
            .distinct('mark_similarity', 'id')

    def same_subtype(self, establishment):
        """Annotate flag same subtype."""
        return self.annotate(same_subtype=Case(
            models.When(
                establishment_subtypes__in=establishment.establishment_subtypes.all(),
                then=True
            ),
            default=False,
            output_field=models.BooleanField(default=False)
        ))

    def similar_artisans_producers(self, establishment):
        """
        Return QuerySet with objects that similar to Artisan/Producer(s).
        :param establishment: Establishment instance
        """
        return self.similar_base(establishment) \
            .same_subtype(establishment) \
            .has_published_reviews() \
            .order_by(F('same_subtype').desc(),
                      F('distance').asc()) \
            .distinct('same_subtype', 'distance', 'id')

    def by_wine_region(self, wine_region):
        """
        Return filtered QuerySet by wine region in wine origin.
        :param wine_region: wine region.
        """
        return self.filter(wine_origin__wine_region=wine_region).distinct()

    def by_wine_sub_region(self, wine_sub_region):
        """
        Return filtered QuerySet by wine region in wine origin.
        :param wine_sub_region: wine sub region.
        """
        return self.filter(wine_origin__wine_sub_region=wine_sub_region).distinct()

    def similar_wineries(self, winery):
        """
        Return QuerySet with objects that similar to Winery.
        :param winery: Establishment instance
        """
        return self.similar_base(winery) \
            .order_by(F('wine_origins__wine_region').asc(),
                      F('wine_origins__wine_sub_region').asc(),
                      F('distance').asc()) \
            .distinct('wine_origins__wine_region',
                      'wine_origins__wine_sub_region',
                      'distance',
                      'id')

    def last_reviewed(self, point: Point):
        """
        Return QuerySet with last reviewed establishments.
        :param point: location Point object, needs to ordering
        """
        subquery_filter_by_distance = Subquery(
            self.filter(image_url__isnull=False, public_mark__gte=10)
                .has_published_reviews()
                .annotate_distance(point=point)
                .order_by('distance')[:settings.LIMITING_QUERY_OBJECTS]
                .values('id')
        )
        return self.filter(id__in=subquery_filter_by_distance) \
            .order_by('-reviews__published_at')

    def prefetch_comments(self):
        """Prefetch last comment."""
        from comment.models import Comment
        return self.prefetch_related(
            models.Prefetch('comments',
                            queryset=Comment.objects.exclude(is_publish=False).order_by('-created'),
                            to_attr='comments_prefetched')
        )

    def prefetch_actual_employees(self):
        """Prefetch actual employees."""
        return self.prefetch_related(
            models.Prefetch('establishmentemployee_set',
                            queryset=EstablishmentEmployee.objects.actual().select_related(
                                'position'),
                            to_attr='actual_establishment_employees'))

    def annotate_in_favorites(self, user):
        """Annotate flag in_favorites"""
        favorite_establishment_ids = []
        if user.is_authenticated:
            favorite_establishment_ids = user.favorite_establishment_ids
        return self.annotate(in_favorites=Case(
            When(
                id__in=favorite_establishment_ids,
                then=True),
            default=False,
            output_field=models.BooleanField(default=False)))

    def by_distance_from_point(self, center, radius, unit='m'):
        """
        Returns nearest establishments

        :param center: point from which to find nearby establishments
        :param radius: the maximum distance within the radius of which to look for establishments
        :return: all establishments within the specified radius of specified point
        :param unit: length unit e.g. m, km. Default is 'm'.
        """
        kwargs = {unit: radius}
        return self.filter(address__coordinates__distance_lte=(center, DistanceMeasure(**kwargs)))

    def artisans(self):
        """Return artisans."""
        return self.filter(establishment_type__index_name__icontains=EstablishmentType.ARTISAN)

    def producers(self):
        """Return producers."""
        return self.filter(establishment_type__index_name__icontains=EstablishmentType.PRODUCER)

    def restaurants(self):
        """Return restaurants."""
        return self.filter(establishment_type__index_name__icontains=EstablishmentType.RESTAURANT)

    def wineries(self):
        """Return wineries."""
        return self.producers().filter(
            establishment_subtypes__index_name__icontains=EstablishmentSubType.WINERY)

    def by_type(self, value):
        """Return QuerySet with type by value."""
        return self.filter(establishment_type__index_name__icontains=value)

    def by_subtype(self, value):
        """Return QuerySet with subtype by value."""
        return self.filter(establishment_subtypes__index_name__icontains=value)

    def by_public_mark_range(self, min_value, max_value):
        """Filter by public mark range."""
        return self.filter(public_mark__gte=min_value, public_mark__lte=max_value)

    def exclude_public_mark_ranges(self, ranges):
        """Exclude public mark ranges."""
        return self.exclude(reduce(or_, [Q(public_mark__gte=r[0],
                                           public_mark__lte=r[1])
                                         for r in ranges]))

    def exclude_countries(self, countries):
        """Exclude countries."""
        return self.exclude(address__city__country__in=countries)

    def with_certain_tag_category_related(self, index_name, attr_name):
        """Includes extra tags."""
        return self.prefetch_related(
            Prefetch('tags', queryset=Tag.objects.filter(category__index_name=index_name),
                     to_attr=attr_name)
        )


class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
                    TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):
    """Establishment model."""

    # todo: delete image URL fields after moving on gallery

    old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
    name = models.CharField(_('name'), max_length=255, default='')
    transliterated_name = models.CharField(default='', max_length=255,
                                           verbose_name=_('Transliterated name'))
    index_name = models.CharField(_('Index name'), max_length=255, default='')
    description = TJSONField(blank=True, null=True, default=None,
                             verbose_name=_('description'),
                             help_text='{"en-GB":"some text"}')
    public_mark = models.PositiveIntegerField(blank=True, null=True,
                                              default=None,
                                              verbose_name=_('public mark'), )
    # todo: set default 0
    toque_number = models.PositiveIntegerField(blank=True, null=True,
                                               default=None,
                                               verbose_name=_('toque number'), )
    establishment_type = models.ForeignKey(EstablishmentType,
                                           related_name='establishment',
                                           on_delete=models.PROTECT,
                                           verbose_name=_('type'))
    establishment_subtypes = models.ManyToManyField(EstablishmentSubType,
                                                    blank=True,
                                                    related_name='subtype_establishment',
                                                    verbose_name=_('subtype'))
    address = models.ForeignKey(Address, blank=True, null=True, default=None,
                                on_delete=models.PROTECT,
                                verbose_name=_('address'))
    price_level = models.PositiveIntegerField(blank=True, null=True,
                                              default=None,
                                              verbose_name=_('price level'))
    website = models.URLField(blank=True, null=True, default=None, max_length=255,
                              verbose_name=_('Web site URL'))
    facebook = models.URLField(blank=True, null=True, default=None, max_length=255,
                               verbose_name=_('Facebook URL'))
    twitter = models.URLField(blank=True, null=True, default=None, max_length=255,
                              verbose_name=_('Twitter URL'))
    lafourchette = models.URLField(blank=True, null=True, default=None, max_length=255,
                                   verbose_name=_('Lafourchette URL'))
    guestonline_id = models.PositiveIntegerField(blank=True, verbose_name=_('guestonline id'),
                                                 null=True, default=None, )
    lastable_id = models.TextField(blank=True, verbose_name=_('lastable id'), unique=True,
                                   null=True, default=None, )
    booking = models.URLField(blank=True, null=True, default=None, max_length=255,
                              verbose_name=_('Booking URL'))
    is_publish = models.BooleanField(default=False, verbose_name=_('Publish status'))
    schedule = models.ManyToManyField(to='timetable.Timetable', blank=True,
                                      verbose_name=_('Establishment schedule'),
                                      related_name='schedule')
    transportation = models.TextField(blank=True, null=True, default=None,
                                      verbose_name=_('Transportation'))
    collections = models.ManyToManyField(to='collection.Collection',
                                         related_name='establishments',
                                         blank=True, default=None,
                                         verbose_name=_('Collections'))
    gallery = models.ManyToManyField('gallery.Image', through='EstablishmentGallery')
    preview_image_url = models.URLField(verbose_name=_('Preview image URL path'), max_length=255,
                                        blank=True, null=True, default=None)
    slug = models.SlugField(unique=True, max_length=255, null=True,
                            verbose_name=_('Establishment slug'))
    tz = TimeZoneField(default=settings.TIME_ZONE)

    awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
    tags = models.ManyToManyField('tag.Tag', related_name='establishments',
                                  verbose_name=_('Tag'))
    reviews = generic.GenericRelation(to='review.Review')
    comments = generic.GenericRelation(to='comment.Comment')
    carousels = generic.GenericRelation(to='main.Carousel')
    favorites = generic.GenericRelation(to='favorites.Favorites')
    currency = models.ForeignKey(Currency, blank=True, null=True, default=None,
                                 on_delete=models.PROTECT,
                                 verbose_name=_('currency'))
    purchased_products = models.ManyToManyField('product.Product', blank=True,
                                                through='product.PurchasedProduct',
                                                related_name='establishments',
                                                verbose_name=_('purchased plaques'),
                                                help_text=_('Attribute from legacy db.\n'
                                                            'Must be deleted after the '
                                                            'implementation of the market.'))

    objects = EstablishmentQuerySet.as_manager()

    class Meta:
        """Meta class."""

        verbose_name = _('Establishment')
        verbose_name_plural = _('Establishments')

    def __str__(self):
        return f'id:{self.id}-{self.name}'

    def delete(self, using=None, keep_parents=False):
        """Overridden delete method"""
        # TODO: If this does not contradict the plan,
        #  it is better to change it.
        #  Just add CASCADE to Company and Note in establishment fk field.
        # Delete all related companies
        self.companies.all().delete()
        # Delete all related notes
        self.notes.all().delete()
        return super().delete(using, keep_parents)

    @property
    def visible_tags(self):
        return super().visible_tags \
            .exclude(category__index_name__in=['guide', 'collection', 'purchased_item',
                                               'business_tag', 'business_tags_de']) \
            .exclude(value__in=['rss', 'rss_selection'])
        # todo: recalculate toque_number

    @property
    def visible_tags_detail(self):
        """Removes some tags from detail Establishment representation"""
        return self.visible_tags.exclude(category__index_name__in=['tag'])

    def recalculate_public_mark(self):
        fresh_review = self.reviews.published().order_by('-modified').first()
        if fresh_review:
            self.public_mark = fresh_review.mark
        else:
            self.public_mark = None
        self.save()

    def recalculate_toque_number(self):
        toque_number = 0
        if self.address and self.public_mark:
            toque_number = RatingStrategy.objects. \
                get_toque_number(country=self.address.city.country,
                                 public_mark=self.public_mark)
        self.toque_number = toque_number
        self.save()

    def recalculate_price_level(self, low_price=None, high_price=None):
        if low_price is None or high_price is None:
            low_price, high_price = self.get_price_level()
        # todo: calculate price level
        self.price_level = 3

    def get_price_level(self):
        country = self.address.city.country
        return country.low_price, country.high_price


    def set_establishment_type(self, establishment_type):
        self.establishment_type = establishment_type
        self.establishment_subtypes.exclude(
            establishement_type=establishment_type).delete()

    def add_establishment_subtype(self, establishment_subtype):
        if establishment_subtype.establishment_type != self.establishment_type:
            raise ValidationError('Establishment type of subtype does not match')
        self.establishment_subtypes.add(establishment_subtype)

    @property
    def vintage_year(self):
        last_review = self.reviews.by_status(Review.READY).last()
        if last_review:
            return last_review.vintage

    @property
    def best_price_menu(self):
        return 150

    @property
    def best_price_carte(self):
        return 200

    @property
    def range_price_menu(self):
        plates = self.menu_set.filter(
            models.Q(category={'en-GB': 'formulas'})
        ).aggregate(
            max=models.Max('plate__price', output_field=models.FloatField()),
            min=models.Min('plate__price', output_field=models.FloatField()))
        return plates

    @property
    def range_price_carte(self):
        plates = self.menu_set.filter(
            models.Q(category={'en-GB': 'starter'}) |
            models.Q(category={'en-GB': 'main_course'}) |
            models.Q(category={'en-GB': 'dessert'})
        ).aggregate(
            max=models.Max('plate__price', output_field=models.FloatField()),
            min=models.Min('plate__price', output_field=models.FloatField()),
        )
        return plates

    @property
    def works_noon(self):
        """ Used for indexing working by day """
        return [ret.weekday for ret in self.schedule.all() if ret.works_at_noon]

    @property
    def works_at_weekday(self):
        """ Used for indexing by working whole day criteria """
        return [ret.weekday for ret in self.schedule.all()]

    @property
    def works_evening(self):
        """ Used for indexing working by day """
        return [ret.weekday for ret in self.schedule.all() if ret.works_at_afternoon]

    @property
    def works_now(self):
        """ Is establishment working now """
        now_at_est_tz = datetime.now(tz=self.tz)
        current_week = now_at_est_tz.weekday()
        schedule_for_today = self.schedule.filter(weekday=current_week).first()
        if schedule_for_today is None or schedule_for_today.opening_time is None or schedule_for_today.ending_time is None:
            return False
        time_at_est_tz = now_at_est_tz.time()
        return schedule_for_today.ending_time > time_at_est_tz > schedule_for_today.opening_time

    @property
    def timezone_as_str(self):
        """ Returns tz in str format"""
        return self.tz.localize(datetime.now()).strftime('%z')

    @property
    def tags_indexing(self):
        return [{'id': tag.metadata.id,
                 'label': tag.metadata.label} for tag in self.tags.all()]

    @property
    def last_published_review(self):
        """Return last published review"""
        return self.reviews.published() \
            .order_by('-published_at').first()

    @property
    def location(self):
        """
        Return Point object of establishment location
        """
        return self.address.coordinates

    @property
    def the_most_recent_award(self):
        return Award.objects.filter(Q(establishment=self) | Q(employees__establishments=self)) \
            .latest(field_name='vintage_year')

    @property
    def country_id(self):
        """
        Return Country id of establishment location
        """
        return self.address.country_id

    @property
    def establishment_id(self):
        """
        Return establishment id of establishment location
        """
        return self.id

    @property
    def wines(self):
        """Return list products with type wine"""
        return self.products.wines()

    @property
    def main_image(self):
        qs = self.establishment_gallery.main_image()
        if qs.exists():
            return qs.first().image

    @property
    def restaurant_category_indexing(self):
        return self.tags.filter(category__index_name='category')

    @property
    def restaurant_cuisine_indexing(self):
        return self.tags.filter(category__index_name='cuisine')

    @property
    def artisan_category_indexing(self):
        return self.tags.filter(category__index_name='shop_category')

    @property
    def last_comment(self):
        if hasattr(self, 'comments_prefetched') and len(self.comments_prefetched):
            return self.comments_prefetched[0]

    @property
    def wine_origins_unique(self):
        return self.wine_origins.distinct('wine_region')

    @property
    def contact_phones(self):
        if self.phones:
            return [phone.phone.as_e164 for phone in self.phones.all()]

    @property
    def establishment_subtype_labels(self):
        if self.establishment_subtypes:
            return [transform_into_readable_str(label)
                    for label in self.establishment_subtypes.all().values_list('index_name', flat=True)]

    @property
    def schedule_display(self):
        if self.schedule:
            timetable = {}
            for weekday, closed_at, opening_at in self.schedule.all().values_list('weekday', 'closed_at', 'opening_at'):
                weekday = dict(Timetable.WEEKDAYS_CHOICES).get(weekday)
                working_hours = 'Closed'
                if closed_at and opening_at:
                    working_hours = f'{str(opening_at)[:-3]} - {str(closed_at)[:-3]}'
                timetable.update({weekday: working_hours})
            return timetable

    @property
    def price_level_display(self):
        if self.get_price_level() and self.currency:
            min_value, max_value = self.get_price_level()
            currency = self.currency.sign
            return f'From {min_value}{currency} to {max_value}{currency}'

    @property
    def last_published_review_data(self):
        if self.last_published_review:
            return self.last_published_review.text

    @property
    def public_mark_display(self):
        if self.public_mark and self.public_mark > 1:
            return f'{self.public_mark}/20'

    @property
    def metadata(self):
        if self.establishment_type:
            metadata = []
            tag_categories = (
                self.establishment_type.tag_categories.exclude(index_name__in=[
                    'business_tag', 'purchased_item', 'accepted_payments_de',
                    'accepted_payments_hr', 'drinks', 'bottles_per_year',
                    'serial_number', 'surface', 'cooperative', 'tag',
                    'outside_sits', 'private_room']).values_list('index_name', flat=True)
            )
            for category in tag_categories:
                tags = self.tags.filter(category__index_name=category).values_list('value', flat=True)

                if tags.exists():
                    category_tags = {category: []}
                    for tag in tags:
                        category_tags[category].append(tag)
                    metadata.append(category_tags)
            return metadata


class EstablishmentNoteQuerySet(models.QuerySet):
    """QuerySet for model EstablishmentNote."""


class EstablishmentNote(ProjectBaseMixin):
    """Note model for Establishment entity."""
    old_id = models.PositiveIntegerField(null=True, blank=True)
    text = models.TextField(verbose_name=_('text'))
    establishment = models.ForeignKey(Establishment, on_delete=models.PROTECT,
                                      related_name='notes',
                                      verbose_name=_('establishment'))
    user = models.ForeignKey('account.User', on_delete=models.PROTECT,
                             null=True,
                             related_name='establishment_notes',
                             verbose_name=_('author'))

    objects = EstablishmentNoteQuerySet.as_manager()

    class Meta:
        """Meta class."""
        verbose_name_plural = _('establishment notes')
        verbose_name = _('establishment note')


class EstablishmentGallery(IntermediateGalleryModelMixin):
    establishment = models.ForeignKey(Establishment, null=True,
                                      related_name='establishment_gallery',
                                      on_delete=models.CASCADE,
                                      verbose_name=_('establishment'))
    image = models.ForeignKey('gallery.Image', null=True,
                              related_name='establishment_gallery',
                              on_delete=models.CASCADE,
                              verbose_name=_('image'))

    class Meta:
        """Meta class."""
        verbose_name = _('establishment gallery')
        verbose_name_plural = _('establishment galleries')
        unique_together = (('establishment', 'is_main'), ('establishment', 'image'))


class Position(BaseAttributes, TranslatedFieldsMixin):
    """Position model."""

    STR_FIELD_NAME = 'name'

    name = TJSONField(blank=True, null=True, default=None, verbose_name=_('Description'),
                      help_text='{"en":"some text"}')

    priority = models.IntegerField(unique=True, null=True, default=None)

    index_name = models.CharField(max_length=255, db_index=True, unique=True,
                                  null=True, verbose_name=_('Index name'))

    class Meta:
        """Meta class."""

        verbose_name = _('Position')
        verbose_name_plural = _('Positions')


class EstablishmentEmployeeQuerySet(models.QuerySet):
    """Extended queryset for EstablishmentEmployee model."""

    def actual(self):
        """Actual objects.."""
        now = timezone.now()
        return self.filter(models.Q(from_date__lte=now),
                           (models.Q(to_date__gte=now) |
                            models.Q(to_date__isnull=True)))


class EstablishmentEmployee(BaseAttributes):
    """EstablishmentEmployee model."""

    IDLE = 'I'
    ACCEPTED = 'A'
    DECLINED = 'D'

    STATUS_CHOICES = (
        (IDLE, 'Idle'),
        (ACCEPTED, 'Accepted'),
        (DECLINED, 'Declined'),
    )

    establishment = models.ForeignKey(Establishment, on_delete=models.PROTECT,
                                      verbose_name=_('Establishment'))
    employee = models.ForeignKey('establishment.Employee', on_delete=models.PROTECT,
                                 verbose_name=_('Employee'))
    from_date = models.DateTimeField(default=timezone.now, verbose_name=_('From date'),
                                     null=True, blank=True)
    to_date = models.DateTimeField(blank=True, null=True, default=None,
                                   verbose_name=_('To date'))
    position = models.ForeignKey(Position, on_delete=models.PROTECT,
                                 verbose_name=_('Position'))

    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=IDLE)

    # old_id = affiliations_id
    old_id = models.IntegerField(verbose_name=_('Old id'), null=True, blank=True)

    objects = EstablishmentEmployeeQuerySet.as_manager()


class EmployeeQuerySet(models.QuerySet):

    def _generic_search(self, value, filter_fields_names: List[str]):
        """Generic method for searching value in specified fields"""
        filters = [
            {f'{field}__icontains': value}
            for field in filter_fields_names
        ]
        return self.filter(reduce(lambda x, y: x | y, [models.Q(**i) for i in filters]))

    def search_by_name_or_last_name(self, value):
        """Search by name or last_name."""
        return self._generic_search(value, ['name', 'last_name'])

    def actual_establishment(self):
        e = EstablishmentEmployee.objects.actual().filter(employee=self)
        return self.prefetch_related(models.Prefetch('establishmentemployee_set',
                                                     queryset=EstablishmentEmployee.objects.actual()
                                                     )).all().distinct()


class Employee(BaseAttributes):
    """Employee model."""

    user = models.OneToOneField('account.User', on_delete=models.PROTECT,
                                null=True, blank=True, default=None,
                                verbose_name=_('User'))
    name = models.CharField(max_length=255, verbose_name=_('Name'))
    last_name = models.CharField(max_length=255, verbose_name=_('Last Name'), null=True,
                                 default=None)

    # SEX CHOICES
    MALE = 0
    FEMALE = 1

    SEX_CHOICES = (
        (MALE, _('Male')),
        (FEMALE, _('Female'))
    )
    sex = models.PositiveSmallIntegerField(choices=SEX_CHOICES, verbose_name=_('Sex'), null=True,
                                           default=None)
    birth_date = models.DateTimeField(editable=True, verbose_name=_('Birth date'), null=True,
                                      default=None)
    email = models.EmailField(blank=True, null=True, default=None, verbose_name=_('Email'))
    phone = PhoneNumberField(null=True, default=None)
    toque_number = models.PositiveSmallIntegerField(verbose_name=_('Toque number'), null=True,
                                                    default=None)

    establishments = models.ManyToManyField(Establishment, related_name='employees',
                                            through=EstablishmentEmployee, )
    awards = generic.GenericRelation(to='main.Award', related_query_name='employees')
    tags = models.ManyToManyField('tag.Tag', related_name='employees',
                                  verbose_name=_('Tags'))
    # old_id = profile_id
    old_id = models.IntegerField(verbose_name=_('Old id'), null=True, blank=True)

    objects = EmployeeQuerySet.as_manager()

    class Meta:
        """Meta class."""

        verbose_name = _('Employee')
        verbose_name_plural = _('Employees')


class EstablishmentScheduleQuerySet(models.QuerySet):
    """QuerySet for model EstablishmentSchedule"""


class ContactPhone(models.Model):
    """Contact phone model."""
    establishment = models.ForeignKey(
        Establishment, related_name='phones', on_delete=models.CASCADE)
    phone = PhoneNumberField()

    class Meta:
        verbose_name = _('contact phone')
        verbose_name_plural = _('contact phones')

    def __str__(self):
        return f'{self.phone.as_e164}'


class ContactEmail(models.Model):
    """Contact email model."""
    establishment = models.ForeignKey(
        Establishment, related_name='emails', on_delete=models.CASCADE)
    email = models.EmailField()

    class Meta:
        verbose_name = _('contact email')
        verbose_name_plural = _('contact emails')

    def __str__(self):
        return f'{self.email}'


class Plate(TranslatedFieldsMixin, models.Model):
    """Plate model."""
    STR_FIELD_NAME = 'name'

    name = TJSONField(
        blank=True, null=True, default=None, verbose_name=_('name'),
        help_text='{"en-GB":"some text"}')
    description = TJSONField(
        blank=True, null=True, default=None, verbose_name=_('description'),
        help_text='{"en-GB":"some text"}')
    price = models.DecimalField(
        _('price'), max_digits=14, decimal_places=2)
    is_signature_plate = models.BooleanField(_('is signature plate'), default=False)
    currency = models.ForeignKey(
        'main.Currency', verbose_name=_('currency'), on_delete=models.CASCADE,
        blank=True, null=True, default=None)
    currency_code = models.CharField(
        _('currency code'), max_length=250, blank=True, null=True, default=None)

    menu = models.ForeignKey(
        'establishment.Menu', verbose_name=_('menu'), on_delete=models.CASCADE)

    @property
    def establishment_id(self):
        return self.menu.establishment.id

    class Meta:
        verbose_name = _('plate')
        verbose_name_plural = _('plates')


class Menu(TranslatedFieldsMixin, BaseAttributes):
    """Menu model."""

    STR_FIELD_NAME = 'category'

    category = TJSONField(
        blank=True, null=True, default=None, verbose_name=_('category'),
        help_text='{"en-GB":"some text"}')
    establishment = models.ForeignKey(
        'establishment.Establishment', verbose_name=_('establishment'),
        on_delete=models.CASCADE)

    class Meta:
        verbose_name = _('menu')
        verbose_name_plural = _('menu')


class SocialChoice(models.Model):
    title = models.CharField(_('title'), max_length=255, unique=True)

    class Meta:
        verbose_name = _('social choice')
        verbose_name_plural = _('social choices')

    def __str__(self):
        return self.title


class SocialNetwork(models.Model):
    old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
    establishment = models.ForeignKey(
        'Establishment',
        verbose_name=_('establishment'),
        related_name='socials',
        on_delete=models.CASCADE,
    )
    network = models.ForeignKey(
        SocialChoice,
        verbose_name=_('social network'),
        related_name='social_links',
        on_delete=models.CASCADE,
    )
    url = models.URLField(_('URL'), max_length=255)

    class Meta:
        verbose_name = _('social network')
        verbose_name_plural = _('social networks')

    def __str__(self):
        return f'{self.network.title}: {self.url}'


class RatingStrategyManager(models.Manager):
    """Extended manager for RatingStrategy."""

    def get_toque_number(self, country, public_mark):
        """Get toque number for country and public_mark."""
        qs = self.model.objects.by_country(country)
        if not qs.exists():
            qs = self.model.objects.by_country(None)
        obj = qs.for_public_mark(public_mark).first()
        if obj:
            return obj.toque_number
        return 0


class RatingStrategyQuerySet(models.QuerySet):
    """Extended queryset for RatingStrategy."""

    def by_country(self, country):
        """Filter by country."""
        return self.filter(country=country)

    def with_country(self, switcher=True):
        """With country."""
        return self.exclude(country__isnull=switcher)

    def for_public_mark(self, public_mark):
        """Filter for value."""
        return self.filter(public_mark_min_value__lte=public_mark,
                           public_mark_max_value__gte=public_mark)


class RatingStrategy(ProjectBaseMixin):
    """Rating Strategy model."""

    TOQUE_NUMBER_CHOICES = (
        (1, _('One')),
        (2, _('Two')),
        (3, _('Three')),
        (4, _('Four')),
        (5, _('Five')),
    )

    country = models.ForeignKey('location.Country', null=True, blank=True,
                                default=None, on_delete=models.CASCADE,
                                verbose_name=_('Country'))
    toque_number = models.IntegerField(choices=TOQUE_NUMBER_CHOICES)
    public_mark_min_value = models.IntegerField()
    public_mark_max_value = models.IntegerField()

    objects = RatingStrategyManager.from_queryset(RatingStrategyQuerySet)()

    class Meta:
        """Meta class."""

        verbose_name = _('Rating strategy')
        verbose_name_plural = _('Rating strategy')
        unique_together = ('country', 'toque_number')

    def __str__(self):
        return f'{self.country.code if self.country else "Other country"}. ' \
               f'"{self.toque_number}": {self.public_mark_min_value}-' \
               f'{self.public_mark_max_value}'


class CompanyQuerySet(models.QuerySet):
    """QuerySet for model Company."""


class Company(ProjectBaseMixin):
    """Establishment company model."""

    establishment = models.ForeignKey(Establishment, on_delete=models.PROTECT,
                                      related_name='companies',
                                      verbose_name=_('establishment'))
    name = models.CharField(max_length=255, verbose_name=_('name'))
    phones = ArrayField(PhoneNumberField(max_length=128),
                        blank=True, null=True, default=None,
                        verbose_name=_('contact phones'))
    faxes = ArrayField(PhoneNumberField(max_length=128),
                       blank=True, null=True, default=None,
                       verbose_name=_('fax numbers'))
    legal_entity = models.CharField(max_length=255,
                                    blank=True, null=True, default=None,
                                    verbose_name=_('legal entity'))
    registry_number = models.CharField(max_length=255,
                                       blank=True, null=True, default=None,
                                       verbose_name=_('registry number'))
    vat_number = models.CharField(max_length=30,
                                  blank=True, null=True, default=None,
                                  verbose_name=_('VAT identification number'))
    sic_code = models.IntegerField(validators=[MinValueValidator(1),
                                               MaxValueValidator(9999)],
                                   blank=True, null=True, default=True,
                                   verbose_name=_('sic code'))
    address = models.ForeignKey(Address, on_delete=models.PROTECT,
                                blank=True, null=True, default=None,
                                related_name='companies',
                                verbose_name=_('address'))

    objects = CompanyQuerySet.as_manager()

    class Meta:
        """Meta class."""
        verbose_name = _('company')
        verbose_name_plural = _('companies')
