Proyectos de Subversion LeadersLinked - Services

Rev

Rev 622 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

/**
 * 
 * Controlador: Mis Perfiles 
 * 
 */

declare(strict_types=1);

namespace LeadersLinked\Controller;

use Laminas\Db\Adapter\AdapterInterface;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;

use LeadersLinked\Form\UserProfile\SkillForm;
use LeadersLinked\Form\UserProfile\LanguageForm;

use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\UserProfileMapper;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Model\UserProfile;
use LeadersLinked\Mapper\CompanyFollowerMapper;
use LeadersLinked\Mapper\LocationMapper;
use LeadersLinked\Model\UserLanguage;
use LeadersLinked\Mapper\UserLanguageMapper;
use LeadersLinked\Mapper\UserSkillMapper;

use LeadersLinked\Model\UserSkill;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Form\UserProfile\ExtendedForm;
use LeadersLinked\Form\UserProfile\LocationForm;
use LeadersLinked\Model\Location;
use LeadersLinked\Form\UserProfile\SocialNetworkForm;
use LeadersLinked\Form\UserProfile\EducationForm;
use LeadersLinked\Model\UserEducation;
use LeadersLinked\Mapper\UserEducationMapper;
use LeadersLinked\Mapper\DegreeMapper;
use LeadersLinked\Form\UserProfile\ExperienceForm;
use LeadersLinked\Mapper\AptitudeMapper;
use LeadersLinked\Mapper\LanguageMapper;
use LeadersLinked\Mapper\UserAptitudeMapper;
use LeadersLinked\Mapper\UserExperienceMapper;
use LeadersLinked\Mapper\IndustryMapper;
use LeadersLinked\Mapper\CompanySizeMapper;
use LeadersLinked\Model\UserExperience;
use LeadersLinked\Mapper\ConnectionMapper;
use LeadersLinked\Form\UserProfile\ImageForm;
use LeadersLinked\Form\UserProfile\CoverForm;
use LeadersLinked\Mapper\SkillMapper;
use LeadersLinked\Form\MyProfiles\CreateForm;
use LeadersLinked\Form\UserProfile\AptitudeForm;
use LeadersLinked\Model\UserAptitude;
use LeadersLinked\Form\UserProfile\HobbyAndInterestForm;
use LeadersLinked\Mapper\HobbyAndInterestMapper;
use LeadersLinked\Mapper\UserHobbyAndInterestMapper;
use LeadersLinked\Model\UserHobbyAndInterest;
use LeadersLinked\Library\Storage;


class MyProfilesController extends AbstractActionController
{
    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;

    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;


    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;

