from flask import render_template, redirect, url_for, request
from flask_login import login_required

from splent_io.splent_feature_profile import profile_bp
from splent_io.splent_feature_auth.services import AuthenticationService
from splent_io.splent_feature_profile.forms import UserProfileForm
from splent_io.splent_feature_profile.services import UserProfileService


@profile_bp.route("/profile/edit", methods=["GET", "POST"])
@login_required
def edit_profile():
    auth_service = AuthenticationService()
    profile = auth_service.get_authenticated_user_profile()
    if not profile:
        return redirect(url_for("public.index"))

    form = UserProfileForm()
    if request.method == "POST":
        service = UserProfileService()
        result, errors = service.update_profile(profile.id, form)
        return service.handle_service_response(
            result,
            errors,
            "profile.edit_profile",
            "Profile updated successfully",
            "profile/edit.html",
            form,
        )

    return render_template("profile/edit.html", form=form, profile=profile)
