from flask import Flask, jsonify, request
from flask_jwt_extended import jwt_required, JWTManager
from werkzeug import serving

app = Flask(__name__)

# JWT Configuration
app.config['JWT_ALGORITHM'] = 'RS256'
app.config['JWT_PUBLIC_KEY'] = "YourPublicKeyHere"
jwt_flask = JWTManager(app)

# Dummy data for QoS, TSN, and Slicing management
qos_profiles = {
    "1": {"profileId": 1, "level": "high"},
    "2": {"profileId": 2, "level": "medium"}
}

tsn_profiles = {
    "profile1": {"name": "profile1", "config": "default"},
    "profile2": {"name": "profile2", "config": "custom"}
}

network_slices = {
    "slice1": {"sliceId": "slice1", "status": "active"},
    "slice2": {"sliceId": "slice2", "status": "inactive"}
}

### QoS Management Endpoints ###


@app.route("/<scsAsId>/qos", methods=["GET", "POST"])
@jwt_required()
def manage_qos(scsAsId):
    """Endpoint to manage QoS levels for network traffic"""
    if request.method == "GET":
        return jsonify({"message": "GET request for QoS management", "scsAsId": scsAsId})

    if request.method == "POST":
        data = request.get_json()
        return jsonify({"message": "POST request to manage QoS", "scsAsId": scsAsId, "data": data})


@app.route("/<scsAsId>/qos/<profileId>", methods=["GET", "PUT", "DELETE"])
@jwt_required()
def manage_single_qos_profile(scsAsId, profileId):
    """Endpoint to manage single QoS profile"""
    if request.method == "GET":
        profile = qos_profiles.get(profileId)
        if profile:
            return jsonify({"profile": profile, "scsAsId": scsAsId})
        return jsonify({"message": f"Profile {profileId} not found"}), 404

    if request.method == "PUT":
        data = request.get_json()
        qos_profiles[profileId] = data
        return jsonify({"message": f"Profile {profileId} updated", "scsAsId": scsAsId, "data": data})

    if request.method == "DELETE":
        if profileId in qos_profiles:
            del qos_profiles[profileId]
            return jsonify({"message": f"Profile {profileId} deleted"})
        return jsonify({"message": f"Profile {profileId} not found"}), 404

### TSN Management Endpoints ###


@app.route("/profile", methods=["GET"])
@jwt_required()
def list_tsn_profiles():
    """Endpoint for retrieving the list of available TSN profiles"""
    return jsonify({"profiles": tsn_profiles})


@app.route("/profile", methods=["GET"])
@jwt_required()
def tsn_profile_by_name():
    """Endpoint for retrieving information about a single TSN profile"""
    profile_name = request.args.get('name')
    profile = tsn_profiles.get(profile_name)
    if profile:
        return jsonify({"profile": profile})
    return jsonify({"message": f"Profile {profile_name} not found"}), 404


@app.route("/apply", methods=["POST"])
@jwt_required()
def apply_tsn_configuration():
    """Endpoint for configuring TSN connection parameters"""
    data = request.get_json()
    return jsonify({"message": "TSN configuration applied", "data": data})


@app.route("/clear", methods=["POST"])
@jwt_required()
def clear_tsn_configuration():
    """Endpoint for removing a previous TSN connection configuration"""
    return jsonify({"message": "TSN configuration cleared"})

### Network Slicing Management Endpoints ###


@app.route("/slice", methods=["GET", "POST"])
@jwt_required()
def manage_slices():
    """Endpoint for managing network slices"""
    if request.method == "GET":
        return jsonify({"slices": network_slices})

    if request.method == "POST":
        data = request.get_json()
        slice_id = f"slice{len(network_slices) + 1}"
        network_slices[slice_id] = data
        return jsonify({"message": "Network slice added", "sliceId": slice_id, "data": data})


@app.route("/slice/<sliceId>", methods=["GET", "PUT", "DELETE"])
@jwt_required()
def manage_single_slice(sliceId):
    """Endpoint for managing a single network slice"""
    if request.method == "GET":
        slice_ = network_slices.get(sliceId)
        if slice_:
            return jsonify({"slice": slice_})
        return jsonify({"message": f"Slice {sliceId} not found"}), 404

    if request.method == "PUT":
        data = request.get_json()
        network_slices[sliceId] = data
        return jsonify({"message": f"Slice {sliceId} updated", "data": data})

    if request.method == "DELETE":
        if sliceId in network_slices:
            del network_slices[sliceId]
            return jsonify({"message": f"Slice {sliceId} deleted"})
        return jsonify({"message": f"Slice {sliceId} not found"}), 404


# Starting the server
if __name__ == '__main__':
    serving.run_simple("0.0.0.0", 8888, app)
