from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/", methods=["GET","POST"])
def index():

    result=None

    if request.method == "POST":

        age = int(request.form["age"])
        gender = request.form["gender"]
        height_cm = float(request.form["height"])
        weight = float(request.form["weight"])

        height_m = height_cm / 100

        # BMI
        bmi = weight / (height_m ** 2)

        # Category
        if bmi < 18.5:
            category = "Underweight"
        elif bmi < 25:
            category = "Normal"
        elif bmi < 30:
            category = "Overweight"
        else:
            category = "Obese"

        # Healthy weight range
        min_weight = 18.5 * (height_m ** 2)
        max_weight = 25 * (height_m ** 2)

        # BMI Prime
        bmi_prime = bmi / 25

        # Ponderal Index
        ponderal = weight / (height_m ** 3)

        result = {
            "bmi": round(bmi,1),
            "category": category,
            "min_weight": round(min_weight,1),
            "max_weight": round(max_weight,1),
            "bmi_prime": round(bmi_prime,2),
            "ponderal": round(ponderal,2)
        }

    return render_template("index.html", result=result)

if __name__ == "__main__":
    app.run(debug=True)