<?php
namespace App\Controllers\MqttServices;

use App\Controllers\MqttServices\Auth\MqttServiceAuth;
use Models\AssetDevicesModel;
use Models\DataPacketsModel;
use Models\UserDeviceInfoModel;

class DataPacketController extends MqttServiceAuth {

    public function actionData() {
        
        if( false == $this->validation->run( $this->g_arrmixRequestParameterDetails, 'validateServiceDataPacket' ) ) { 
            $this->generateMqttError( 'data', ERROR_INVALID_PARAMETERS, implode( ',', $this->validation->getErrors() ), ERROR_TYPE_MESSAGE_INVALID_PARAMETERS );
        }
        
        if( ( int ) $this->g_arrmixDecryptedTokenDetails['imei_number'] != ( int ) $this->g_arrmixRequestParameterDetails['imei']  ) {
            $this->generateMqttError( 'data', ERROR_INVALID_IMEI, 'Provided Imei number does not match with token', ERROR_TYPE_MESSAGE_INVALID_VALUE );
        }

        $arrmixUserDeviceInfoDetails = UserDeviceInfoModel::createService()->findUserDeviceInfoByImei( $this->g_arrmixRequestParameterDetails['imei'] );
        
        if( false == isValArr( $arrmixUserDeviceInfoDetails ) ) {
            $this->generateMqttError( 'error', ERROR_DATA_NOT_FOUND, 'for given imei ' . $this->g_arrmixRequestParameterDetails['imei'], ERROR_TYPE_MESSAGE_DATA_NOT_FOUND );   
        }

        if( $arrmixUserDeviceInfoDetails['product_model_number'] != $this->g_arrmixRequestParameterDetails['model_no'] ) {
            $this->generateMqttError( 'data', ERROR_INVALID_VALUE, 'Model Number', ERROR_TYPE_MESSAGE_INVALID_VALUE );
        }
        

        $arrmixUpdateUserDeviceInfo = [
            'pump_status'               => $this->g_arrmixRequestParameterDetails['value']['P15'],
            'fault_error_code'          => $this->g_arrmixRequestParameterDetails['value']['P16'],
            'user_id'                   => $this->g_arrmixDecryptedTokenDetails['user_id'],
            'last_uploaded_data_packet' => date( 'Y-m-d H:i:s', $this->g_arrmixRequestParameterDetails['epoach'] )
        ];

        UserDeviceInfoModel::createService()->edit( 'user_id', $arrmixUpdateUserDeviceInfo );

        $arrmixInsertData = [
            'user_id'             => $this->g_arrmixDecryptedTokenDetails['user_id'],
            'imei_number'         => $this->g_arrmixRequestParameterDetails['imei'],
            'packet_id'           => $this->g_arrmixRequestParameterDetails['p_id'],
            'device_reading_time' => $this->g_arrmixRequestParameterDetails['epoach'],
            'details '            => json_encode( $this->g_arrmixRequestParameterDetails )
        ];

        $intDataPacketId = DataPacketsModel::createService()->add( $arrmixInsertData );
        
        if( false == isValId( $intDataPacketId ) ) {
            $this->generateMqttError( 'data', ERROR_INSERTION_FAILURE, 'Not able to insert data packet details.', ERROR_TYPE_MESSAGE_SYSTEM_FAILURE );   
        } 

        $arrmixReceivedTopic =  explode( "/", $this->g_strReceivedTopic );

        $arrmixResponseData['is_require_publish'] = false;
        $arrmixResponseData['message']            = 'Data packet has been inserted successfully.';
        $arrmixResponseData['topic']              = self::MQTT_TOPIC . 'pub/' . $arrmixReceivedTopic[3] . '/data-inserted-successfully';
        
        return $arrmixResponseData;
    }
    
}
