from dotenv import load_dotenv # load env variables
import argparse # pars user args
import pymongo # for connecting to the db
import bcrypt # to hash the password
import os # deal with the os

# laod env variables
load_dotenv()
DB_URI = os.getenv("DB_URI")

def hash_password(password):
    # Generate a salt and hash the password
    salt = bcrypt.gensalt()
    hashed = bcrypt.hashpw(password.encode('utf-8'), salt)
    return hashed.decode('utf-8')

def main():
    try:
        parser = argparse.ArgumentParser(description="A script to parse command-line arguments")
        parser.add_argument("-u", "--username", required=False, help="Username")
        parser.add_argument("-p", "--password", required=False, help="Password")

        # get args
        args = parser.parse_args()

        # connect to mongodb
        client = pymongo.MongoClient(DB_URI)

        # create the account
        db = client['Infinity']
        user_coll = db['users']

        user_coll.insert_one({
            # "username": args.username,
            # "password": hash_password(args.password)
            "username": os.getenv("ADMIN_USERNAME"),
            "password": hash_password(os.getenv("ADMIN_PASSWORD"))
        })

        print('Account created successfully')

    except Exception as e:
        print(e)
        print("\n \n please if this error wont be solved feel free to contact me")

if __name__ == "__main__":
    main()