import textwrap

from django.core.management.base import BaseCommand

from corehq.apps.domain.dbaccessors import iter_all_domains_and_deleted_domains_with_name
from corehq.apps.domain.utils import is_domain_in_use


class Command(BaseCommand):
    help = "Deletes the given domain and its contents"

    def add_arguments(self, parser):
        parser.add_argument(
            'domain_name',
        )
        parser.add_argument(
            '--noinput',
            action='store_true',
            dest='noinput',
            default=False,
            help='Skip important confirmation warnings.',
        )
        parser.add_argument(
            '--ignore-domain-in-use-check',
            action='store_true',
            dest='ignore_domain_in_use',
            default=False,
            help='Allow deleting a domain that is in use.',
        )

    def handle(self, domain_name, **options):
        if not options['ignore_domain_in_use'] and is_domain_in_use(domain_name):
            print(f'WARNING: Domain {domain_name} is currently in use. If you are sure you want to delete'
                  f'this domain, use the "--ignore_domain_in_use" argument.')
            return

        domain_objs = list(iter_all_domains_and_deleted_domains_with_name(domain_name))
        if not domain_objs:
            print('domain with name "{}" not found'.format(domain_name))
            return
        if len(domain_objs) > 1:
            print("FYI: There are multiple domain objects for this domain"
                  "and they will all be soft-deleted.")
        if not options['noinput']:
            confirm = input(textwrap.dedent(
                f"""
                Are you sure you want to delete the domain "{domain_name}" and all of it's data?
                This operation is not reversible and all forms and cases will be PERMANENTLY deleted.

                Type the domain's name again to continue, or anything else to cancel:
                """
            ))
            if confirm != domain_name:
                print("\n\t\tDomain deletion cancelled.")
                return
        print(f"Soft-Deleting domain {domain_name} "
              "(i.e. switching its type to Domain-Deleted, "
              "which will prevent anyone from reusing that domain)")
        for domain_obj in domain_objs:
            assert domain_obj.name == domain_name  # Just to be really sure!
            domain_obj.delete(leave_tombstone=True)
        print("Operation completed")

import textwrap

from django.core.management.base import BaseCommand

from corehq.apps.domain.dbaccessors import iter_all_domains_and_deleted_domains_with_name


class Command(BaseCommand):
    help = "Deletes the given domain and its contents"

    def add_arguments(self, parser):
        parser.add_argument(
            'domain_name',
        )
        parser.add_argument(
            '--noinput',
            action='store_true',
            dest='noinput',
            default=False,
            help='Skip important confirmation warnings.',
        )

    def handle(self, domain_name, **options):
        domain_objs = list(iter_all_domains_and_deleted_domains_with_name(domain_name))
        if not domain_objs:
            print('domain with name "{}" not found'.format(domain_name))
            return
        if len(domain_objs) > 1:
            print("FYI: There are multiple domain objects for this domain"
                  "and they will all be soft-deleted.")
        if not options['noinput']:
            confirm = input(textwrap.dedent(
                f"""
                Are you sure you want to delete the domain "{domain_name}" and all of it's data?
                This operation is not reversible and all forms and cases will be PERMANENTLY deleted.

                Type the domain's name again to continue, or anything else to cancel:
                """
            ))
            if confirm != domain_name:
                print("\n\t\tDomain deletion cancelled.")
                return
        print(f"Soft-Deleting domain {domain_name} "
              "(i.e. switching its type to Domain-Deleted, "
              "which will prevent anyone from reusing that domain)")
        for domain_obj in domain_objs:
            assert domain_obj.name == domain_name  # Just to be really sure!
            domain_obj.delete(leave_tombstone=True)
        print("Operation completed")

revert check for deleted domain in delete_domain command