from __future__ import absolute_import, print_function
import csv

from django.core.management.base import BaseCommand, CommandError
from corehq import toggles
from corehq.apps.app_manager.models import Domain
from corehq.apps.toggle_ui.utils import find_static_toggle
from corehq.toggles import NAMESPACE_DOMAIN


class Command(BaseCommand):
    help = """
    Checks if an add on is enabled or was ever enabled for applications under all domains
    or under a specific domain with domain name if passed
    Also checks if toggle ENABLE_ALL_ADD_ONS enabled for domains
    """

    def add_arguments(self, parser):
        parser.add_argument('--domain', type=str)
        parser.add_argument('add_on_name')
        parser.add_argument('--add_to_toggle')

    @staticmethod
    def _iter_domains(options):
        if options.get('domain'):
            yield Domain.get_by_name(options['domain'])
        else:
            domain_ids = [
                result['id'] for result in
                Domain.get_db().view(
                    "domain/domains", reduce=False, include_docs=False
                ).all()
            ]
            print("Count of domains : %s" % len(domain_ids))
            for domain_id in domain_ids:
                yield Domain.get(domain_id)

    def handle(self, add_on_name, *args, **options):
        add_to_toggle = options.get('add_to_toggle')
        if add_to_toggle:
            add_to_toggle = find_static_toggle(add_to_toggle)
            if not add_to_toggle:
                raise CommandError('Toggle %s not found.' % add_to_toggle)
        with open("apps_with_feature_%s.csv" % add_on_name, "w") as csvfile:
            writer = csv.DictWriter(csvfile,
                                    fieldnames=[
                                        'domain', 'application_id', 'app_name',
                                        'all_add_ons_enabled', 'status'
                                    ])
            writer.writeheader()
            for domain in self._iter_domains(options):
                for application in domain.full_applications(include_builds=False):
                    if not application.is_remote_app():
                        all_add_ons_enabled = toggles.ENABLE_ALL_ADD_ONS.enabled(domain.name)
                        if add_on_name in application.add_ons or all_add_ons_enabled:
                            writer.writerow({
                                'domain': domain.name,
                                'application_id': application.get_id,
                                'app_name': application.name,
                                'all_add_ons_enabled': all_add_ons_enabled,
                                'status': application.add_ons.get(add_on_name)
                            })
                            if add_to_toggle:
                                add_to_toggle.set(domain.name, True, NAMESPACE_DOMAIN)

@@ -560,24 +560,255 @@
 for domains%0A
+    Can also enable the domains found for another toggle in case the add-on is meant to%0A    be switched to a toggle%0A    Example: ./manage.py applications_with_add_ons custom_icon_badges%0A             --add_to_toggle=new_custom_icon%0A
     %22%22%22%0A%0A   

add help content for adding to toggle [skip ci]