from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import calling_nps
import phone_filter

app = Flask(__name__)
# database setup and start
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///phone.db'
db = SQLAlchemy(app)


@app.route('/questions', methods=['GET', 'POST'])
def get_answers():
    """ Function for communication with frontend, uses required answers for consultation, will get criteria for filter
        and sends filtered phones back 
    funkce pro komunikaci s frontendem se ziskanymi odpovedmi provede konzultaci, ziska kriteria pro 
    pro filtr a vyfiltrovane telefony posle zpet

    Returns:
        [json]: [filtered phones]
    """
    answers = request.get_json()
    # print("--------------------------------------------------------")
    #print("novy pokus")
    # print(100*'-')
    criteria = calling_nps.get_hypothesis(answers)
    print(criteria)
    phones = phone_filter.filter_phones(criteria)
    return jsonify(phones)


# @app.route('/results', methods=['GET'])
# def get_phones():
#    phones = phone_filter.filter_phones(criteria)
#    return jsonify(phones)


if __name__ == '__main__':
    from database import Phone
    app.run(debug=True)