    /**
     *
     * @var array
     */
    private $config;


    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;


    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        $this->translator   = $translator;
    }

    /**
     * 
     * Generación del listado de perfiles
     * {@inheritDoc}
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
     */
    public function indexAction()
    {
        // Obtener el usuario actual del sistema
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        // Obtener la petición HTTP
        $request = $this->getRequest();
        if ($request->isGet()) {
            // Sanitizar el parámetro de búsqueda si existe
            $search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));

            // Obtener el control de acceso (ACL) para verificar permisos
            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
            // Verificar permisos para diferentes acciones
            $allowView = $acl->isAllowed($currentUser->usertype_id, 'profile/view');
            $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'profile/my-profiles/edit');
            $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'profile/my-profiles/delete');

            // Inicializar el sistema de almacenamiento para manejar imágenes
            $storage = Storage::getInstance($this->config, $this->adapter);

            // Obtener el mapper de perfiles de usuario
            $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
            // Obtener todos los perfiles del usuario actual, opcionalmente filtrados por búsqueda
            $records  = $userProfileMapper->fetchAllByUserIdAndSearch($currentUser->id, $search);

            // Preparar el array de items para la respuesta
            $items = [];
            foreach ($records as $record) {
                // Construir cada item con la información del perfil
                $item = [
                    'id' => $record->id,
                    'name' => $record->name,
                    'image' => $storage->getUserProfileImage($currentUser, $record),
                    'link_view' => $allowView ? $this->url()->fromRoute('profile/view', ['id' => $record->uuid])  : '',
                    'link_edit' => $allowEdit ? $this->url()->fromRoute('profile/my-profiles/edit', ['id' => $record->uuid])  : '',
                    'link_delete' => $allowDelete && $record->public == UserProfile::PUBLIC_NO ? $this->url()->fromRoute('profile/my-profiles/delete', ['id' => $record->uuid]) : '',
                ];

                array_push($items, $item);
            }

            // Preparar la respuesta exitosa con los items
            $response = [
                'success' => true,
                'data' => $items
            ];

            return new JsonModel($response);
        } else {
            // Si no es una petición GET, devolver error
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);
        }
    }



    /**
     * 
     * Agregar un nuevo perfil
     * @return \Laminas\View\Model\JsonModel
     */
    public function addAction()
    {
        $request = $this->getRequest();


        if ($request->isPost()) {
            $form = new  CreateForm();
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $dataPost = (array) $form->getData();

                $hydrator = new ObjectPropertyHydrator();
                $userProfile = new UserProfile();
                $hydrator->hydrate($dataPost, $userProfile);

                $currentUserPlugin = $this->plugin('currentUserPlugin');
                $currentUser = $currentUserPlugin->getUser();

                $userProfile->uuid = Functions::genUUID();
                $userProfile->user_id = $currentUser->id;
                $userProfile->public = \LeadersLinked\Model\UserProfile::PUBLIC_NO;

                $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
                $result = $userProfileMapper->insert($userProfile);

                if ($result) {
                    $this->logger->info('Se agrego el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                    $data = [
                        'success'   => true,
                        'data'   => 'LABEL_RECORD_ADDED'
                    ];
                } else {
                    $data = [
                        'success'   => false,
                        'data'      => $userProfile->getError()
                    ];
                }

                return new JsonModel($data);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {

                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }
        } else {
            $data = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }

    /**
     * 
     * Borrar un perfil excepto el público
     * @return \Laminas\View\Model\JsonModel
     */
    public function deleteAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $request = $this->getRequest();
        $id = $this->params()->fromRoute('id');

        if (!$id) {
            // Use helper for consistency
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
        }

        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
        $userProfile = $userProfileMapper->fetchOneByUuid($id);
        if (!$userProfile) {
            // Use helper for consistency
            return $this->_createSimpleErrorResponse('ERROR_RECORD_NOT_FOUND');
        }

        // Authorize: Check if current user owns the profile
        if ($currentUser->id != $userProfile->user_id) {
            // Use helper for consistency
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
        }

        // Prevent deletion of the public profile
        if ($userProfile->public == UserProfile::PUBLIC_YES) {
            // Use helper for consistency
            return $this->_createSimpleErrorResponse('ERROR_PUBLIC_PROFILE');
        }

        if ($request->isPost()) {
            $storage = Storage::getInstance($this->config, $this->adapter);
            $target_path = $storage->getPathUser();
            $user_uuid = $currentUser->uuid; // Assuming user UUID matches profile owner's UUID for path

            // Attempt to delete associated files first
            $image_deleted = true;
            if ($userProfile->image) {
                $image_deleted = $storage->deleteFile($target_path, $user_uuid, $userProfile->image);
                if (!$image_deleted) {
                    $this->logger->err("Failed to delete profile image file: {$target_path}/{$user_uuid}/{$userProfile->image}", ['user_id' => $userProfile->user_id]);
                    // Decide if this is a critical error. Proceeding with DB deletion for now.
                }
            }
            $cover_deleted = true;
            if ($userProfile->cover) {
                $cover_deleted = $storage->deleteFile($target_path, $user_uuid, $userProfile->cover);
                if (!$cover_deleted) {
                    $this->logger->err("Failed to delete profile cover file: {$target_path}/{$user_uuid}/{$userProfile->cover}", ['user_id' => $userProfile->user_id]);
                    // Decide if this is a critical error. Proceeding with DB deletion for now.
                }
            }

            // Delete the database record
            $result = $userProfileMapper->delete($userProfile);
            if ($result) {
                $this->logger->info('Se borro el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                // Success response
                $data = [
                    'success' => true,
                    'data' => 'LABEL_RECORD_DELETED'
                ];
            } else {
                // Use helper for DB error
                $data = $this->_createSimpleErrorResponse($userProfileMapper->getError() ?: 'ERROR_THERE_WAS_AN_ERROR');
            }
        } else {
            // Use helper for method error
            $data = $this->_createSimpleErrorResponse('ERROR_METHOD_NOT_ALLOWED');
        }

        // Return JsonModel for all paths
        return $data instanceof JsonModel ? $data : new JsonModel($data);
    }

    /**
     * Presenta el perfil con las opciones de edición de cada sección.
     * Este método:
     * - Obtiene y valida el perfil del usuario
     * - Recopila toda la información relacionada (educación, experiencia, habilidades, etc.)
     * - Prepara los datos para la vista de edición
     * - Genera las URLs necesarias para las diferentes acciones de edición
     * 
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
     */
    public function editAction()
    {
        // Obtener el usuario actual del sistema
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        // Obtener el plugin para mensajes flash
        $flashMessenger = $this->plugin('FlashMessenger');

        // Obtener parámetros de la petición
        $request = $this->getRequest();
        $id = $this->params()->fromRoute('id');

        // Validar que se proporcionó un ID
        if (!$id) {
            $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
            return $this->redirect()->toRoute('dashboard');
        }

        // Obtener el perfil de usuario correspondiente
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
        $userProfile = $userProfileMapper->fetchOneByUuid($id);

        // Validar que el perfil existe
        if (!$userProfile) {
            $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
            return $this->redirect()->toRoute('dashboard');
        }

        // Verificar que el usuario actual es el propietario del perfil
        if ($currentUser->id != $userProfile->user_id) {
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
            return $this->redirect()->toRoute('dashboard');
        }

        // Determinar la clave de API de Google Maps según el entorno
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
        if ($sandbox) {
            $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
        } else {
            $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
        }

        // Procesar solo peticiones GET
        if ($request->isGet()) {
            // Obtener información de ubicación si existe
            if ($userProfile->location_id) {
                $locationMapper = LocationMapper::getInstance($this->adapter);
                $location = $locationMapper->fetchOne($userProfile->location_id);

                $formattedAddress = $location->formatted_address;
                $country = $location->country;
            } else {
                $formattedAddress = '';
                $country = '';
            }

            // Obtener información del usuario
            $userMapper = UserMapper::getInstance($this->adapter);
            $user = $userMapper->fetchOne($userProfile->user_id);

            // Obtener idiomas del usuario
            $userLanguages = [];
            $languageMapper = LanguageMapper::getInstance($this->adapter);
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
            $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
            foreach ($records as $record) {
                $language = $languageMapper->fetchOne($record->language_id);
                $userLanguages[$language->id] = $language->name;
            }

            // Obtener educación del usuario
            $locationMapper = LocationMapper::getInstance($this->adapter);
            $degreeMapper = DegreeMapper::getInstance($this->adapter);
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
            $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);

            // Formatear información de educación
            foreach ($userEducations  as &$userEducation) {
                $location = $locationMapper->fetchOne($userEducation->location_id);
                $degree = $degreeMapper->fetchOne($userEducation->degree_id);

                $userEducation = [
                    'university' => $userEducation->university,
                    'degree' => $degree->name,
                    'field_of_study' => $userEducation->field_of_study,
                    'grade_or_percentage' => $userEducation->grade_or_percentage,
                    'formatted_address' => $location->formatted_address,
                    'from_year' => $userEducation->from_year,
                    'to_year' => $userEducation->to_year,
                    'description' => $userEducation->description,
                    'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid]),
                    'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid]),
                ];
            }

            // Obtener experiencia laboral del usuario
            $industryMapper = IndustryMapper::getInstance($this->adapter);
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
            $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);

            // Formatear información de experiencia laboral
            foreach ($userExperiences  as &$userExperience) {
                $location = $locationMapper->fetchOne($userExperience->location_id);
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
                $industry = $industryMapper->fetchOne($userExperience->industry_id);

                $userExperience = [
                    'company' => $userExperience->company,
                    'industry' => $industry->name,
                    'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ')',
                    'title' => $userExperience->title,
                    'formatted_address' => $location->formatted_address,
                    'from_year' => $userExperience->from_year,
                    'from_month' => $userExperience->from_month,
                    'to_year' => $userExperience->to_year,
                    'to_month' => $userExperience->to_month,
                    'description' => $userExperience->description,
                    'is_current' => $userExperience->is_current,
                    'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid]),
                    'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid])
                ];
            }

            // Obtener aptitudes del usuario
            $userAptitudes = [];
            $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
            $records  = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
            foreach ($records as $record) {
                $aptitude = $aptitudeMapper->fetchOne($record->aptitude_id);
                if ($aptitude) {
                    $userAptitudes[$aptitude->uuid] = $aptitude->name;
                }
            }

            // Obtener hobbies e intereses del usuario
            $userHobbiesAndInterests = [];
            $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
            $records  = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
            foreach ($records as $record) {
                $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($record->hobby_and_interest_id);
                if ($hobbyAndInterest) {
                    $userHobbiesAndInterests[$hobbyAndInterest->uuid] = $hobbyAndInterest->name;
                }
            }

            // Obtener habilidades del usuario
            $userSkills = [];
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
            $skillMapper = SkillMapper::getInstance($this->adapter);
            $records  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
            foreach ($records as $record) {
                $skill = $skillMapper->fetchOne($record->skill_id);
                $userSkills[$skill->uuid] = $skill->name;
            }

            // Obtener estadísticas de conexiones
            $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
            $following = $companyFollowerMapper->getCountFollowing($user->id);

            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
            $follower = $connectionMapper->fetchTotalConnectionByUser($user->id);

            // Obtener configuraciones de tamaño de imágenes
            $image_size_cover = $this->config['leaderslinked.image_sizes.user_cover_upload'];
            $image_size_profile = $this->config['leaderslinked.image_sizes.user_upload'];

            // Inicializar el sistema de almacenamiento
            $storage = Storage::getInstance($this->config, $this->adapter);

            // Preparar datos para la respuesta
            $data = [
                'following'         => $following,
                'follower'          => $follower,
                'user_id'           => $user->id,
                'user_uuid'         => $user->uuid,
                'full_name'         => trim($user->first_name . ' ' . $user->last_name),
                'user_profile_id'   => $userProfile->id,
                'user_profile_uuid' => $userProfile->uuid,
                'image'             => $storage->getUserProfileImage($currentUser, $userProfile),
                'cover'             => $storage->getUserProfileCover($currentUser, $userProfile),
                'overview'          => $userProfile->description,
                'facebook'          => $userProfile->facebook,
                'instagram'         => $userProfile->instagram,
                'twitter'           => $userProfile->twitter,
                'formatted_address' => $formattedAddress,
                'country'           => $country,
                'user_skills'       => $userSkills,
                'user_languages'    => $userLanguages,
                'user_educations'   => $userEducations,
                'user_experiences'  => $userExperiences,
                'user_aptitudes'                => $userAptitudes,
                'user_hobbies_and_interests'    => $userHobbiesAndInterests,
                'image_size_cover' =>  $image_size_cover,
                'image_size_profile' => $image_size_profile,

                // Generar URLs para las diferentes acciones de edición
                'link_extended' => $this->url()->fromRoute('profile/my-profiles/extended', ['id' => $userProfile->uuid]),
                'link_image_upload' => $this->url()->fromRoute('profile/my-profiles/image', ['id' => $userProfile->uuid, 'operation' => 'upload']),
                'link_image_delete' => $this->url()->fromRoute('profile/my-profiles/image', ['id' => $userProfile->uuid, 'operation' => 'delete']),
                'link_cover_upload' => $this->url()->fromRoute('profile/my-profiles/cover', ['id' => $userProfile->uuid, 'operation' => 'upload']),
                'link_cover_delete' => $this->url()->fromRoute('profile/my-profiles/cover', ['id' => $userProfile->uuid, 'operation' => 'delete']),
                'link_experience_add' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'add']),
                'link_education_add' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'add']),
                'link_language' => $this->url()->fromRoute('profile/my-profiles/language', ['id' => $userProfile->uuid]),
                'link_location' => $this->url()->fromRoute('profile/my-profiles/location', ['id' => $userProfile->uuid]),
                'link_skill' => $this->url()->fromRoute('profile/my-profiles/skill', ['id' => $userProfile->uuid]),
                'link_social_network' => $this->url()->fromRoute('profile/my-profiles/social-network', ['id' => $userProfile->uuid]),
                'link_aptitude' => $this->url()->fromRoute('profile/my-profiles/aptitude', ['id' => $userProfile->uuid]),
                'link_hobby_and_interest' => $this->url()->fromRoute('profile/my-profiles/hobby-and-interest', ['id' => $userProfile->uuid]),
            ];

            return new JsonModel($data);
        } else {
            // Si no es una petición GET, devolver error
            $data = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }

    /**
     * Actualización de las habilidades
     * @return \Laminas\View\Model\JsonModel
     */
    public function skillAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isGet()) {
            $skillMapper = SkillMapper::getInstance($this->adapter);


            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
            $userSkills  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);

            $items = [];
            foreach ($userSkills as $userSkill) {
                $skill = $skillMapper->fetchOne($userSkill->skill_id);
                array_push($items, $skill->uuid);
            }

            $data = [
                'success' => true,
                'data' => $items
            ];

            return new JsonModel($data);
        } else if ($request->isPost()) {

            $form = new SkillForm($this->adapter);
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $this->logger->info('Se actualizaron las habilidades del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $skillMapper = SkillMapper::getInstance($this->adapter);


                $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
                $userSkillMapper->deleteByUserProfileId($userProfile->id);

                $dataPost = (array) $form->getData();
                $skills = $dataPost['skills'];
                foreach ($skills as $skill_uuid) {

                    $skill = $skillMapper->fetchOneByUuid($skill_uuid);



                    $userSkill = new UserSkill();
                    $userSkill->user_id = $userProfile->user_id;
                    $userSkill->user_profile_id = $userProfile->id;
                    $userSkill->skill_id =  $skill->id;

                    $userSkillMapper->insert($userSkill);
                }

                $items = [];

                $records = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
                foreach ($records as $record) {
                    $skill = $skillMapper->fetchOne($record->skill_id);
                    array_push($items,  ['value' => $skill->uuid, 'label' => $skill->name]);
                }

                return new JsonModel([
                    'success'   => true,
                    'data'   => $items
                ]);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de los idiomas
     * 
     * Esta acción maneja la lógica para obtener y actualizar los idiomas asociados
     * a un perfil de usuario específico.
     * - En una petición GET, devuelve la lista actual de IDs de idiomas del perfil.
     * - En una petición POST, valida los datos del formulario, actualiza los idiomas
     *   (borrando los antiguos y añadiendo los nuevos) y devuelve la nueva lista
     *   de idiomas con su ID y nombre.
     * 
     * @return \Laminas\View\Model\JsonModel Retorna un modelo JSON con el resultado de la operación.
     *         - success (bool): Indica si la operación fue exitosa.
     *         - data (array|string): 
     *           - En GET exitoso: Array de IDs de idiomas.
     *           - En POST exitoso: Array de objetos idioma (con 'value' => id, 'label' => nombre).
     *           - En error: Mensaje de error o array de mensajes de validación del formulario.
     */
    public function languageAction()
    {
        // Obtener el usuario actualmente autenticado
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        // Obtener el ID del perfil de usuario desde los parámetros de la ruta
        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        // Buscar el perfil de usuario por su UUID
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        // Si el perfil no se encuentra, devolver error
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        // Verificar que el usuario actual es el propietario del perfil
        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }


        // Obtener la petición HTTP actual
        $request = $this->getRequest();
        // Procesar si la petición es GET
        if ($request->isGet()) {
            // Registrar la acción (aunque es un GET, se registra como actualización, revisar si es intencional)
            $this->logger->info('Se actualizaron los idiomas del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

            // Obtener los idiomas asociados al perfil
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
            $languages  = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);

            // Preparar un array con los IDs de los idiomas
            $items = [];
            foreach ($languages as $language) {
                array_push($items, $language->language_id);
            }

            $data = [
                'success' => true,
                'data' => $items
            ];

            return new JsonModel($data);
            // Procesar si la petición es POST
        } else if ($request->isPost()) {
            // Instanciar el formulario de idiomas
            $form = new LanguageForm($this->adapter);
            $dataPost = $request->getPost()->toArray();

            // Establecer los datos del POST en el formulario
            $form->setData($dataPost);

            // Validar el formulario
            if ($form->isValid()) {
                // Mappers para idiomas y la relación usuario-idioma
                $languageMapper = LanguageMapper::getInstance($this->adapter);
                $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
                // Eliminar todos los idiomas previamente asociados al perfil
                $userLanguageMapper->deleteByUserProfileId($userProfile->id);

                // Obtener los datos validados del formulario
                $dataPost = (array) $form->getData();
                $languages = $dataPost['languages'];
                // Iterar sobre los IDs de idiomas enviados
                foreach ($languages as $language_id) {
                    $language = $languageMapper->fetchOne($language_id); // Obtener el objeto idioma completo

                    // Crear y configurar la nueva entidad de relación usuario-idioma
                    $userLanguage = new UserLanguage();
                    $userLanguage->user_id = $userProfile->user_id;
                    $userLanguage->user_profile_id = $userProfile->id;
                    $userLanguage->language_id = $language->id;

                    // Insertar el nuevo registro de idioma para el perfil
                    $userLanguageMapper->insert($userLanguage);
                }

                // Preparar la respuesta con la lista actualizada de idiomas (ID y nombre)
                $items = [];
                $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
                foreach ($records as $record) {
                    $language = $languageMapper->fetchOne($record->language_id);
                    array_push($items,  ['value' => $language->id, 'label' => $language->name]);
                }

                return new JsonModel([
                    'success'   => true,
                    'data'   => $items
                ]);
            } else {
                // Si el formulario no es válido, obtener y devolver los mensajes de error
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de la descripción y cualquier otro campo extendido del perfil a futuro
     * @return \Laminas\View\Model\JsonModel
     */
    public function extendedAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();


        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isGet()) {
            $data = [
                'success' => true,
                'data' => [
                    'description' => $userProfile->description,
                ]
            ];

            return new JsonModel($data);
        } else if ($request->isPost()) {


            $form = new ExtendedForm();
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $dataPost = (array) $form->getData();

                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $userProfile);

                $userProfileMapper->updateExtended($userProfile);

                $this->logger->info('Se actualizo las descripción del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                return new JsonModel([
                    'success'   => true,
                    'data' => [
                        'description' => $userProfile->description,
                    ]
                ]);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de la ubucación
     * @return \Laminas\View\Model\JsonModel
     */
    public function locationAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }


        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isPost()) {

            $form = new LocationForm();
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $this->logger->info('Se actualizaron la ubicación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $dataPost = (array) $form->getData();

                $location = new Location();
                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $location);

                $location->id = $userProfile->location_id ? $userProfile->location_id : null;



                $locationMapper = LocationMapper::getInstance($this->adapter);
                if ($userProfile->location_id) {
                    $result = $locationMapper->update($location);
                } else {
                    $result = $locationMapper->insert($location);

                    if ($result) {
                        $userProfile->location_id = $location->id;
                        $userProfileMapper->updateLocation($userProfile);
                    }
                }

                if ($result) {
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
                        $currentUser->location_id = $location->id;

                        $userMapper = UserMapper::getInstance($this->adapter);
                        $userMapper->updateLocation($currentUser);
                    }


                    $response = [
                        'success'   => true,
                        'data' => [
                            'formatted_address' => $location->formatted_address,
                            'country' => $location->country,
                        ]
                    ];
                } else {
                    $response = [
                        'success'   => false,
                        'data' => 'ERROR_THERE_WAS_AN_ERROR'
                    ];
                }



                return new JsonModel($response);
            } else {
                return new JsonModel([
                    'success'   => false,
                    'data'   =>   'ERROR_PLACED_AUTOCOMPLETE_DOES_NOT_CONTAIN_GEOMETRY'
                ]);
            }
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de las redes sociales
     * @return \Laminas\View\Model\JsonModel
     */
    public function socialNetworkAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }


        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isGet()) {
            $data = [
                'success' => true,
                'data' => [
                    'facebook' => $userProfile->facebook,
                    'instagram' => $userProfile->instagram,
                    'twitter' => $userProfile->twitter
                ]
            ];

            return new JsonModel($data);
        } else if ($request->isPost()) {

            $form = new SocialNetworkForm();
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $this->logger->info('Se actualizaron las redes sociales del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $dataPost = (array) $form->getData();

                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $userProfile);

                $userProfileMapper->updateSocialNetwork($userProfile);
                return new JsonModel([
                    'success'   => true,
                    'data' => [
                        'facebook' => $userProfile->facebook,
                        'instagram' => $userProfile->instagram,
                        'twitter' => $userProfile->twitter
                    ]
                ]);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }




    /**
     * Actualización de los registros de estudios realizados
     * @return \Laminas\View\Model\JsonModel
     */
    public function  educationAction()
    {

        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id    = $this->params()->fromRoute('id');
        $user_education_id  = $this->params()->fromRoute('user_education_id');
        $operation          = $this->params()->fromRoute('operation');

        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }


        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isPost()) {
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);

            if ($operation == 'delete' || $operation == 'edit') {
                $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);


                if (!$userEducation) {

                    $response = [
                        'success' => false,
                        'data' => 'ERROR_RECORD_NOT_FOUND'
                    ];

                    return new JsonModel($response);
                } else if ($userProfile->id != $userEducation->user_profile_id) {
                    $response = [
                        'success' => false,
                        'data' => 'ERROR_UNAUTHORIZED'
                    ];

                    return new JsonModel($response);
                }
            } else {
                $userEducation = null;
            }

            $locationMapper = LocationMapper::getInstance($this->adapter);
            if ($operation == 'delete') {
                $this->logger->info('Se borro un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $result = $userEducationMapper->delete($userEducation);
                if ($result) {
                    $locationMapper->delete($userEducation->location_id);
                }
            } else {


                $form = new EducationForm($this->adapter);
                $dataPost = $request->getPost()->toArray();

                $form->setData($dataPost);

                if ($form->isValid()) {

                    if (!$userEducation) {
                        $userEducation = new UserEducation();
                        $userEducation->user_id = $userProfile->user_id;
                        $userEducation->user_profile_id = $userProfile->id;
                    }

                    $dataPost = (array) $form->getData();

                    $hydrator = new ObjectPropertyHydrator();
                    $hydrator->hydrate($dataPost, $userEducation);

                    $degreeMapper = DegreeMapper::getInstance($this->adapter);
                    $degree = $degreeMapper->fetchOneByUuid($dataPost['degree_id']);
                    $userEducation->degree_id = $degree->id;




                    if ($userEducation->location_id) {
                        $location = $locationMapper->fetchOne($userEducation->location_id);
                    } else {
                        $location = new Location();
                    }

                    $hydrator->hydrate($dataPost, $location);
                    if ($userEducation->location_id) {
                        $this->logger->info('Se actualizo un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                        $result = $locationMapper->update($location);
                    } else {
                        $this->logger->info('Se agrego un registro de educación del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                        $result = $locationMapper->insert($location);

                        if ($result) {
                            $userEducation->location_id = $location->id;
                        }
                    }
                    if ($result) {
                        if ($userEducation->id) {
                            $result = $userEducationMapper->update($userEducation);
                        } else {
                            $result =  $userEducationMapper->insert($userEducation);
                        }
                    }
                } else {
                    $messages = [];
                    $form_messages = (array) $form->getMessages();
                    foreach ($form_messages  as $fieldname => $field_messages) {
                        $messages[$fieldname] = array_values($field_messages);
                    }

                    return new JsonModel([
                        'success'   => false,
                        'data'   => $messages
                    ]);
                }
            }

            if ($result) {
                $degreeMapper = DegreeMapper::getInstance($this->adapter);
                $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);

                foreach ($userEducations  as &$userEducation) {
                    $location = $locationMapper->fetchOne($userEducation->location_id);
                    $degree = $degreeMapper->fetchOne($userEducation->degree_id);

                    $userEducation = [
                        'university' => $userEducation->university,
                        'degree' => $degree->name,
                        'field_of_study' => $userEducation->field_of_study,
                        'grade_or_percentage' => $userEducation->grade_or_percentage,
                        'formatted_address' => $location->formatted_address,
                        'from_year' => $userEducation->from_year,
                        'to_year' => $userEducation->to_year,
                        'description' => $userEducation->description,
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_education_id' => $userEducation->uuid]),
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/education', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_education_id' => $userEducation->uuid]),
                    ];
                }

                $response = [
                    'success'   => true,
                    'data' => $userEducations
                ];
            } else {
                $response = [
                    'success'   => false,
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
                ];
            }



            return new JsonModel($response);
        } else if ($request->isGet() && $operation == 'edit') {
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
            $userEducation = $userEducationMapper->fetchOneByUuid($user_education_id);

            if (!$userEducation) {

                $response = [
                    'success' => false,
                    'data' => 'ERROR_RECORD_NOT_FOUND'
                ];
            } else if ($userProfile->id != $userEducation->user_profile_id) {
                $response = [
                    'success' => false,
                    'data' => 'ERROR_UNAUTHORIZED'
                ];
            } else {
                $locationMapper = LocationMapper::getInstance($this->adapter);
                $location = $locationMapper->fetchOne($userEducation->location_id);

                $hydrator = new ObjectPropertyHydrator();
                $education = $hydrator->extract($userEducation);

                $degree = $degreeMapper = DegreeMapper::getInstance($this->adapter);
                $degree = $degreeMapper->fetchOne($education['degree_id']);
                $education['degree_id'] = $degree->uuid;

                $location = [
                    'address1' => $location->address1,
                    'address2' => $location->address2,
                    'city1' => $location->city1,
                    'city2' => $location->city2,
                    'country' => $location->country,
                    'formatted_address' => $location->formatted_address,
                    'latitude' => $location->latitude,
                    'longitude' => $location->longitude,
                    'postal_code' => $location->postal_code,
                    'state' => $location->state,
                ];

                $response = [
                    'success' => true,
                    'data' => [
                        'location' => $location,
                        'education' => $education,
                    ]
                ];
            }

            return new JsonModel($response);
        }




        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de los registros de la experiencia laboral
     * @return \Laminas\View\Model\JsonModel
     */
    public function  experienceAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id    = $this->params()->fromRoute('id');
        $user_experience_id = $this->params()->fromRoute('user_experience_id');
        $operation          = $this->params()->fromRoute('operation');

        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isPost()) {
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);

            if ($operation == 'delete' || $operation == 'edit') {
                $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);

                if (!$userExperience) {

                    $response = [
                        'success' => false,
                        'data' => 'ERROR_RECORD_NOT_FOUND'
                    ];

                    return new JsonModel($response);
                } else if ($userProfile->id != $userExperience->user_profile_id) {
                    $response = [
                        'success' => false,
                        'data' => 'ERROR_UNAUTHORIZED'
                    ];

                    return new JsonModel($response);
                }
            } else {
                $userExperience = null;
            }

            $locationMapper = LocationMapper::getInstance($this->adapter);
            if ($operation == 'delete') {
                $this->logger->info('Se borro un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $result = $userExperienceMapper->delete($userExperience);
                if ($result) {
                    $locationMapper->delete($userExperience->location_id);
                }
            } else {


                $form = new ExperienceForm($this->adapter);
                $dataPost = $request->getPost()->toArray();


                $dataPost['is_current'] = isset($dataPost['is_current']) ? $dataPost['is_current'] : UserExperience::IS_CURRENT_NO;
                if ($dataPost['is_current']  == UserExperience::IS_CURRENT_YES) {
                    $dataPost['to_month'] = 12;
                    $dataPost['to_year'] = date('Y');
                }


                $form->setData($dataPost);

                if ($form->isValid()) {




                    if (!$userExperience) {
                        $userExperience = new UserExperience();
                        $userExperience->user_id = $userProfile->user_id;
                        $userExperience->user_profile_id = $userProfile->id;
                    }

                    $dataPost = (array) $form->getData();
                    $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
                    $companySize = $companySizeMapper->fetchOneByUuid($dataPost['company_size_id']);

                    $industryMapper = IndustryMapper::getInstance($this->adapter);
                    $industry = $industryMapper->fetchOneByUuid($dataPost['industry_id']);

                    $hydrator = new ObjectPropertyHydrator();
                    $hydrator->hydrate($dataPost, $userExperience);

                    $userExperience->company_size_id = $companySize->id;
                    $userExperience->industry_id = $industry->id;

                    if ($userExperience->is_current == UserExperience::IS_CURRENT_YES) {
                        $userExperience->to_month = null;
                        $userExperience->to_year = null;
                    }

                    if ($userExperience->location_id) {
                        $location = $locationMapper->fetchOne($userExperience->location_id);
                    } else {
                        $location = new Location();
                    }
                    $hydrator->hydrate($dataPost, $location);

                    if ($userExperience->location_id) {
                        $this->logger->info('Se actualizo un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                        $result = $locationMapper->update($location);
                    } else {
                        $this->logger->info('Se agrego un registro de experiencia del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                        $result = $locationMapper->insert($location);

                        if ($result) {
                            $userExperience->location_id = $location->id;
                        }
                    }
                    if ($result) {
                        if ($userExperience->id) {
                            $result = $userExperienceMapper->update($userExperience);
                        } else {
                            $result =  $userExperienceMapper->insert($userExperience);
                        }
                    }
                } else {
                    $messages = [];
                    $form_messages = (array) $form->getMessages();
                    foreach ($form_messages  as $fieldname => $field_messages) {
                        $messages[$fieldname] = array_values($field_messages);
                    }

                    return new JsonModel([
                        'success'   => false,
                        'data'   => $messages,
                    ]);
                }
            }

            if ($result) {
                $industryMapper = IndustryMapper::getInstance($this->adapter);
                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
                $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);

                foreach ($userExperiences  as &$userExperience) {
                    $location = $locationMapper->fetchOne($userExperience->location_id);
                    $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
                    $industry = $industryMapper->fetchOne($userExperience->industry_id);

                    $userExperience = [
                        'company' => $userExperience->company,
                        'industry' => $industry,
                        'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . '-' . $companySize->maximum_no_of_employee . ') ',
                        'title' => $userExperience->title,
                        'formatted_address' => $location->formatted_address,
                        'from_year' => $userExperience->from_year,
                        'from_month' => $userExperience->from_month,
                        'to_year' => $userExperience->to_year,
                        'to_month' => $userExperience->to_month,
                        'description' => $userExperience->description,
                        'is_current' => $userExperience->is_current,
                        'link_edit' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'edit', 'user_experience_id' => $userExperience->uuid]),
                        'link_delete' => $this->url()->fromRoute('profile/my-profiles/experience', ['id' => $userProfile->uuid, 'operation' => 'delete', 'user_experience_id' => $userExperience->uuid]),
                    ];
                }

                $response = [
                    'success'   => true,
                    'data' => $userExperiences
                ];
            } else {
                $response = [
                    'success'   => false,
                    'data' => 'ERROR_THERE_WAS_AN_ERROR'
                ];
            }

            return new JsonModel($response);
        } else if ($request->isGet() && $operation == 'edit') {
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
            $userExperience = $userExperienceMapper->fetchOneByUuid($user_experience_id);

            if (!$userExperience) {
                $response = [
                    'success' => false,
                    'data' => 'ERROR_RECORD_NOT_FOUND'
                ];
            } else if ($userProfile->id != $userExperience->user_profile_id) {
                $response = [
                    'success' => false,
                    'data' => 'ERROR_UNAUTHORIZED'
                ];
            } else {
                $hydrator = new ObjectPropertyHydrator();
                $experience = $hydrator->extract($userExperience);


                $industryMapper = IndustryMapper::getInstance($this->adapter);
                $industry = $industryMapper->fetchOne($userExperience->industry_id);

                $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);

                $experience['industry_id'] = $industry->uuid;
                $experience['company_size_id'] = $companySize->uuid;

                $locationMapper = LocationMapper::getInstance($this->adapter);
                $location = $locationMapper->fetchOne($userExperience->location_id);

                $response = [
                    'success' => true,
                    'data' => [
                        'location' => $hydrator->extract($location),
                        'experience' => $experience
                    ]
                ];
            }
            return new JsonModel($response);
        }

        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Maneja la subida y eliminación de la imagen del perfil de usuario.
     * Este método permite:
     * - Subir una nueva imagen de perfil
     * - Eliminar la imagen existente
     * - Actualizar la imagen principal del usuario si el perfil es público
     * 
     * @return \Laminas\View\Model\JsonModel
     */
    public function imageAction()
    {
        // Obtener el usuario actual del sistema
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        // Obtener parámetros de la ruta: ID del perfil y operación (upload/delete)
        $user_profile_id    = $this->params()->fromRoute('id');
        $operation          = $this->params()->fromRoute('operation');

        // Obtener el perfil de usuario correspondiente
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);

        // Validar que el perfil existe
        if (!$userProfile) {
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
        }

        // Verificar que el usuario actual es el propietario del perfil
        if ($currentUser->id != $userProfile->user_id) {
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
        }

        // Procesar solo peticiones POST
        $request = $this->getRequest();
        if ($request->isPost()) {
            // Inicializar el sistema de almacenamiento
            $storage = Storage::getInstance($this->config, $this->adapter);
            $target_path = $storage->getPathUser();
            $user_uuid = $currentUser->uuid;

            // Manejar la eliminación de la imagen
            if ($operation == 'delete') {
                // Registrar la acción en el log
                $this->logger->info('Se borro el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                // Si existe una imagen, intentar eliminarla
                if ($userProfile->image) {
                    if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->image)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }
                }

                // Limpiar el campo de imagen y actualizar la base de datos
                $userProfile->image = '';
                if (!$userProfileMapper->updateImage($userProfile)) {
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                }
            } else {
                // Manejar la subida de una nueva imagen
                $form = new ImageForm($this->config);
                $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());

                $form->setData($data);

                if ($form->isValid()) {
                    // Configurar el archivo para procesamiento
                    $storage->setFiles($request->getFiles()->toArray());
                    if (!$storage->setCurrentFilename('image')) {
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
                    }

                    // Obtener dimensiones objetivo para el redimensionamiento
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
                    $target_width = (int)$target_width_str;
                    $target_height = (int)$target_height_str;

                    // Generar nombres de archivo únicos
                    $source_filename    = $storage->getTmpFilename();
                    $filename           = 'user-profile-' . uniqid() . '.png';
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $user_uuid, $filename);

                    // Subir y redimensionar la imagen
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }

                    // Eliminar la imagen anterior si existe
                    if ($userProfile->image) {
                        if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->image)) {
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                        }
                    }

                    // Actualizar el perfil con la nueva imagen
                    $userProfile->image = $filename;
                    if (!$userProfileMapper->updateImage($userProfile)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }

                    // Si es un perfil público, actualizar también la imagen principal del usuario
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
                        $main_user_filename = 'user-' . uniqid() . '.png';
                        $target_filename_main_user = $storage->composePathToFilename(Storage::TYPE_USER, $user_uuid, $main_user_filename);

                        // Copiar la imagen del perfil para la imagen principal
                        if (!$storage->copyFile($target_filename, $target_filename_main_user)) {
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                        }

                        // Eliminar la imagen principal anterior si existe
                        if ($currentUser->image) {
                            if (!$storage->deleteFile($target_path, $user_uuid, $currentUser->image)) {
                                return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                            }
                        }

                        // Actualizar la imagen principal del usuario
                        $currentUser->image = $main_user_filename;
                        $userMapper = UserMapper::getInstance($this->adapter);
                        if (!$userMapper->updateImage($currentUser)) {
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                        }
                    }

                    // Registrar la actualización en el log
                    $this->logger->info('Se actualizo el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
                } else {
                    // Devolver errores de validación del formulario
                    return $this->_createFormErrorResponse($form);
                }
            }

            // Devolver respuesta exitosa con las URLs actualizadas
            $storage = Storage::getInstance($this->config, $this->adapter);
            return new JsonModel([
                'success'   => true,
                'data' => [
                    'user' => $storage->getUserImage($currentUser),
                    'profile' => $storage->getUserProfileImage($currentUser, $userProfile),
                    'update_navbar' =>  $userProfile->public == UserProfile::PUBLIC_YES ? 1 : 0,
                ]
            ]);
        }

        // Si no es una petición POST, devolver error
        return $this->_createSimpleErrorResponse('ERROR_METHOD_NOT_ALLOWED');
    }

    /**
     * Handles changing the user profile's cover image.
     * Supports uploading a new cover or deleting the existing one.
     * @return \\Laminas\\View\\Model\\JsonModel
     */
    public function coverAction()
    {
        // Get current user
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        // Get parameters from route
        $user_profile_id    = $this->params()->fromRoute('id');
        $operation          = $this->params()->fromRoute('operation'); // Expected: 'upload' or 'delete'

        // Fetch user profile
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);

        // Validate profile existence
        if (!$userProfile) {
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
        }

        // Authorize current user against the profile: only the profile owner can modify it
        if ($currentUser->id != $userProfile->user_id) {
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $storage = Storage::getInstance($this->config, $this->adapter);
            $target_path = $storage->getPathUser(); // Base path for user-specific files
            $user_uuid = $currentUser->uuid;

            // Handle cover deletion operation
            if ($operation == 'delete') {
                if ($userProfile->cover) {
                    // Attempt to delete the existing cover file from storage
                    if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->cover)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }
                }
                // Log the deletion action
                $this->logger->info('Se borro el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
                $userProfile->cover = ''; // Clear the cover field in the profile model

                // Persist the cleared cover information to the database
                if (!$userProfileMapper->updateCover($userProfile)) {
                    // Handle error if database update fails for deletion
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                }
            } else { // Handle cover upload operation
                $form = new CoverForm($this->config);
                // Merge POST data and FILES data for the form
                $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
                $form->setData($data);


                if ($form->isValid()) {
                    // Set files in storage and select the 'cover' file by its input name
                    $storage->setFiles($request->getFiles()->toArray());
                    if (!$storage->setCurrentFilename('cover')) { // Ensure 'cover' is the correct file input name from CoverForm
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
                    }

                    // Prepare for image resize and save: Get target dimensions from config
                    // Example config value: '1200x400'
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
                    $target_width = (int)$target_width_str;
                    $target_height = (int)$target_height_str;

                    $filename           = 'user-cover-' . uniqid() . '.png'; // Generate unique filename for the new cover
                    $source_filename    = $storage->getTmpFilename(); // Temporary path of the uploaded file
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $user_uuid, $filename);

                    // Upload, resize, and save the image
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }

                    // If an old cover exists, delete it from storage
                    if ($userProfile->cover) {
                        if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->cover)) {
                            // Decide if this is a critical error. Original code returns error.
                            // Consider logging this and proceeding if deletion of old file is non-critical.
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                        }
                    }

                    // Update profile model with new cover filename
                    $userProfile->cover = $filename;
                    // Persist the new cover information to the database
                    if (!$userProfileMapper->updateCover($userProfile)) {
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
                    }

                    // Log successful update
                    $this->logger->info('Se actualizo el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
                } else {
                    // Form is invalid, return validation messages
                    return $this->_createFormErrorResponse($form);
                }
            } // End of upload/delete specific logic

            // Successful operation (upload or delete has been handled and persisted)
            // Return success response with the current cover URL (new, or default after delete)
            // The $storage instance from the beginning of the POST block is still valid.
            return new JsonModel([
                'success'   => true,
                'data' => $storage->getUserProfileCover($currentUser, $userProfile)
            ]);
        } // End of isPost()

        // If not a POST request, return method not allowed error
        return $this->_createSimpleErrorResponse('ERROR_METHOD_NOT_ALLOWED');
    }

    /**
     * Actualización de las habilidades
     * @return \Laminas\View\Model\JsonModel
     */
    public function aptitudeAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isGet()) {
            $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);


            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
            $userAptitudes  = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);

            $items = [];
            foreach ($userAptitudes as $userAptitude) {
                $aptitude = $aptitudeMapper->fetchOne($userAptitude->aptitude_id);
                array_push($items, $aptitude->uuid);
            }

            $data = [
                'success' => true,
                'data' => $items
            ];

            return new JsonModel($data);
        } else if ($request->isPost()) {

            $form = new AptitudeForm($this->adapter);
            $dataPost = $request->getPost()->toArray();

            $form->setData($dataPost);

            if ($form->isValid()) {
                $this->logger->info('Se actualizaron las habilidades del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);


                $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
                $userAptitudeMapper->deleteByUserProfileId($userProfile->id);

                $dataPost = (array) $form->getData();
                $aptitudes = $dataPost['aptitudes'];
                foreach ($aptitudes as $aptitude_uuid) {

                    $aptitude = $aptitudeMapper->fetchOneByUuid($aptitude_uuid);


                    $userAptitude = new UserAptitude();
                    $userAptitude->user_id = $userProfile->user_id;
                    $userAptitude->user_profile_id = $userProfile->id;
                    $userAptitude->aptitude_id =  $aptitude->id;

                    $userAptitudeMapper->insert($userAptitude);
                }

                $items = [];

                $records = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
                foreach ($records as $record) {
                    $aptitude = $aptitudeMapper->fetchOne($record->aptitude_id);
                    array_push($items,  ['value' => $aptitude->uuid, 'label' => $aptitude->name]);
                }

                return new JsonModel([
                    'success'   => true,
                    'data'   => $items
                ]);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }

            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    /**
     * Actualización de las habilidades
     * @return \Laminas\View\Model\JsonModel
     */
    public function hobbyAndInterestAction()
    {
        $currentUserPlugin = $this->plugin('currentUserPlugin');
        $currentUser = $currentUserPlugin->getUser();

        $user_profile_id = $this->params()->fromRoute('id');
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);

        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
        if (!$userProfile) {
            $response = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($response);
        }

        if ($currentUser->id != $userProfile->user_id) {
            $response = [
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ];

            return new JsonModel($response);
        }



        $request = $this->getRequest();
        if ($request->isGet()) {
            $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);


            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
            $userHobbyAndInterests  = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);

            $items = [];
            foreach ($userHobbyAndInterests as $userHobbyAndInterest) {
                $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($userHobbyAndInterest->hobbyAndInterest_id);
                array_push($items, $hobbyAndInterest->uuid);
            }

            $data = [
                'success' => true,
                'data' => $items
            ];

            return new JsonModel($data);
        } else if ($request->isPost()) {

            $form = new HobbyAndInterestForm($this->adapter);
            $dataPost = $request->getPost()->toArray();


            $form->setData($dataPost);

            if ($form->isValid()) {
                $this->logger->info('Se actualizaron las habilidades del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);

                $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);


                $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
                $userHobbyAndInterestMapper->deleteByUserProfileId($userProfile->id);

                $dataPost = (array) $form->getData();
                $hobbyAndInterests = $dataPost['hobbies_and_interests'];
                foreach ($hobbyAndInterests as $hobbyAndInterest_uuid) {

                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOneByUuid($hobbyAndInterest_uuid);


                    $userHobbyAndInterest = new UserHobbyAndInterest();
                    $userHobbyAndInterest->user_id = $userProfile->user_id;
                    $userHobbyAndInterest->user_profile_id = $userProfile->id;
                    $userHobbyAndInterest->hobby_and_interest_id =  $hobbyAndInterest->id;

                    $userHobbyAndInterestMapper->insert($userHobbyAndInterest);
                }

                $items = [];

                $records = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
                foreach ($records as $record) {
                    $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($record->hobby_and_interest_id);
                    array_push($items,  ['value' => $hobbyAndInterest->uuid, 'label' => $hobbyAndInterest->name]);
                }

                return new JsonModel([
                    'success'   => true,
                    'data'   => $items
                ]);
            } else {
                $messages = [];
                $form_messages = (array) $form->getMessages();
                foreach ($form_messages  as $fieldname => $field_messages) {
                    $messages[$fieldname] = array_values($field_messages);
                }

                return new JsonModel([
                    'success'   => false,
                    'data'   => $messages
                ]);
            }

            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
        }


        $data = [
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ];


        return new JsonModel($data);
    }

    // Helper methods for creating standardized JSON responses
    // These can be used across various actions in this controller

    /**
     * Creates a standardized JSON error response with a simple message.
     * @param string $errorMessageKey Identifier for the error message (e.g., a translation key or constant).
     * @return JsonModel
     */
    private function _createSimpleErrorResponse($errorMessageKey)
    {
        return new JsonModel([
            'success' => false,
            'data'    => $errorMessageKey
        ]);
    }

    /**
     * Creates a JSON response for form validation errors.
     * Extracts messages from the form and structures them for the client.
     * @param \Laminas\Form\FormInterface $form The form instance containing validation messages.
     * @return JsonModel
     */
    private function _createFormErrorResponse($form)
    {
        $messages = [];
        $form_messages = (array) $form->getMessages(); // Get all error messages from the form
        foreach ($form_messages as $fieldname => $field_messages_for_field) {
            // Ensure messages for each field are in a simple array format for the frontend
            $messages[$fieldname] = array_values((array)$field_messages_for_field);
        }
        return new JsonModel([
            'success' => false,
            'data'    => $messages
        ]);
    }
}