from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/score', methods=['GET'])
def score():
    val1 = request.args.get('val1', default = 1, type = int)    #get ละไม่เจอ ค่า val1 จะ set default เป็น 1
    val2 = request.args.get('val2', default = 0.5, type = float)
     
    return f"val1 is {val1} , val2 is {val2}"

@app.route('/user', methods=['POST']) 
def user():
    data = request.json     #เหมือน dict ใน python(มี key, value)
    return jsonify(data)

@app.route('/bmi', methods=['GET'])
def bmi():
    weight = request.args.get('weight', default = 0, type = float)
    height = request.args.get('height', default = 0, type = float)
    bmi = round((weight / ((height / 100) ** 2)), 2)   #2 ตัวสุดท้ายหมายถึง round กี่ ตน.
    return f"For an adult with height of {height} cm and weight of {height} \n bmi is {bmi}"    #ควรใส่ render_template

@app.route('/bmi2', methods=['POST'])
def bmi2():
    data = request.json
    weight = data['weight']
    height = data['height']

    bmi = round((weight / ((height / 100) ** 2)), 2)

    return_data = {"weight:": weight, "height": height, "bmi": bmi}

    return jsonify(return_data)  #ตอบกลับเป็น JSON