from inference import *
from flask_cors import CORS
from flask import Flask, Response, request

app = Flask(__name__)
CORS(app)

@app.route('/risk', methods=['GET'])
def risk():
    risk_level, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr = risk_level_prediction()
    return Response(
        response=json.dumps({
                            "status": "success",
                            "HeartRate": HeartRateArr,
                            "BloodOxygenLevel": BloodOxygenLevelArr,
                            "BodyTemperature": BodyTemperatureArr,                            
                            "RiskLevel": f"{risk_level}"
                            }),
                        status=200, 
                        mimetype="application/json"
                        )

@app.route('/diwo', methods=['GET'])
def diwo():
    exercise_plan, diet_plan, HeartRateArr, BloodOxygenLevelArr, BodyTemperatureArr = diet_workout_prediction()
    return Response(
        response=json.dumps({   
                            "status": "success",
                            "HeartRate": HeartRateArr,
                            "BloodOxygenLevel": BloodOxygenLevelArr,
                            "BodyTemperature": BodyTemperatureArr,
                            "ExercisePlan": f"{exercise_plan}",
                            "DietPlan": f"{diet_plan}"
                            }),
                        status=200, 
                        mimetype="application/json"
                        )

@app.route('/pharmacy', methods=['POST'])
def pharmacy():
    '''
    {
        "user_location" : "7.09408649773395,79.99345377711329",
        "medicine_list" : "salbutamol,theophylline"
    }
    '''
    data = request.get_json()
    user_location = data['user_location']
    medicine_list = data['medicine_list']
    rec_list = pharmacy_recommendation(user_location, medicine_list)
    return Response(
                    response=json.dumps({
                                    "status": "success",
                                    "pharmacy_recommendation": rec_list
                                    }), 
                    status=200, 
                    mimetype="application/json"
                    )

if __name__ == "__main__": 
    app.run(
            debug=True, 
            host='0.0.0.0', 
            port=5000, 
            threaded=False, 
            use_reloader=True
            )