import os,  configparser,random,string, sys #,MySQLdb
from pathlib import Path
# reading config file
config = configparser.ConfigParser()

from MySQLdb import _mysql

BASE_DIR = Path(__file__).parent.parent.parent 
config.read(os.path.join(BASE_DIR, 'prod.env'))

def clear_migration_files():
    django_modules = [    # local applications
        'medic','consult',
        'baseapp',
        'diagnoses',
        'help',
        #'internal',
        ]    
    for mods in django_modules:
        migrate_folder = os.path.join(BASE_DIR,mods, 'migrations')
        for filename in os.listdir(migrate_folder):
            if filename != '__init__.py' and  filename != '__pycache__': 
                print(filename)
                os.remove(os.path.join(migrate_folder, filename))
def clear_sqllite():   
    if os.path.isfile(os.path.join(BASE_DIR, 'db.sqlite3')): os.remove(os.path.join(BASE_DIR, 'db.sqlite3'))
# def clear_migration_mysqldb():    # clear database tables 
#     db = MySQLdb.connect(config.get('dbdefault','HOST'),config.get('dbdefault','USER'),config.get('dbdefault','PASSWORD'))
#     cursor = db.cursor()# prepare a cursor object using cursor() method
#     cursor.execute("DROP DATABASE "+ config.get('dbdefault','NAME'))
#     cursor.execute("CREATE DATABASE "+ config.get('dbdefault','NAME'))
#     db.close()


def get_password(p_length =13):
    str_symbols = '!@#$%^&*()'
    str_pass = ''
    for counter  in range(1,p_length, +1):
       if  counter % 5 == 0 : 
            str_pass = str_pass + random.choice(string.ascii_letters)
       elif  counter % 3 == 0 : 
            str_pass = str_pass + random.choice(str_symbols)   
       else:
            str_pass = str_pass + random.choice(string.digits)
    return str_pass    

def get_random_character(p_length =13):
    str_pass = ''
    for counter  in range(1,p_length, +1):        str_pass = str_pass + random.choice(string.ascii_letters)
    return str_pass  

def get_random_number(p_length =13):
    str_pass = ''
    for counter  in range(1,p_length, +1):        str_pass = str_pass + random.choice(string.digits)
    return str_pass  



def get_random_list(p_length =[]):
    str_pass = ''
    for counter  in range(1,p_length, +1):        str_pass = str_pass + random.choice(string.digits)
    return str_pass  
#def clear_migration_SQLLite(): 
#clear_migration_files()
#clear_sqllite()
#clear_migration_mysqldb()

def test_print():
    print('test print') 
    return 'test pritt'

def help_docs():
    print('test')
    print('help')
    print('clearmigrationfiles')
    print('clearsqllite')

if str(sys.argv[1])== 'test':test_print()
if str(sys.argv[1])== 'clearmigrationfiles':clear_migration_files()
if str(sys.argv[1])== 'help':help_docs()
if str(sys.argv[1])== 'clearsqllite':clear_sqllite()



# print(BASE_DIR)
#list_selection = ['India', 'US', 'Africa']
#print(random.choice(list_selection)) # get random value from list 
# print(get_random_character(13))
# print(get_random_number(10))
