from django.core.management.base import BaseCommand
import os
import time

class Command(BaseCommand):
    help = 'Run MariaDB Container and login into mysql prompt'
    
    def start_mariadb(self):
        try:
            return os.system('docker start mariadb10_3_39')
        except Exception as e:
            return self.stdout.write(self.style.ERROR('Error while starting MariaDB Container'))
        
    def login_mariadb(self):
        try:
            return os.system('mysql -h 172.17.0.2 -P 3307 --protocol=TCP -u root -pX170710y@')
        except Exception as e:
            return self.stdout.write(self.style.ERROR('Error while logging into mysql prompt'))

    def handle(self, *args, **options):
        try:
            self.start_mariadb()
            time.sleep(5)
            self.login_mariadb()
            return self.stdout.write(self.style.SUCCESS('Successfully started MariaDB Container and logged into mysql prompt'))
        except Exception as e:
            return self.stdout.write(self.style.ERROR('Error while starting MariaDB Container and logging into mysql prompt'))