from flask import Flask, render_template, request, jsonify
from zxcvbn import zxcvbn

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/evaluate", methods=["POST"])
def evaluate():
    data = request.get_json(silent=True) or {}
    password = data.get("password", "")

    if not password:
        return jsonify({"score": -1, "crack_time": "", "warning": "", "suggestions": []})

    result = zxcvbn(password)
    crack_time = result["crack_times_display"]["offline_slow_hashing_1e4_per_second"]
    warning = result["feedback"]["warning"] or ""
    suggestions = result["feedback"]["suggestions"]

    return jsonify({
        "score": result["score"],          # 0-4
        "crack_time": crack_time,
        "warning": warning,
        "suggestions": suggestions,
    })


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