from django_elasticsearch_dsl_drf.compat import StringField, KeywordField
from elasticsearch_dsl import analyzer
from apps.authorization.models import UserRelations, Role
from apps.authentication.models import User, Organization
from django_elasticsearch_dsl.registries import registry
from django_elasticsearch_dsl import Document, fields

html_strip = analyzer(
    'html_strip',
    tokenizer="standard",
    filter=["lowercase", "stop", "snowball"],
    char_filter=["html_strip"]
)


@registry.register_document
class UserRelationDocument(Document):
    """Address Elasticsearch document."""

    # In different parts of the code different fields are used. There are
    # a couple of use cases: (1) more-like-this functionality, where `title`,
    # `description` and `summary` fields are used, (2) filter and filtering
    # functionality where all the fields are used.

    user = fields.ObjectField(properties={
        'username': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'mobile': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': KeywordField()
            }
        ),
        'national_code': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'first_name': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'last_name': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
    })
    organization = fields.ObjectField(properties={
        'name': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'type': fields.ObjectField(properties={
            'key': StringField(
                analyzer=html_strip,
                fields={
                    'raw': KeywordField(),
                    'suggest': fields.CompletionField()
                }
            )
        }),
        'national_unique_id': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'field_of_activity': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'company_code': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        ),
        'province': fields.ObjectField(properties={
            'name': StringField(
                analyzer=html_strip,
                fields={
                    'raw': KeywordField(),
                    'suggest': fields.CompletionField()
                }
            ),
        }),
        'city': fields.ObjectField(properties={
            'name': StringField(
                analyzer=html_strip,
                fields={
                    'raw': KeywordField(),
                    'suggest': fields.CompletionField()
                }
            ),
        }),
        'parent_organization': fields.ObjectField(properties={
            'name': StringField(
                analyzer=html_strip,
                fields={
                    'raw': KeywordField(),
                    'suggest': fields.CompletionField()
                }
            ),
            'unique_national_id': StringField(
                analyzer=html_strip,
                fields={
                    'raw': KeywordField(),
                    'suggest': fields.CompletionField()
                }
            ),
        })
    })
    role = fields.ObjectField(properties={
        'role_name': StringField(
            analyzer=html_strip,
            fields={
                'raw': KeywordField(),
                'suggest': fields.CompletionField()
            }
        )
    })

    class Index:
        name = 'userrelations'  # noqa
        settings = {
            'number_of_shards': 1,
            'number_of_replicas': 1  # number of copies from data in document
        }

    class Django:
        model = UserRelations
        relates_models = [User, Organization, Role]
