from elasticsearch.helpers import scan

from flask_api_auth.models import APIRole, APIUser

from distribution import app
from distribution.models.feed import FeedItem
from distribution.models.taxonomy import TaxonomyTopic


def update_taxonomy_index():
    actions = []

    generator = scan(FeedItem.manager.backend.client,
                     {},
                     index=app.config.get('ELASTICSEARCH_FEED_INDEX_NAME', 'distribution_feed'),
                     doc_type=TaxonomyTopic.manager.doc_type,
                     raise_on_error=False)

    for key in generator:

        actions.append({'_op_type': 'index',
                        '_index': app.config.get('ELASTICSEARCH_TAXONOMY_INDEX_NAME',
                                                 'distribution_taxonomy'),
                        '_type': TaxonomyTopic.manager.doc_type,
                        '_id': key['_id'],
                        '_source': key['_source']})
        actions.append({'_op_type': 'delete',
                        '_index': app.config.get('ELASTICSEARCH_FEED_INDEX_NAME',
                                                 'distribution_feed'),
                        '_type': TaxonomyTopic.manager.doc_type,
                        '_id': key['_id']})

    if actions:
        print '-- Executing {} actions for taxonomy --'.format(len(actions))
        TaxonomyTopic.manager.backend.bulk(actions)


def update_auth_role_index():
    actions = []

    generator = scan(FeedItem.manager.backend.client,
                     {},
                     index=app.config.get('ELASTICSEARCH_FEED_INDEX_NAME', 'distribution_feed'),
                     doc_type=APIRole.manager.doc_type,
                     raise_on_error=False)

    for key in generator:

        actions.append({'_op_type': 'index',
                        '_index': app.config.get('ELASTICSEARCH_AUTH_INDEX_NAME',
                                                 'distribution_auth'),
                        '_type': APIRole.manager.doc_type,
                        '_id': key['_id'],
                        '_source': key['_source']})
        actions.append({'_op_type': 'delete',
                        '_index': app.config.get('ELASTICSEARCH_FEED_INDEX_NAME',
                                                 'distribution_feed'),
                        '_type': TaxonomyTopic.manager.doc_type,
                        '_id': key['_id']})

    if actions:
        print '-- Executing {} actions for api role --'.format(len(actions))
        APIRole.manager.backend.bulk(actions)


def update_auth_user_index():
    actions = []

    generator = scan(FeedItem.manager.backend.client,
                     {},
                     index=app.config.get('ELASTICSEARCH_FEED_INDEX_NAME', 'distribution_feed'),
                     doc_type=APIUser.manager.doc_type,
                     raise_on_error=False)

    for key in generator:

        actions.append({'_op_type': 'index',
                        '_index': app.config.get('ELASTICSEARCH_AUTH_INDEX_NAME',
                                                 'distribution_auth'),
                        '_type': APIUser.manager.doc_type,
                        '_id': key['_id'],
                        '_source': key['_source']})
        actions.append({'_op_type': 'delete',
                        '_index': app.config.get('ELASTICSEARCH_FEED_INDEX_NAME',
                                                 'distribution_feed'),
                        '_type': TaxonomyTopic.manager.doc_type,
                        '_id': key['_id']})

    if actions:
        print '-- Executing {} actions for api user --'.format(len(actions))
        APIUser.manager.backend.bulk(actions)


if __name__ == "__main__":
    update_taxonomy_index()
    update_auth_role_index()
    update_auth_user_index()
