"""
Use this command to delete every single thing in the database
"""

from django.core.management.base import CommandError
from django.core.management.color import no_style
from django.core.management.commands import flush
from django.core.management.sql import sql_flush
from django.db import connections


class Command(flush.Command):
    help = __doc__

    def handle(self, **options):
        interactive = options["interactive"]
        database = options["database"]
        connection = connections[database]

        self.style = no_style()

        sql_list = sql_flush(
            self.style,
            connection,
            reset_sequences=True,
            allow_cascade=True,
        )

        if interactive:
            confirm = input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the %r database,
and return each table to an empty state.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict["NAME"])
        else:
            confirm = "yes"

        if confirm == "yes":
            try:
                connection.ops.execute_sql_flush(sql_list)
            except Exception as exc:
                raise CommandError(
                    "Database %s couldn't be flushed. Possible reasons:\n"
                    "  * The database isn't running or isn't configured correctly.\n"
                    "  * At least one of the expected database tables doesn't exist.\n"
                    "  * The SQL was invalid.\n"
                    "Hint: Look at the output of 'django-admin sqlflush'. "
                    "That's the SQL this command wasn't able to run.\n"
                    % (connection.settings_dict["NAME"],)
                ) from exc
        else:
            self.stdout.write("Flush cancelled.\n")
