import database, re, bcrypt, secrets, time

def new(username, password):
    uid = secrets.randbelow(99999999)
    created_at = int(time.time())

    passsalt = bcrypt.gensalt()
    passhash = bcrypt.hashpw(password.encode("utf8"), passsalt)

    database.execute("""INSERT INTO user
        (uid, created_at, username, password) VALUES
        (?, ?, ?, ?)""",
        (uid, created_at, username, passhash))

    return

def from_uid():
    pass

def id_from_username(username):
    query = database.fetch("SELECT id FROM user WHERE username=?", [username], False)
    if query:
        return query[0]
    else:
        return 0

def id_from_session(session):
    sessioninfo = database.fetch("""SELECT uid, expires
        FROM session
        WHERE value=?;""",
        [sessionid], False)

    if not sessioninfo:
        return 0

    # if int(sessioninfo[2] > int(time.time())):
    #     return 0 # session expired


    pass

def exists():
    # count from database
    pass

def authenticate(password):
    # verify password with stored hash
    pass

def get_info() -> dict:
    pass

def set_username(newname):
    # update username
    pass

def set_password(newpass):
    # update password
    pass

def delete():
    # delete database entry
    pass
