import boto3
import hashlib
import binascii
import json

DB_CLUSTER = "database320"
DB_NAME = "db320"
MASTER_USERNAME = "admin320"
MASTER_PASSWORD = "pwFor320"
ARN = "arn:aws:rds:us-east-2:007372221023:cluster:database320"
SECRET_ARN = "arn:aws:secretsmanager:us-east-2:007372221023:secret:rds-db-credentials/cluster-BZEL6PSDLGVBVJB6BIDZGZQ4MI/admin320-fsoCse"
SALT = b'89a9880855f7adf8eb780f800c2f756dd31f6f3dd901622e755f9432e93da028'

rds = boto3.client('rds-data')

def query_db(query_string):
    print("Hello from layer")
    result = rds.execute_statement(
        secretArn = SECRET_ARN,
        database = DB_NAME,
        resourceArn = ARN,
        sql = query_string,
    )
    return result

def hash_password(password):
    salt = SALT
    pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 100000)
    pwdhash = binascii.hexlify(pwdhash)
    return (salt + pwdhash).decode('ascii')

def respond(err=None, res="{}", statusCode=None):
    if (err is not None):
        err = {'message': err}
    return {
        'statusCode': statusCode if statusCode else '200',
        'body': json.dumps(err) if err else json.dumps(res),
        'headers': {
            'Content-Type': 'application/json',
            'X-Content-Type-Options': 'nosniff',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': '*',
            'Access-Control-Allow-Methods': '*'
            
        },
    }