import socket

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.utils import OperationalError, ProgrammingError

from daiquiri.metadata.models import Database


class Command(BaseCommand):

    requires_system_checks = False
    can_import_settings = True

    def get_config(self, key):
        config = settings.DATABASES.get(key)

        if 'HOST' not in config or not config['HOST']:
            config['HOST'] = 'localhost'
            config['CLIENT'] = 'localhost'
        else:
            config['CLIENT'] = socket.gethostname()

        config['PREFIX'] = settings.QUERY_USER_DATABASE_PREFIX

        return config

    def handle(self, *args, **options):

        default = self.get_config('default')
        tap = self.get_config('tap')
        data = self.get_config('data')

        print('''-- Run the following commands on \'%(HOST)s\':

CREATE DATABASE `%(NAME)s`;
CREATE USER \'%(USER)s\'@\'%(CLIENT)s\' identified by \'%(PASSWORD)s\'
GRANT ALL PRIVILEGES ON `%(NAME)s`.* to \'%(USER)s\'@\'%(CLIENT)s\'
''' % default)

        print('''-- Run the following commands on \'%(HOST)s\':

CREATE DATABASE `%(NAME)s`;
CREATE USER \'%(USER)s\'@\'%(CLIENT)s\' identified by \'%(PASSWORD)s\'
GRANT ALL PRIVILEGES ON `%(NAME)s`.* to \'%(USER)s\'@\'%(CLIENT)s\'
''' % tap)

        print('''-- Run the following commands on \'%(HOST)s\':

CREATE USER \'%(USER)s\'@\'%(CLIENT)s\' identified by \'%(PASSWORD)s\'
GRANT ALL PRIVILEGES ON `%(PREFIX)s`%%.* to \'%(USER)s\'@\'%(CLIENT)s\'
''' % data)

        try:
            for database in Database.objects.all():
                data.update({'DATABASE_NAME': database.name})
                print('GRANT SELECT ON `%(DATABASE_NAME)s`.* to \'%(USER)s\'@\'%(CLIENT)s\'' % data)
        except (OperationalError, ProgrammingError):
            pass
        else:
            print('')

@@ -989,32 +989,33 @@
 %5C'%25(PASSWORD)s%5C'
+;
 %0AGRANT ALL PRIVI
@@ -1058,32 +1058,33 @@
 '@%5C'%25(CLIENT)s%5C'
+;
 %0A''' %25 default)%0A
@@ -1239,32 +1239,33 @@
 %5C'%25(PASSWORD)s%5C'
+;
 %0AGRANT ALL PRIVI
@@ -1312,24 +1312,25 @@
 %25(CLIENT)s%5C'
+;
 %0A''' %25 tap)%0A
@@ -1465,16 +1465,17 @@
 WORD)s%5C'
+;
 %0AGRANT A
@@ -1506,11 +1506,11 @@
 IX)s
-%60
 %25%25
+%60
 .* t
@@ -1538,16 +1538,17 @@
 IENT)s%5C'
+;
 %0A''' %25 d
@@ -1771,16 +1771,17 @@
 IENT)s%5C'
+;
 ' %25 data
@@ -1888,8 +1888,497 @@
 int('')%0A
+%0A        print('''-- For testing, run the following commands on %5C'%25(HOST)s%5C':%0A%0AGRANT ALL PRIVILEGES ON %60test_%25(NAME)s%60.* to %5C'%25(USER)s%5C'@%5C'%25(CLIENT)s%5C';%0A''' %25 default)%0A%0A        print('''-- For testing, run the following commands on %5C'%25(HOST)s%5C':%0A%0AGRANT ALL PRIVILEGES ON %60test_%25(NAME)s%60.* to %5C'%25(USER)s%5C'@%5C'%25(CLIENT)s%5C';%0A''' %25 tap)%0A%0A        print('''-- For testing, run the following commands on %5C'%25(HOST)s%5C':%0A%0AGRANT ALL PRIVILEGES ON %60test_%60.* to %5C'%25(USER)s%5C'@%5C'%25(CLIENT)s%5C';%0A''' %25 data)%0A

fix sqlcreate script