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

app = Flask(__name__)

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

with open("/Users/IDB0128/Documents/OpenCapif/test_provider_certificate_folder/echeva_0/capif_cert_server.pem", "rb") as cert_file:
    cert = cert_file.read()

crtObj = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
pubKeyObject = crtObj.get_pubkey()
pubKeyString = crypto.dump_publickey(crypto.FILETYPE_PEM, pubKeyObject)

app.config['JWT_ALGORITHM'] = 'RS256'
app.config['JWT_PUBLIC_KEY'] = pubKeyString


# Dummy data for resilience management (In practice, this would connect to a database or service)
resilience_profiles = {
    "1": {"profileId": 1, "status": "active"},
    "2": {"profileId": 2, "status": "inactive"}
}


@app.route("/resilience", methods=["GET", "POST"])
@jwt_required()
def manage_resilience():
    """Endpoint to manage resilience functionalities in 6G networks"""
    if request.method == "GET":
        return jsonify({"message": "GET request for resilience management received."})

    if request.method == "POST":
        data = request.get_json()
        return jsonify({"message": "Resilience management POST request received", "data": data})


@app.route("/resilience/<profileId>", methods=["GET", "PUT", "DELETE"])
@jwt_required()
def manage_single_resilience(profileId):
    """Endpoint to manage a single resilience profile"""
    if request.method == "GET":
        profile = resilience_profiles.get(profileId, None)
        if profile:
            return jsonify({"profile": profile})
        else:
            return jsonify({"message": f"Profile {profileId} not found"}), 404

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

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


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

    if request.method == "POST":
        data = request.get_json()
        return jsonify({"message": "POST request for slice resilience management received", "data": data})


@app.route("/slice/<sliceId>/resilience", methods=["GET", "PUT", "DELETE"])
@jwt_required()
def manage_single_slice_resilience(sliceId):
    """Endpoint for managing a single slice's resilience"""
    if request.method == "GET":
        return jsonify({"message": f"GET request for slice {sliceId} resilience received"})

    if request.method == "PUT":
        data = request.get_json()
        return jsonify({"message": f"PUT request to update slice {sliceId}'s resilience", "data": data})

    if request.method == "DELETE":
        return jsonify({"message": f"DELETE request to remove slice {sliceId}'s resilience"})


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