<?php

require_once(APPPATH . 'third_party/php_utils/abstract_entity' . EXT);
require_once(APPPATH . 'entities/user_details_entity' . EXT);
require_once(APPPATH . 'entities/guest_user_entity' . EXT);
require_once(APPPATH . 'entities/salaried_employee_entity' . EXT);
require_once(APPPATH . 'entities/user_business_entity' . EXT);
require_once(APPPATH . 'entities/other_business_entity' . EXT);
require_once(APPPATH . 'entities/other_profession_entity' . EXT);
require_once(APPPATH . 'entities/other_occupation_entity' . EXT);
require_once(APPPATH . 'entities/otp_entity' . EXT);
require_once(APPPATH . 'entities/user_profession_entity' . EXT);
require_once(APPPATH . 'entities/session_entity' . EXT);
require_once(APPPATH . 'entities/admin_settings_entity' . EXT);
class User {

    public function login($rest) {

        $rest->load->library('login_lib');
        $rest->load->library('get_uuid');
        $rest->load->helper('password');
        
       
        $contact = isset($rest->body->contact) ? strtolower(trim($rest->body->contact)) : '';
        $email = isset($rest->body->email) ? strtolower(trim($rest->body->email)) : '';
        $password = isset($rest->body->password) ? trim($rest->body->password) : '';

        if ($contact == '' && $email == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The contact or email field is required.', 'field_name' => 'contact or email')), 400);
            return;
        }
        if ($password == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The password field is required.', 'field_name' => 'password')), 400);
            return;
        }
        
        if($contact)
        {
            $mob="/^[0-9][0-9]{9}$/"; 
            if(!preg_match($mob, $contact)){
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter valid phone number.', 'field_name' => 'contact')), 400);
                return;
            }
            $valid = $rest->login_lib->auth_user($contact, $password,'contact');
            
        }
        
        if($email){
            if (!filter_var($rest->body->email, FILTER_VALIDATE_EMAIL)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter a valid email.')), 401);
            return;
            }
            $valid = $rest->login_lib->auth_user($email, $password,'email');    
        }
       
        if ($valid) { 
            
            $data = $rest->login_lib->get();
           
            if($data->role=='User' || $data->role=='Admin') {
               if($data->active_status=='0' || $data->active_status=='') {
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Your account has not been activated.')), 400);
                return; 
              }
            }
              $enable_notifications='';
              $enable_moderations='';
           if($data->role=='Admin'){
               $admin_settings_obj = new Admin_settings_entity();
               $admin_settings_obj->admin_id = $data->id;
               $admin_settings_details = $admin_settings_obj->get();
               if(count($admin_settings_details['result'])>0){
                   $enable_notifications = $admin_settings_details['result'][0]['enable_notifications'];
                   $enable_moderations = $admin_settings_details['result'][0]['enable_moderations'];
               }
           }
            $user_id           = $data->id;
            $first_name        = isset($data->first_name)?$data->first_name:'';
            $email             = isset($data->email)?$data->email:'';
            $last_name         = isset($data->last_name)?$data->last_name:'';
            $date_of_birth     = isset($data->date_of_birth)?$data->date_of_birth:'';
            $contact           = isset($data->contact)?$data->contact:'';
            $gender            = isset($data->gender)?$data->gender:'';
            $native_place      = isset($data->native_place)?$data->native_place:'';
            $image             = !empty($data->image)?base_url() . 'uploads/users/' .$data->image:'';
            $role              = $data->role;
            $profile_visited   = $data->profile_visited;
            
            $session_obj = new Session_entity();
            $session_obj->user_id = $data->id;
            $session_check = $session_obj->get_user_session();
            
            if (!empty($session_check['result'])) {
                $old_session = $session_check['result'][0]['id'];
            } else {
                $old_session = '';
            }
            $session_obj->id = $rest->get_uuid->_get_uuid();
            $rest->session->set_userdata(array('session_id' => $session_obj->id));
           
            if ($old_session == '') {
                $session_result = $session_obj->save();
               
                if (!$session_result['success']) {
                    $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session expired.')), 400);
                    return;
                }
            } else {
                    $session_obj->update_user_session();
            }
         
            $rest->response(array(
                'api_session_id' => $session_obj->id,
                'id' => $user_id,
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $email,
                "date_of_birth" => $date_of_birth,
                "contact" => $contact,
                "gender" => $gender,
                "native_place" => $native_place,
                "image" => $image,
                "role"=>$role,
                "profile_visited"=>$profile_visited,
                "enable_moderations"=>$enable_moderations,
                "enable_notifications"=>$enable_notifications,
                'message' => "You have successfully logged in to Alumni."), 200);
            return;
        } else {

            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Invalid Sign In credentials.')), 401);
            return;
        }
    }
    public function guest_login($rest) {

        $rest->load->library('login_lib');
        $rest->load->library('get_uuid');
        $rest->load->helper('password');
        
        
        
        $contact = isset($rest->body->contact) ? strtolower(trim($rest->body->contact)) : '';
        $email = isset($rest->body->email) ? strtolower(trim($rest->body->email)) : '';
        $password = isset($rest->body->password) ? trim($rest->body->password) : '';

        if ($contact == '' && $email == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The contact or email field is required.', 'field_name' => 'contact or email')), 400);
            return;
        }
        if ($password == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The password field is required.', 'field_name' => 'password')), 400);
            return;
        }
        if($contact)
        $valid = $rest->login_lib->auth_guest_user($contact, $password,'contact');
        
        if($email){
            if (!filter_var($rest->body->email, FILTER_VALIDATE_EMAIL)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter a valid email.')), 401);
            return;
            }
            $valid = $rest->login_lib->auth_guest_user($email, $password,'email');    
        }
        

       
        if ($valid) { 

            $data = $rest->login_lib->get();
           
            $session_obj = new Session_entity();
            $session_obj->user_id = $data->id;
            $session_check = $session_obj->get_user_session();
            
            if (!empty($session_check['result'])) {
                $old_session = $session_check['result'][0]['id'];
            } else {
                $old_session = '';
            }
            $session_obj->id = $rest->get_uuid->_get_uuid();
            $rest->session->set_userdata(array('session_id' => $session_obj->id));
           
            if ($old_session == '') {
                $session_result = $session_obj->save();
               
                if (!$session_result['success']) {
                    $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session expired.')), 400);
                    return;
                }
            } else {
                    $session_obj->update_user_session();
            }
         
            $user_id           = $data->id;
            $first_name        = isset($data->first_name)?$data->first_name:'';
            $email             = isset($data->email)?$data->email:'';
            $last_name         = isset($data->last_name)?$data->last_name:'';
            $date_of_birth     = isset($data->date_of_birth)?$data->date_of_birth:'';
            $contact           = isset($data->contact)?$data->contact:'';
            $gender            = isset($data->gender)?$data->gender:'';
            $native_place      = isset($data->native_place)?$data->native_place:'';
            $image             = !empty($data->image)?base_url() . 'uploads/users/' .$data->image:'';
            $role              = "Guest";
            
            $rest->response(array(
                'api_session_id' => $session_obj->id,
                'id' => $user_id,
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $email,
                "date_of_birth" => $date_of_birth,
                "contact" => $contact,
                "gender" => $gender,
                "native_place" => $native_place,
                "image" => $image,
                "role"=>$role,
                'message' => "You have successfully logged in to Alumni."), 200);
            
            return;
        } else {

            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Invalid Sign In credentials.')), 401);
            return;
        }
    }
    public function updateUserProfile($rest) {
        
        $user_details_obj = new User_details_entity();

        $rest->load->helper('password');
        if(!isset($rest->body->user_details)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => "Invalid Format.")), 400);
            return; 
        }
        $user_details_data_obj = $rest->body->user_details;

        $user_details_properties = array('first_name', 'last_name');

        // Required Properties exist - User details
       
        $result = $rest->required_properties_present($user_details_data_obj, $user_details_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        
        // Required properties not null - User details
        
        $result = $rest->required_properties_notnull($user_details_data_obj, $user_details_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }

        // email validation 
        if(isset($rest->body->user->email)) {
            if (!filter_var($rest->body->user->email, FILTER_VALIDATE_EMAIL)) {
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter a valid email.')), 401);
                return;
            }
        }

        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }

        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_detail = $user_details_obj->get();

        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_details_obj->first_name = $rest->body->user_details->first_name;
        $user_details_obj->last_name = $rest->body->user_details->last_name;
        $user_details_obj->gender = $user_detail['result'][0]['gender'];
        $user_details_obj->date_of_birth = isset($rest->body->user_details->date_of_birth) ? strtolower(trim($rest->body->user_details->date_of_birth)) : '';
        $user_details_obj->native_place = isset($rest->body->user_details->native_place) ? strtolower(trim($rest->body->user_details->native_place)) : '';
        $user_details_obj->contact = isset($rest->body->user_details->contact) ? strtolower(trim($rest->body->user_details->contact)) : '';
        $user_details_obj->image = $user_detail['result'][0]['image'];
        $user_details_obj->password = $user_detail['result'][0]['password'];
        $user_details_obj->blood_group = $user_detail['result'][0]['blood_group']; 
        $user_details_obj->marital_status = $user_detail['result'][0]['marital_status'];
        $user_details_obj->email = $user_detail['result'][0]['email'];
        $user_details_obj->address = $user_detail['result'][0]['address'];
        $user_details_obj->role = $user_detail['result'][0]['role'];;
        $user_details_obj->active_status = 1;
        $user_details_obj->profile_visited = 1;
        $user_details_obj->updated_at = time();
        $user_result = $user_details_obj->update();

        if (!$user_result['success']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User profile update failed.')), 400);
            return;
        }
        if (!empty($user_detail['result'])) {
            $user_det_result = $user_details_obj->update();
            
        } else {
            $user_det_result = $user_details_obj->save();
           
        }
        $this->deleteUserProfessions($session_data['result'][0]['user_id']);
        
        
        if(property_exists($rest->body,'salaried')){
           $this->updateUserEmployer($rest);
        }
        if(property_exists($rest->body,'business')){
            $this->updateUserBusiness($rest);
        }
        if(property_exists($rest->body,'profession')){
           $this->updateUserProfession($rest);
        }
        if(property_exists($rest->body, 'other_business')) {
            $this->updateUserOtherBusiness($rest);
        }
        if(property_exists($rest->body, 'other_profession')) {
            $this->updateUserOtherProfession($rest);
        }
        if(property_exists($rest->body, 'other_occupation')) {
            $this->updateUserOtherOccupation($rest);
        }

        if (!$user_det_result['success']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User profile update failed.')), 400);
            return;
        }

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'User profile have been successfully updated.')), 200);
        return;
    }
    public function deleteUserProfessions($user_id) {
       $salaried_employee_obj = new Salaried_employee_entity();
       $salaried_employee_obj->user_id = $user_id;
       $salaried_employee_obj->delete();
       
       $user_business_obj           = new User_business_entity();
       $user_business_obj->user_id = $user_id;
       $user_business_obj->delete();
       
       $user_profession_obj          = new User_profession_entity();
       $user_profession_obj->user_id = $user_id;
       $user_profession_obj->delete();
       
       $other_business_obj          = new Other_business_entity();
       $other_business_obj->user_id = $user_id;
       $other_business_obj->delete();
       
       $other_profession_obj          = new Other_profession_entity();
       $other_profession_obj->user_id = $user_id;
       $other_profession_obj->delete();
       
       $other_occupation_obj          = new Other_occupation_entity();
       $other_occupation_obj->user_id = $user_id;
       $other_occupation_obj->delete();
    }
    public function updateUserEmployer($rest) {
        
        $error_string = array();
       
        $salaried_employee_obj = new Salaried_employee_entity();
        unset( $salaried_employee_obj->created_at);
        unset( $salaried_employee_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $salaried_employee_data_obj = $rest->body->salaried;
        $salaried_employee_properties = array('designation', 'employer','sector');
        
        // Required Properties exist - Salaried Employee
        $result = 0;
        $result = $rest->required_properties_present($salaried_employee_data_obj, $salaried_employee_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        
         // Required properties not null - Salaried Employee
        $result = 0;
        $result = $rest->required_properties_notnull($salaried_employee_data_obj, $salaried_employee_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
      
        
        $salaried_employee_obj->user_id = $session_data['result'][0]['user_id'];
        $salaried_employee_details = $salaried_employee_obj->get();
       
        $salaried_employee_obj->id = $rest->get_uuid->_get_uuid();
        $salaried_employee_obj->designation = $rest->body->salaried->designation;
        $salaried_employee_obj->skills = isset($rest->body->salaried->skills) ? strtolower(trim($rest->body->salaried->skills)) : '';
        $salaried_employee_obj->employer = $rest->body->salaried->employer;
        $salaried_employee_obj->sector = $rest->body->salaried->sector;
        $salaried_employee_obj->website = isset($rest->body->salaried->employer_site) ? $rest->body->salaried->employer_site : '';
        $salaried_employee_obj->user_id = $session_data['result'][0]['user_id'];
       
        if (!empty($salaried_employee_details['result'])) {
              $salaried_employee_result = $salaried_employee_obj->update();
        } else {
             $salaried_employee_result = $salaried_employee_obj->save();
        }
       
        return $salaried_employee_result;
       
    }
    public function updateUserBusiness($rest) {
        
        $error_string = array();
       
        $user_business_obj = new User_business_entity();
        unset( $user_business_obj->created_at);
        unset( $user_business_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $user_business_data_obj = $rest->body->business;
        $user_business_properties = array('business_name', 'business_type','business_nature','business_address','business_location','business_description');
        $count = count($user_business_data_obj);
        $single_row = ($count>1)?'FALSE':'TRUE';
       
        if($single_row == 'TRUE') {
            $user_business_data_obj = $user_business_data_obj[0];
            // Required Properties exist - Business
            $result = 0;
            $result = $rest->required_properties_present($user_business_data_obj, $user_business_properties, $error_string);

            if (!$result) {
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
                return;
            }

             // Required properties not null - Business
            $result = 0;
            $result = $rest->required_properties_notnull($user_business_data_obj, $user_business_properties, $error_string);

            if (!$result) {
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
                return;
            }
        }
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
       
       
        if($single_row == 'TRUE') {
            $user_business_obj->user_id = $session_data['result'][0]['user_id'];
            $user_business_details = $user_business_obj->get();

            $user_business_obj->id = $rest->get_uuid->_get_uuid();
            $user_business_obj->business_name = $user_business_data_obj->business_name;
            $user_business_obj->business_type = $user_business_data_obj->business_type;
            $user_business_obj->business_nature = $user_business_data_obj->business_nature;
            $user_business_obj->business_address = $user_business_data_obj->business_address;
            $user_business_obj->business_description = $user_business_data_obj->business_description;
            $user_business_obj->location = $user_business_data_obj->business_location;
            $user_business_obj->website = isset($user_business_data_obj->business_site) ? $user_business_data_obj->business_site : '';
            $user_business_obj->user_id = $session_data['result'][0]['user_id'];

            if (!empty($user_business_details['result'])) {
                  $user_business_result = $user_business_obj->update();
            } else {
                  $user_business_result = $user_business_obj->save();
            }
        } else {
             $user_business_result = $this->updateUserMultipleBusiness($rest,$session_data['result'][0]['user_id']);
        }
       
        return $user_business_result;
    }
    public function updateUserOtherBusiness($rest) {
        
        $error_string = array();
       
        $other_business_obj = new Other_business_entity();
        unset( $other_business_obj->created_at);
        unset( $other_business_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $other_business_data_obj = $rest->body->other_business;
        
        $count = count($other_business_data_obj);
        $single_row = ($count>1)?'FALSE':'TRUE';
        
        if($single_row == 'TRUE'){
        
        $other_business_data_obj=$other_business_data_obj[0];
        $other_business_properties = array('other_business_type','other_business_nature','other_business_name', 'other_business_description');
        
                  // Required Properties exist - Business
       
        $result = $rest->required_properties_present($other_business_data_obj, $other_business_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }

         // Required properties not null - Business
       
        $result = $rest->required_properties_notnull($other_business_data_obj, $other_business_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        }
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        if($single_row == 'TRUE'){
        $other_business_obj->user_id = $session_data['result'][0]['user_id'];
        $other_business_details = $other_business_obj->get();

        $other_business_obj->id = $rest->get_uuid->_get_uuid();
        $other_business_obj->business_type = $other_business_data_obj->other_business_type;
        $other_business_obj->business_nature = $other_business_data_obj->other_business_nature;
        $other_business_obj->business_name = $other_business_data_obj->other_business_name;
        $other_business_obj->business_description = $other_business_data_obj->other_business_description;
        $other_business_obj->location = isset($other_business_data_obj->other_business_location)?$other_business_data_obj->other_business_location:'';
        $other_business_obj->business_address = isset($other_business_data_obj->other_business_address)?$other_business_data_obj->other_business_address:'';
        $other_business_obj->website = isset($other_business_data_obj->other_business_site)?$other_business_data_obj->other_business_location:'';
        $other_business_obj->user_id = $session_data['result'][0]['user_id'];

        if (!empty($other_business_details['result'])) {
              $other_business_details = $other_business_obj->update();
        } else {
              $other_business_details = $other_business_obj->save();
        }
        }else
        {
            $other_business_details = $this->updateUserOtherMultipleBusiness($rest,$session_data['result'][0]['user_id']);
        }
        return $other_business_details;
    }
    public function updateUserMultipleBusiness($rest,$user_id) {
       
        $user_business_obj = new User_business_entity();
        $user_business_result = array();
        unset( $user_business_obj->created_at);
        unset( $user_business_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $user_business_data_obj = $rest->body->business;
        
        $user_business_obj->user_id = $user_id;
        $user_business_details = $user_business_obj->get();
        
        if (!empty($user_business_details['result'])) {
            $user_business_result = $user_business_obj->delete();
        }
        foreach ($user_business_data_obj as $key=>$value) {
          
            $user_business_obj->id = $rest->get_uuid->_get_uuid();
            $user_business_obj->business_name = $value->business_name;
            $user_business_obj->business_type = $value->business_type;
            $user_business_obj->business_nature = $value->business_nature;
            $user_business_obj->business_address = $value->business_address;
            $user_business_obj->location = $value->business_location;
            $user_business_obj->website = isset($value->business_site) ? $value->business_site : '';
            $user_business_obj->user_id = $user_id;
            $user_business_result = $user_business_obj->save();
        }
       return $user_business_result;
    }
     public function updateUserOtherMultipleBusiness($rest,$user_id) {
       
        $user_business_result = array();
        
        $user_business_obj = new Other_business_entity();
        
        unset( $user_business_obj->created_at);
        unset( $user_business_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        
        $user_business_data_obj = $rest->body->other_business;
        
        $user_business_obj->user_id = $user_id;
        $user_business_details = $user_business_obj->get();
        
        if (!empty($user_business_details['result'])) {
            $user_business_result = $user_business_obj->delete();
        }
        foreach ($user_business_data_obj as $key=>$value) {
          
            $user_business_obj->id = $rest->get_uuid->_get_uuid();
            $user_business_obj->business_name = $value->other_business_name;
            $user_business_obj->business_type = $value->other_business_type;
            $user_business_obj->business_nature = $value->other_business_nature;
            $user_business_obj->business_address = $value->other_business_address;
            $user_business_obj->location = $value->other_business_location;
            $user_business_obj->website = isset($value->other_business_site) ? $value->other_business_site : '';
            $user_business_obj->user_id = $user_id;
            $user_business_result = $user_business_obj->save();
        }
       return $user_business_result;
    }
    public function updateUserProfession($rest) {
        
        $error_string = array();
       
        $user_profession_obj = new User_profession_entity();
        unset( $user_profession_obj->created_at);
        unset( $user_profession_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $user_profession_data_obj = $rest->body->profession;
        $user_profession_properties = array('firm_name', 'profession_type','area_of_expertise','firm_address');
        
        $count = count($user_profession_data_obj);
        $single_row = ($count>1)?'FALSE':'TRUE';
        if($single_row == 'TRUE') {
        // Required Properties exist - Business
        $result = $rest->required_properties_present($user_profession_data_obj, $user_profession_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        
         // Required properties not null - Business
        $result = $rest->required_properties_notnull($user_profession_data_obj, $user_profession_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        }
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        if($single_row == 'TRUE') {
            $user_profession_obj->user_id = $session_data['result'][0]['user_id'];
            $user_profession_details = $user_profession_obj->get();

            $user_profession_obj->id = $rest->get_uuid->_get_uuid();
            $user_profession_obj->firm_name = $rest->body->profession->firm_name;
            $user_profession_obj->profession_type = $rest->body->profession->profession_type;
            $user_profession_obj->area_of_expertise = $rest->body->profession->area_of_expertise;
            $user_profession_obj->firm_address = $rest->body->profession->firm_address;
            $user_profession_obj->website = isset($rest->body->profession->firm_site) ? $rest->body->profession->firm_site : '';
            $user_profession_obj->user_id = $session_data['result'][0]['user_id'];

            if (!empty($user_profession_details['result'])) {
                  $user_profession_result = $user_profession_obj->update();
            } else {
                  $user_profession_result = $user_profession_obj->save();
            }
        } else {
            $user_profession_result =$this->updateUserMultipleProfession($rest,$session_data['result'][0]['user_id']);
        }
       
        return $user_profession_result;
    }
    public function updateUserMultipleProfession($rest,$user_id) {
       
        $user_profession_obj = new User_profession_entity();
        unset( $user_profession_obj->created_at);
        unset( $user_profession_obj->updated_at);
       
        
        $rest->load->library('get_uuid');
        
        $user_profession_data_obj = $rest->body->profession;
        
        $user_profession_obj->user_id = $user_id;
        $user_profession_details = $user_profession_obj->get();
        
        if (!empty($user_profession_details['result'])) {
            $user_profession_result = $user_profession_obj->delete();
        }
        foreach ($user_profession_data_obj as $key=>$value) {
          
            $user_profession_obj->id = $rest->get_uuid->_get_uuid();
            $user_profession_obj->firm_name = $value->firm_name;
            $user_profession_obj->profession_type = $value->profession_type;
            $user_profession_obj->area_of_expertise = $value->area_of_expertise;
            $user_profession_obj->firm_address = $value->firm_address;
            $user_profession_obj->website = isset($value->firm_site) ? $value->firm_site : '';
            $user_profession_obj->user_id = $user_id;
            
            $user_profession_result = $user_profession_obj->save();
        }
       return $user_profession_result;
    }
    public function updateUserOtherProfession($rest) {
        
        $error_string = array();
       
        $other_profession_obj = new Other_profession_entity();
        unset( $other_profession_obj->created_at);
        unset( $other_profession_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $other_profession_data_obj = $rest->body->other_profession;
        
        $other_profession_properties = array('other_profession_name','other_firm_name', 'other_area_of_expertise','other_firm_address');
        $count = count($other_profession_data_obj);
        $single_row = ($count>1)?'FALSE':'TRUE';
                  // Required Properties exist - Business
        if($single_row == 'TRUE'){
        $other_profession_data_obj=$other_profession_data_obj[0];
        $result = $rest->required_properties_present($other_profession_data_obj, $other_profession_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }

         // Required properties not null - Business
       
        $result = $rest->required_properties_notnull($other_profession_data_obj, $other_profession_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        }
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        if($single_row == 'TRUE'){
        $other_profession_obj->user_id = $session_data['result'][0]['user_id'];
        $other_profession_details = $other_profession_obj->get();

        $other_profession_obj->id = $rest->get_uuid->_get_uuid();
        $other_profession_obj->profession_type =  $other_profession_data_obj->other_profession_name;
        $other_profession_obj->firm_name =  $other_profession_data_obj->other_firm_name;
        $other_profession_obj->area_of_expertise =  $other_profession_data_obj->other_area_of_expertise;
        $other_profession_obj->firm_address = isset($other_profession_data_obj->other_firm_address)? $other_profession_data_obj->other_firm_address:'';
        $other_profession_obj->website = isset( $other_profession_data_obj->other_firm_site)? $other_profession_data_obj->other_firm_site:'';
        $other_profession_obj->user_id = $session_data['result'][0]['user_id'];

        if (!empty($other_profession_details['result'])) {
              $other_profession_details = $other_profession_obj->update();
        } else {
              $other_profession_details = $other_profession_obj->save();
        } 
        
        }else
        {
            $other_profession_details = $this->updateUserOtherMultipleProfession($rest,$session_data['result'][0]['user_id']);
        }
        return $other_profession_details;
    }
    public function updateUserOtherMultipleProfession($rest,$user_id) {
       
        $user_profession_obj = new Other_profession_entity();
        unset( $user_profession_obj->created_at);
        unset( $user_profession_obj->updated_at);
       
        
        $rest->load->library('get_uuid');
        
        $user_profession_data_obj = $rest->body->other_profession;
        
        $user_profession_obj->user_id = $user_id;
        $user_profession_details = $user_profession_obj->get();
        
        if (!empty($user_profession_details['result'])) {
            $user_profession_result = $user_profession_obj->delete();
        }
        foreach ($user_profession_data_obj as $key=>$value) {
          
            $user_profession_obj->id = $rest->get_uuid->_get_uuid();
            $user_profession_obj->firm_name = $value->other_firm_name;
            $user_profession_obj->profession_type = $value->other_profession_name;
            $user_profession_obj->area_of_expertise = $value->other_area_of_expertise;
            $user_profession_obj->firm_address = $value->other_firm_address;
            $user_profession_obj->website = isset($value->other_firm_site) ? $value->other_firm_site : '';
            $user_profession_obj->user_id = $user_id;
            
            $user_profession_result = $user_profession_obj->save();
        }
       return $user_profession_result;
    }
    public function updateUserOtherOccupation($rest) {
        
        $error_string = array();
       
        $other_occupation_obj = new Other_occupation_entity();
        unset( $other_occupation_obj->created_at);
        unset( $other_occupation_obj->updated_at);
        
        $rest->load->library('get_uuid');
        
        $other_occupation_data_obj = $rest->body->other_occupation;
        $other_occupation_properties = array('other_occupation_name', 'other_occupation_description','other_occupation_location');
        
                  // Required Properties exist - Business
       
        $result = $rest->required_properties_present($other_occupation_data_obj, $other_occupation_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }

         // Required properties not null - Business
       
        $result = $rest->required_properties_notnull($other_occupation_data_obj, $other_occupation_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
      
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
  
        $other_occupation_obj->user_id = $session_data['result'][0]['user_id'];
        $other_occupation_details = $other_occupation_obj->get();

        $other_occupation_obj->id = $rest->get_uuid->_get_uuid();
        $other_occupation_obj->occupation_name = $rest->body->other_occupation->other_occupation_name;
        $other_occupation_obj->occupation_description = $rest->body->other_occupation->other_occupation_description;
        $other_occupation_obj->location = isset($rest->body->other_occupation->other_occupation_location)?$rest->body->other_occupation->other_occupation_location:'';
        $other_occupation_obj->user_id = $session_data['result'][0]['user_id'];

        if (!empty($other_occupation_details['result'])) {
              $other_occupation_details = $other_occupation_obj->update();
        } else {
              $other_occupation_details = $other_occupation_obj->save();
        }      
        return $other_occupation_details;
    }
    public function registerNewUser($rest) {
        
        $error_string = array();
       
        $user_details_obj = new User_details_entity();

        $rest->load->helper('password');
        $rest->load->library('get_uuid');
        
        
        
        if(!isset($rest->body->user_details)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => "Invalid json format")), 400);
            return;
        }
        $user_details_data_obj = $rest->body->user_details;
        
        $user_details_properties = array('first_name', 'last_name','contact','pass_out_year','native_place');
        
        $result = 0;
        $result = $rest->required_properties_present($user_details_data_obj, $user_details_properties, $error_string);
        
        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        
        $result = 0;
        $result = $rest->required_properties_notnull($user_details_data_obj, $user_details_properties, $error_string);

        if (!$result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error_string)), 400);
            return;
        }
        $new_password = $this->generateOtp();
        $user_details_obj->contact = $rest->body->user_details->contact;
        $user_detail = $user_details_obj->get();
        
        $user_details_obj->id = $rest->get_uuid->_get_uuid();
        $user_details_obj->first_name = $rest->body->user_details->first_name;
        $user_details_obj->last_name = $rest->body->user_details->last_name;
        $user_details_obj->gender = isset($rest->body->user_details->gender) ? strtolower(trim($rest->body->user_details->gender)) : '';
        $user_details_obj->date_of_birth = isset($rest->body->user_details->date_of_birth) ? strtolower(trim($rest->body->user_details->date_of_birth)) : '';
        $user_details_obj->native_place = isset($rest->body->user_details->native_place) ? strtolower(trim($rest->body->user_details->native_place)) : '';
        $user_details_obj->contact = isset($rest->body->user_details->contact) ? strtolower(trim($rest->body->user_details->contact)) : '';
        $user_details_obj->email = isset($rest->body->user_details->email) ? strtolower(trim($rest->body->user_details->email)) : '';
        $user_details_obj->image = isset($rest->body->user_details->image) ? strtolower(trim($rest->body->user_details->image)) : '';
        $user_details_obj->password = create_hash($new_password);
        $user_details_obj->role = 1;
        $user_details_obj->active_status = 0;
        $user_details_obj->profile_visited = 0;
        $user_details_obj->created_at = time();
        $user_details_obj->updated_at = time();
       
        if (!empty($user_detail['result'])) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User already registered with contact number.','field_name'=>'contact')), 400);
            return;
        } else {
            $user_det_result = $user_details_obj->save();
        }
        
        if (!$user_det_result['success']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User registration failed.')), 400);
            return;
        }
         if(!empty($rest->body->user_details->contact))
        {
            $contact = $rest->body->user_details->contact;
            $user_id = $user_details_obj->id;
            $this->sendSMS($rest,$new_password,$contact,$user_id,$type='new-user');
            
        }
        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'User registration have been successfully done.')), 200);
        return;
    }
    public function guest_user_registration($rest) {
        
        $user_details_obj = new User_details_entity();
        $guest_user_obj = new Guest_user_entity();
        
        $rest->load->helper('password');
        $rest->load->library('get_uuid');
        
        $contact = isset($rest->body->contact)?$rest->body->contact:'';
        $email = isset($rest->body->email)?$rest->body->email:'';
        
       if ($contact == '' && $email == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The contact or email field is required.', 'field_name' => 'contact or email')), 400);
            return;
        }
        if($contact) {
            $user_details_obj->contact = $contact;
        }
        
        if($email){
            if (!filter_var($rest->body->email, FILTER_VALIDATE_EMAIL)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter a valid email.')), 401);
            return;
            }
            $user_details_obj->email = $email; 
        } 
        
        $user_details_data = $user_details_obj->get();
      
        if(count($user_details_data['result'])>0){
        
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User already registered.')), 401);
            return;
        } else {
            $new_password = $this->generateOtp();
            $guest_user_obj->id = $rest->get_uuid->_get_uuid();
            $guest_user_obj->contact = isset($rest->body->contact) ? strtolower(trim($rest->body->contact)) : '';
            $guest_user_obj->email = isset($rest->body->email) ? strtolower(trim($rest->body->email)) : '';
            $guest_user_obj->password = create_hash($new_password);
            $guest_user_obj->created_at = time();
            $guest_user_obj->updated_at = time();
            $guest_user_obj->save();
            
            if($contact || !empty($user_detail['result'][0]['contact']))
            {
                $this->sendSMS($rest,$new_password,$contact,$guest_user_obj->id);
            }
        }
        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'User registration have been successfully done.')), 200);
        return;
    }
    public function get_users_list($rest) {
        
        $user_details_obj = new User_details_entity();
        $filters = isset($rest->body->filters)?$rest->body->filters:'';
        $user_detail = $user_details_obj->get_users_list($filters);
       
        
        
         if (!empty($user_detail)) {
             foreach ($user_detail as $key => $value){
                 
                 $array['id']=$value['user_id'];
                 $user_details_obj->id = $value['user_id'];
                 $array['first_name']=$value['first_name'];
                 $array['last_name']=$value['last_name'];
                 $array['profile_picture']= !empty($value['image'])?base_url().'uploads/users/'.$value['image']:'';
                 $array['native_place']=$value['native_place'];
                 $array['pass_out_year']=$value['pass_out_year'];
                
                 $user_business_detail = $user_details_obj->get_business_list($user_details_obj);
                 
                 if(!empty($user_business_detail[0]['business_id'])){
                    $array['business_id']= $user_business_detail[0]['business_id'];
                    $array['business_name']= $user_business_detail[0]['business_name'];
                    $array['business_type_name']= $user_business_detail[0]['business_type_name'];
                    $array['business_address']= $user_business_detail[0]['business_address'];
                    $array['business_site']= $user_business_detail[0]['website'];
                    $array['business_location']= $user_business_detail[0]['location'];
                    
                   
                 }
                 else
                 {
                    $array['business_id'] = "";
                    $array['business_name']= "";
                    $array['business_type_name']="";
                    $array['business_address']= "";
                    $array['business_site']= "";
                    $array['business_location']= "";
                 }
                 $user_profession_detail = $user_details_obj->get_profession_list($user_details_obj);
                 
                 if(!empty($user_profession_detail[0]['profession_id']) && empty($array['business_id'])){
                    $array['profession_id']= $user_profession_detail[0]['profession_id'];
                    $array['profession_type_name']= $user_profession_detail[0]['profession_type_name'];
                    $array['firm_name']= $user_profession_detail[0]['firm_name'];
                    $array['firm_address']= $user_profession_detail[0]['firm_address'];
                    $array['firm_site']= $user_profession_detail[0]['website'];
                   
                 }
                 else
                 {
                    $array['profession_id'] = "";
                    $array['firm_name']= "";
                    $array['profession_type']="";
                    $array['firm_address']= "";
                    $array['firm_site']= "";
                 }
                 if(!empty($value['salaried_id']) && empty($array['business_id']) && empty($array['profession_id'])){
                    $array['salaried_id']= $value['salaried_id'];
                    $array['designation']= $value['designation'];
                    $array['company_name']= $value['employer'];
                    $array['skills']= $value['skills'];
                    $array['sector']= $value['sector'];
                    $array['employer_site']= $value['employer_site'];
                   
                 }
                 else
                 {
                    $array['salaried_id'] = "";
                    $array['company_name']= "";
                    $array['designation'] = "";
                    $array['company_name']= "";
                    $array['skills']= "";
                    $array['sector']= "";
                    $array['employer_site']= "";
                 }
                 
                    $user_array['result'][]=$array; 
                 }
                 
              
            $rest->response($user_array, 200);
            return;
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_salaried_list($rest) {
        
        $user_details_obj = new User_details_entity();
        $filters = isset($rest->body->filters)?$rest->body->filters:'';
        $user_detail = $user_details_obj->get_salaried_list($filters);
        
         if (!empty($user_detail)) {
            foreach ($user_detail as $key => $value){
                    
                    $array['id']                = $value['user_id'];
                    $array['first_name']        = $value['first_name'];
                    $array['last_name']         = $value['last_name'];
                    $array['profile_picture']   = !empty($value['image'])?base_url().'uploads/users/'.$value['image']:'';
                    $array['native_place']      = $value['native_place'];
                    $array['pass_out_year']     = $value['pass_out_year'];
                    $array['salaried_id']       = $value['salaried_id'];
                    $array['designation']       = $value['designation'];
                    $array['company_name']      = $value['employer'];
                    $user_array['result'][]=$array; 
               
                }          
                $rest->response($user_array, 200);
                return;
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_business_list($rest) {
        
        $user_details_obj = new User_details_entity();
        $filters = isset($rest->body->filters)?$rest->body->filters:'';
        $user_detail = $user_details_obj->get_business_list($filters);
      
        $user_array['result'] =array();
         if (!empty($user_detail)) {
             foreach ($user_detail as $key => $value){
             
                 $array['id']                 = $value['business_id'];
                 $array['business_name']      = !empty($value['business_name'])?$value['business_name']:'';
                 $array['business_owner_name']= $value['first_name'].' '.$value['last_name'];
                 $array['business_type']      = !empty($value['business_type_name'])?$value['business_type_name']:'';
                 $array['business_nature']    = !empty($value['business_nature_name'])?$value['business_nature_name']:''; 
                 $array['business_address']   = !empty($value['business_address'])?$value['business_address']:'';
                 $array['website']            = !empty($value['business_site'])?$value['business_site']:'';
                 $array['profile_picture']    = !empty($value['image'])?base_url().'uploads/users/'.$value['image']:'';
                 $user_array['result'][]=$array; 
               
               
             }
             if(empty($user_array['result'])){
                 $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
                 return;
             }else {
                 $rest->response($user_array, 200);
                 return;
             }
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_profession_list($rest) {
        
        $user_details_obj = new User_details_entity();
        $filters = isset($rest->body->filters)?$rest->body->filters:'';
        $user_detail = $user_details_obj->get_profession_list($filters);
       
        $user_array =array();
         if (!empty($user_detail)) {
             foreach ($user_detail as $key => $value){
               if($value['profession_id'] != null) {
                 $array['id']                     = $value['profession_id'];
                 $array['firm_name']              = !empty($value['firm_name'])?$value['firm_name']:'';
                 $array['profession_owner_name']  = $value['first_name'].' '.$value['last_name'];
                 $array['owner_contact_no']       = !empty($value['contact'])?$value['contact']:'';
                 $array['profession_type']        = !empty($value['profession_type_name'])?$value['profession_type_name']:'';
                 $array['area_of_expertise']      = !empty($value['area_of_expertise'])?$value['area_of_expertise']:''; 
                 $array['firm_address']           = !empty($value['firm_address'])?$value['firm_address']:'';
                 $array['website']                = !empty($value['firm_site'])?$value['firm_site']:'';
                 $array['profile_picture']        = !empty($value['image'])?base_url().'uploads/users/'.$value['image']:'';
                 $user_array['result'][]=$array; 
               }
             }
            if(empty($user_array['result'])){
                 $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
                 return;
             }else {
                 $rest->response($user_array, 200);
                 return;
             }
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_user($rest) {
        
        $user_details_obj = new User_details_entity();
        
        if($_GET['id']){
            $user_id= $_GET['id'];
            $user_details_obj->user_id= $user_id;
        }else{
            $user_id='';
            $user_details_obj->user_id= $user_id;
        }
        
        $filters = $user_details_obj;
        
        $user_detail = $user_details_obj->get_user_details_metadata($filters);
       
         if (!empty($user_detail)) {
             
             foreach ($user_detail as $key => $value){
                 
                unset($value['password']);
                $array['result']['id']         = $value['id'];
                $array['result']['first_name'] = $value['first_name'];
                $array['result']['last_name']  = $value['last_name'];
                
                $array['result']['native_place']          = !empty($value['native_place'])?$value['native_place']:'';
                $array['result']['image']                 = !empty($value['image'])?base_url().'uploads/users/'.$value['image']:'';
                $array['result']['email']                 = !empty($value['email'])?$value['email']:'';
                $array['result']['contact']               = !empty($value['contact'])?$value['contact']:'';
                $array['result']['address']               = !empty($value['address'])?$value['address']:'';
                $array['result']['pass_out_year']         = !empty($value['pass_out_year'])?$value['pass_out_year']:'';
                $array['result']['blood_group']           = !empty($value['blood_group'])?$value['blood_group']:'';
                $array['result']['marital_status']        = !empty($value['marital_status'])?$value['marital_status']:'';
               
                $value['employer_site']         = !empty($value['employer_site'])?$value['employer_site']:'';
               
                $value['salaried_id']           = !empty($value['salaried_id'])?$value['salaried_id']:'';
                $value['designation']           = !empty($value['designation'])?$value['designation']:'';
                $value['employer']              = !empty($value['employer'])? $value['employer']:'';
                $value['employer_site']         = !empty($value['employer_site'])?$value['employer_site']:'';
                $value['sector']                = !empty($value['sector'])?$value['sector']:'';
                $value['skills']                = !empty($value['skills'])?$value['skills']:'';
                
                $array['result']['record_status']         = !empty($value['record_status'])?$value['record_status']:'';
                
                $array['result']['other_occupation_id']         = !empty($value['other_occupation_id'])?$value['other_occupation_id']:'';
                $array['result']['other_occupation_name']       = !empty($value['other_occupation_name'])?$value['other_occupation_name']:'';
                $array['result']['other_occupation_description']= !empty($value['other_occupation_description'])?$value['other_occupation_description']:'';
               
                if(!empty($value['profession_id'])) {
                    $array['result']['profession'][]= array("id"=>$value['profession_id'],
                                                        "firm_name"=>$value['firm_name'],
                                                        "profession_type_name"=>$value['profession_type_name'],
                                                        "area_of_expertise"=>$value['area_of_expertise'],
                                                        "firm_address"=>$value['firm_address'],
                                                        "firm_site"=>$value['firm_site']);
                } else {
                    $array['result']['profession'][0]= array("id"=>"",
                                                        "firm_name"=>"",
                                                        "profession_type_name"=>"",
                                                        "area_of_expertise"=>"",
                                                        "firm_address"=>"",
                                                        "firm_site"=>"");
                }
                if(!empty($value['salaried_id'])) {
                    $array['result']['salaried'][]= array("id"=>$value['salaried_id'],
                                                        "designation"=>$value['designation'],
                                                        "employer"=>$value['employer'],
                                                        "employer_site"=>$value['employer_site'],
                                                        "sector"=>$value['sector']);
                }else {
                    $array['result']['salaried'][0]= array("id"=>"",
                                                        "designation"=>"",
                                                        "employer"=>"",
                                                        "employer_site"=>"",
                                                        "sector"=>"");
                }
                
                if(!empty($value['business_id'])) {
                    $array['result']['business'][]= array("id"=>$value['business_id'],
                                                        "business_name"=>$value['business_name'],
                                                        "business_type"=>$value['business_type_name'],
                                                        "business_nature"=>$value['business_nature_name'],
                                                        "business_address"=>$value['business_address'],
                                                        "business_location"=>$value['business_location'],
                                                        "business_site"=>$value['business_site'],
                                                        "business_description"=>$value['business_description']
                                                        );
                } else {
                    $array['result']['business'][0]= array("id"=>"",
                                                        "business_name"=>"",
                                                        "business_type_name"=>"",
                                                        "business_nature_name"=>"",
                                                        "business_address"=>"",
                                                        "business_location"=>"",
                                                        "business_site"=>"",
                                                        "business_description"=>"");
                }
                if(!empty($value['other_profession_id'])) {
                    $array['result']['other_profession'][]= array("id"=>$value['other_profession_id'],
                                                            "other_profession_name"=>$value['other_profession_name'],
                                                            "other_firm_name"=>$value['other_firm_name'],
                                                            "other_firm_address"=>$value['other_firm_address'],
                                                            "other_firm_site"=>$value['other_firm_site']); 
                     
                }
                else {
                    $array['result']['other_profession'][0]= array("id"=>"",
                                                            "other_profession_name"=>"",
                                                            "other_firm_name"=>"",
                                                            "other_firm_address"=>"",
                                                            "other_firm_site"=>""); 
                     
                }
                if(!empty($value['other_business_id'])) {
                    $array['result']['other_business'][]= array("id"=>$value['other_business_id'],
                                                            "other_business_name"=>$value['other_business_name'],
                                                            "other_business_type"=>$value['other_business_type'],
                                                            "other_business_nature"=>$value['other_business_nature'],
                                                            "other_business_description"=>$value['other_business_description'],
                                                            "other_business_address"=>$value['other_business_address'],
                                                            "other_business_location"=>$value['other_business_location'],
                                                            "other_business_site"=>$value['other_business_site']); 
                     
                }
                else {
                    $array['result']['other_business'][0]= array("id"=>"",
                                                            "other_business_name"=>"",
                                                            "other_business_type"=>"",
                                                            "other_business_nature"=>"",
                                                            "other_business_description"=>"",
                                                            "other_business_address"=>"",
                                                            "other_business_location"=>"",
                                                            "other_business_site"=>"");  
                     
                }
                  
             }
          
                $rest->response($array, 200);
                return;
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_business($rest) {
        
        $user_details_obj = new User_details_entity();
        
        if(isset($_GET['id'])){
            $business_id= $_GET['id'];
            $user_details_obj->business_id= $business_id;
        }else{
            $business_id='';
            $user_details_obj->business_id= $business_id;
        }
        
        $filters = $user_details_obj;
        
        $user_detail = $user_details_obj->get_user_details_metadata($filters);
      
         if (!empty($user_detail)) {
             foreach ($user_detail as $key => $value){
                 $array['result']['id']=$value['business_id'];
                 $array['result']['business_name']=$value['business_name'];
                 $array['result']['business_owner_name']=$value['first_name'].' '.$value['last_name'];
                 $array['result']['business_type']=$value['business_type_name'];
                 $array['result']['business_nature']=$value['business_nature_name']; 
                 $array['result']['business_address']=$value['business_address'];
                 $array['result']['website']=$value['business_site'];
                  
             }
            $rest->response($array, 200);
            return;
            } else {
              $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'No Results found.')), 400);
              return;
                
            }
           
    }
    public function get_profession_type_metadata($rest) {
        
        
        $array=array();
        if(isset($_GET['pType'])){
            $profession_type= $_GET['pType'];
         
        }else{
            $profession_type='';
        }
        if($profession_type=='salaried_employee'){
            $user_salaried_obj = new Salaried_employee_entity();
            $user_designations = $user_salaried_obj->get_designations();
            $user_sectors = $user_salaried_obj->get_sectors();
            
            $array['designations']=$user_designations;
            $array['sectors']=$user_sectors;
           
        }
        if($profession_type == 'business') {
            $user_business_obj = new User_business_entity();
            $user_business_type = $user_business_obj->get_business_type();
            $user_business_nature = $user_business_obj->get_business_nature();
            $array['business_type']=$user_business_type;
            $array['business_nature']=$user_business_nature;
        }
        if($profession_type == 'professional') {
            $user_profession_obj = new User_profession_entity();
            $user_profession_type = $user_profession_obj->get_profession_type();
            $array['profession_type']=$user_profession_type;
        }
        
         $rest->response($array, 200);
         return;
    }
    public function skip_user_profile($rest) {
        
        $user_details_obj   = new User_details_entity();

        $session_obj        = new Session_entity();
        $session_obj->id    = $rest->headers['APIKey'];
        $session_data       = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }

        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_detail          = $user_details_obj->get();

        $user_details_obj->id               = $session_data['result'][0]['user_id'];
        $user_details_obj->first_name       = $user_detail['result'][0]['first_name'];
        $user_details_obj->last_name        = $user_detail['result'][0]['last_name'];
        $user_details_obj->gender           = $user_detail['result'][0]['gender'];
        $user_details_obj->date_of_birth    = $user_detail['result'][0]['date_of_birth'];
        $user_details_obj->native_place     = $user_detail['result'][0]['native_place'];
        $user_details_obj->contact          = $user_detail['result'][0]['contact'];
        $user_details_obj->image            = $user_detail['result'][0]['image'];
        $user_details_obj->password         = $user_detail['result'][0]['password'];
        $user_details_obj->blood_group      = $user_detail['result'][0]['blood_group']; 
        $user_details_obj->marital_status   = $user_detail['result'][0]['marital_status'];
        $user_details_obj->email            = $user_detail['result'][0]['email'];
        $user_details_obj->address          = $user_detail['result'][0]['address'];
        $user_details_obj->role             = $user_detail['result'][0]['role'];
        $user_details_obj->active_status    = 1;
        $user_details_obj->profile_visited  = 1;
        $user_details_obj->updated_at       = time();
        
        $user_result = $user_details_obj->update();

        if (!$user_result['success']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User profile skipped failed.')), 400);
            return;
        }
        if (!empty($user_detail['result'])) {
            $user_det_result = $user_details_obj->update();
            
        } else {
            $user_det_result = $user_details_obj->save();
           
        }
        
        if (!$user_det_result['success']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'User profile skipped failed.')), 400);
            return;
        }

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'User profile skipped.')), 200);
        return;
    }
    public function upload_profile_pic($rest) {
      
        $foldername = './uploads/users/';
       //print_r($_FILES);exit;
        if(!file_exists($foldername) && !is_dir($foldername)) {
            mkdir($foldername, 0750, true);
        }
        $user_details_obj = new User_details_entity();
        
        $session_obj = new Session_entity();
        $session_obj->id = $rest->input->post('APIKey');
        $session_data = $session_obj->get();
       
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_detail = $user_details_obj->get();

        if (count($user_detail['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
       
        //image configuration
        $new_image_name = time().$_FILES['file']['name'];
        
        $config['upload_path'] = $foldername;
        $config['allowed_types'] = 'gif|jpg|png|jpeg|JPG|JPEG|PNG|GIF';
        $config['max_size'] = '10240';
        $config['overwrite'] = true;
        $config['file_name'] = $new_image_name;
        
        $rest->load->library('upload', $config);        

        if ( ! $rest->upload->do_upload("file"))
        {
          
            $error = array('error' => $rest->upload->display_errors('',''));
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => $error)), 400);
            return;

        }
        else
        {
            $data = array('upload_data' => $rest->upload->data());
            $user_details_obj->id = $session_data['result'][0]['user_id'];
            $user_details_obj->first_name = $user_detail['result'][0]['first_name'];
            $user_details_obj->last_name = $user_detail['result'][0]['last_name'];
            $user_details_obj->gender = $user_detail['result'][0]['gender'];
            $user_details_obj->date_of_birth = $user_detail['result'][0]['date_of_birth'];
            $user_details_obj->native_place = $user_detail['result'][0]['native_place'];
            $user_details_obj->contact = $user_detail['result'][0]['contact'];
            $user_details_obj->blood_group = $user_detail['result'][0]['blood_group']; 
            $user_details_obj->marital_status = $user_detail['result'][0]['marital_status'];
            $user_details_obj->email = $user_detail['result'][0]['email'];
            $user_details_obj->address = $user_detail['result'][0]['address'];
            $user_details_obj->image =  $new_image_name;
            $user_details_obj->role = $user_detail['result'][0]['role'];;
            $user_details_obj->active_status = 1;
            $user_details_obj->profile_visited = 1;
            $user_details_obj->password = $user_detail['result'][0]['password'];
            $user_details_obj->created_at = time();
            $user_details_obj->updated_at = time();
            $user_details_obj->update();
            $image_path=base_url().$foldername.$new_image_name;
            $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'Profile pic uploaded successfully.','image_path'=>$image_path)), 200);
            return;
             
        }
         $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'Profile pic uploaded successfully.')), 200);
         return;
    }
    public function remove_profile_pic($rest) {
        
        $user_details_obj = new User_details_entity();
        
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
       
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_detail = $user_details_obj->get();

        if (count($user_detail['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        
        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $user_details_obj->first_name = $user_detail['result'][0]['first_name'];
        $user_details_obj->last_name = $user_detail['result'][0]['last_name'];
        $user_details_obj->gender = $user_detail['result'][0]['gender'];
        $user_details_obj->date_of_birth = $user_detail['result'][0]['date_of_birth'];
        $user_details_obj->native_place = $user_detail['result'][0]['native_place'];
        $user_details_obj->contact = $user_detail['result'][0]['contact'];
        $user_details_obj->blood_group = $user_detail['result'][0]['blood_group']; 
        $user_details_obj->marital_status = $user_detail['result'][0]['marital_status'];
        $user_details_obj->email = $user_detail['result'][0]['email'];
        $user_details_obj->address = $user_detail['result'][0]['address'];
        $user_details_obj->image = "";
        $user_details_obj->role = $user_detail['result'][0]['role'];
        $user_details_obj->active_status = 1;
        $user_details_obj->profile_visited = 1;
        $user_details_obj->password = $user_detail['result'][0]['password'];
        $user_details_obj->created_at = time();
        $user_details_obj->updated_at = time();
        $user_details_obj->update();
        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'Profile pic removed successfully.')), 200);
        return;
    }
    public function logout($rest) {
 
        
        $session_obj = new Session_entity();
        
        $session_obj->id = $rest->headers['APIKey'];

        $session_data = $session_obj->get();
       
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Unable to logout. Please try later.')), 400);
            return;
        }
        $session_obj->delete();

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'You have been successfully logged out..')), 200);
    }
    public function generateOtp() {
        $length = 6;
        $alphabets = range('A', 'Z');
        $numbers = range('0', '9');
        $final_array = array_merge($alphabets, $numbers);

        $new_password = '';

        while ($length--) {
            $key = array_rand($final_array);
            $new_password .= $final_array[$key];
        }
        return $new_password;
    }
    public function forgotPassword($rest) {


        $rest->load->helper('password');
        $rest->load->library('mail');
        $rest->load->library('email');

       
        $user_details_obj = new User_details_entity();
        
        $contact = isset($rest->body->contact) ? strtolower(trim($rest->body->contact)) : '';
        $email = isset($rest->body->email) ? strtolower(trim($rest->body->email)) : '';
        
        if ($contact == '' && $email == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The contact or email field is required.', 'field_name' => 'contact or email')), 400);
            return;
        }
        if($contact) {
            $user_details_obj->contact = $rest->body->contact;
        }
       
        // email validation 
        if($email) {
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Please enter a valid Email Address.')), 400);
            return;
            }
            $user_details_obj->email = $rest->body->email;
        }

       
        $user_detail = $user_details_obj->get();
        
        if (count($user_detail['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'This user is not registered in the system. Please contact the system admin.')), 400);
            return;
        }
        
        $type = 'forgot_password';
        $data['email'] = $user_detail['result'][0]['email'];
        $email = $user_detail['result'][0]['email'];
        //echo $email;die();
        $new_password = $this->generateOtp();

        $hash_password = create_hash($new_password);

        $data['password'] = $new_password;

       
        $user_details_obj->id = $user_detail['result'][0]['id'];
        $user_details_obj->first_name = $user_detail['result'][0]['first_name'];
        $user_details_obj->last_name = $user_detail['result'][0]['last_name'];
        $user_details_obj->gender = $user_detail['result'][0]['gender'];
        $user_details_obj->date_of_birth = $user_detail['result'][0]['date_of_birth'];
        $user_details_obj->native_place = $user_detail['result'][0]['native_place'];
        $user_details_obj->contact = $user_detail['result'][0]['contact'];
        $user_details_obj->blood_group = $user_detail['result'][0]['blood_group']; 
        $user_details_obj->marital_status = $user_detail['result'][0]['marital_status'];
        $user_details_obj->email = $user_detail['result'][0]['email'];
        $user_details_obj->address = $user_detail['result'][0]['address'];
        $user_details_obj->image = $user_detail['result'][0]['image'];
        $user_details_obj->role = $user_detail['result'][0]['role'];;
        $user_details_obj->active_status = 1;
        $user_details_obj->profile_visited = 1;
        

       
        
        if($contact || !empty($user_detail['result'][0]['contact']))
        {
            $response = $this->sendSMS($rest,$new_password,$contact,$user_detail['result'][0]['id'],$type='forgot-password');
            if(isset($response['response']['error'])){
                $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array($response['response']['error'])), 400);
                return;
            } else
            {
                $rest->mail->_create_template($type, $data, $email);
                 
                $user_details_obj->password = $hash_password;

                $user_details_obj->update();
                $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array("message"=>"OTP send successfully")), 200);
                return;
            }
            
        }
    }
    public function getNotifications($rest) {
        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();

        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        $user_id = $session_data['result'][0]['user_id'];
        $notification_obj = new User_notification_entity();
        $notification_update_obj = new User_notification_entity();
        $notification_obj->user_id = $user_id;
        $notification_obj->read_status = 'N';

        $get_all_notifications = $notification_obj->get();
        //print_r($get_all_notifications); exit;
        if (empty($get_all_notifications)) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Notifications are not available.')), 404);
            return;
        }
        $notification_obj->user_id = $user_id;
        $notification_obj->read_status = 'Y';
        //$notification_update_obj->upadate();
        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'notifications' => $get_all_notifications), 200);
        return;
    }
    public function changePassword($rest) {

        $error_string = array();
        $rest->load->helper('password');
        $rest->load->library('mail');
        $rest->load->library('email');

       
        $user_details_obj = new User_details_entity();
        
        $current_password = isset($rest->body->current_password) ? $rest->body->current_password : '';
        $new_password = isset($rest->body->new_password) ? trim($rest->body->new_password) : '';

        if ($current_password == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The Current Password field is required.', 'field_name' => 'current_password')), 400);
            return;
        }
        if ($new_password == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The New Password field is required.', 'field_name' => 'new_password')), 400);
            return;
        }
        

        $rest->load->helper('password');

        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();
        
        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }

        $user_details_obj->id = $session_data['result'][0]['user_id'];
        $old_password = $user_details_obj->get_old_password();
       
        $user_details_obj->password = create_hash($new_password);
        $user_details_obj->updated_at = time();
        if (create_hash($current_password) != $old_password['result'][0]['password']) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Current password not match.')), 400);
            return;
        }
        
        $user_result = $user_details_obj->update_password();
        
        if (!$user_result) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Password update failed.')), 400);
            return;
        }

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'messages' => array('message' => 'Password has been changed successfully.')), 200);
        return;
    }
    public function sendSMS($rest,$password = null, $contact = null,$user_id,$type) {
       
        
        $rest->load->library('sms');
        $response = $rest->sms->send_message($contact,$password,$type);
        
        
        if(isset($response['response']['error'])){
            
            return $response;
        }
        else {
            
            //create top obj
            $otp_obj = new Otp_entity();

            //$otp_obj->user_id = $user_id;
            $otp_obj->contact = $contact;
            $otp_details_data = $otp_obj->get();

            $status ='';
            if (count($otp_details_data['result'])>0) {
                $status = 'update';
                $user_password =  $password;
            }
            else
            {
                $status = 'insert';
                $user_password =  $password;
            }
       
            if ($status == 'update'){
                $otp_obj->id = $otp_details_data['result'][0]['id'];
                $otp_obj->user_id = $user_id;
                $otp_obj->contact = $contact;
                $otp_obj->password = $user_password;
                $otp_obj->status = 'D';
                $otp_obj->updated_at = time(); 

                $result = $otp_obj->update();
            } 
            if ($status == 'insert') {
                $otp_obj->id = $rest->get_uuid->_get_uuid();
                $otp_obj->user_id = $user_id;
                $otp_obj->contact = $contact;
                $otp_obj->password =  $user_password;
                $otp_obj->status = 'A';
                $otp_obj->created_at = time(); 
                $otp_obj->updated_at = time(); 
                $result = $otp_obj->save();
            }
            return $response;
        }
        
       
    }
    public function sendMessage($rest) {

        $rest->load->library('Gcm');
        $rest->load->library('Apns');
        $error_string = array();
        $message = isset($rest->body->message) ? trim($rest->body->message) : '';
        $receiver_id = isset($rest->body->receiver_id) ? trim($rest->body->receiver_id) : '';

        if ($message == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The message field is required.', 'field_name' => 'message')), 400);
            return;
        }
        if ($receiver_id == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The receiver_id field is required.', 'field_name' => 'receiver_id')), 400);
            return;
        }


        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();

        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }

        $chat_history_obj = new Chat_history_entity();
        $chat_history_obj->id = $rest->get_uuid->_get_uuid();
        $chat_history_obj->sender_id = $session_data['result'][0]['user_id'];
        $chat_history_obj->message = $message;
        $chat_history_obj->receiver_id = $receiver_id;
        $chat_history_obj->created_by = $session_data['result'][0]['user_id'];
        $chat_history_obj->updated_by = $session_data['result'][0]['user_id'];
        $chat = $chat_history_obj->save();

        $push_token_obj = new User_register_devices_entity();
        $push_token = $push_token_obj->get_push_token($receiver_id);
        $registatoin_ids = array();
        if (!empty($push_token['result'])) {
            $registatoin_ids = array($push_token['result'][0]['device_push_token']);
        }
        $type = 'chat';
        $messages = array("message" => $message, "type" => $type, "senderid" => $session_data['result'][0]['user_id']);
        if (!empty($push_token['result'])) {
            if ($push_token['result'][0]['device_type'] == 'android') {
                $rest->gcm->send_notification($registatoin_ids, $messages);
            } else if ($push_token['result'][0]['device_type'] == 'ios') {
                $rest->apns->send_notification($registatoin_ids, $message, $type, $session_data['result'][0]['user_id']);
            }
        }
        //print_r($push_token);
        //exit;


        if (!isset($chat['success'])) {


            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Failed to send message.')), 400);
            return;
        }
        $chat_history_objs = new Chat_history_entity();

        $chat = $chat_history_objs->get_message($chat_history_obj->id);
        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'chat_history' => $chat['result'][0], 'messages' => array('message' => 'Message send successfully.')), 200);
        return;
    }
    public function chatHistory($rest, $timestamp) {

        $error_string = array();
        //$timestamp = isset($rest->body->timestamp) ? strtolower(trim($rest->body->timestamp)) : '';


        if ($timestamp == '') {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'The timestamp field is required.', 'field_name' => 'timestamp')), 400);
            return;
        }



        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();

        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        $user_id = $session_data['result'][0]['user_id'];
        $chat_history_obj = new Chat_history_entity();

        $chat = $chat_history_obj->get_all_history($user_id, $timestamp);

        if (empty($chat['result'])) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Messages are not available')), 400);
            return;
        }
        if (!isset($chat['success'])) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Failed to get message.')), 400);
            return;
        }

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'chat_history' => $chat['result']), 200);
        return;
    }   
    public function getMessage($rest) {

        $session_obj = new Session_entity();
        $session_obj->id = $rest->headers['APIKey'];
        $session_data = $session_obj->get();

        if (count($session_data['result']) == 0) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Session has been expired.')), 400);
            return;
        }
        $user_id = $session_data['result'][0]['user_id'];
        $messages_obj = new Messages_entity();
        $messages_obj->active_status = '1';
        $message = $messages_obj->get();

        if (empty($message['result'])) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Message are not available')), 400);
            return;
        }
        if (!isset($message['success'])) {
            $rest->response(array('type' => 'ERROR-BUSINESS', 'messages' => array('message' => 'Failed to get message.')), 400);
            return;
        }

        $rest->response(array('type' => 'SUCCESS-BUSINESS', 'post_message' => $message['result'][0]['message']), 200);
        return;
    }
    
}

?>
