<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class CMBIRD_ZCRM_Modules {

	/**
	 * create API Get call to Zoho CRM to get all custom fields
	 *
	 * @param module $module - module name
	 * @return array | \WP_Error
	 */
	public function cmbird_get_module( $module ) {
		$zoho_crm_url             = get_option( 'cmbird_zoho_crm_url' );
		$url                      = $zoho_crm_url . 'crm/v7/settings/fields?module=' . $module;
		$execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
		$json                     = $execute_curl_call_handle->execute_curl_call_get( $url );
		if ( is_wp_error( $json ) ) {
			return $json;
		} else {
			// Parse the response.
			$parsed_fields = array();

			foreach ( $json->fields as $field ) {
				$parsed_fields[] = array(
					'id'           => $field->id,
					'apiName'      => $field->api_name,
					'visible'      => (bool) $field->visible,
					'fieldLabel'   => $field->field_label,
					'displayLabel' => $field->display_label,
					'customField'  => (bool) $field->custom_field,
					'dataType'     => $field->data_type,
				);
			}

			return $parsed_fields;
		}
	}

	/**
	 * create API Get call to Zoho CRM to get a record of a module
	 *
	 * @param module    $module - module name
	 * @param record_id $record_id - record id
	 * @return array | \WP_Error
	 */
	public function cmbird_get_record( $module, $record_id ) {
		// add delay of 0.5 seconds to avoid rate limiting.
		usleep( 500000 ); // 0.5 seconds.
		if ( empty( $record_id ) ) {
			return new \WP_Error( 'cmbird_no_record_id', __( 'No record ID provided.', 'commercebird' ) );
		}
		$zoho_crm_url             = get_option( 'cmbird_zoho_crm_url' );
		$url                      = $zoho_crm_url . 'crm/v7/' . $module . '/' . $record_id;
		$execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
		$json                     = $execute_curl_call_handle->execute_curl_call_get( $url );
		if ( is_wp_error( $json ) ) {
			return $json;
		} else {
			return $json;
		}
	}

	/**
	 * create API Get call to Zoho CRM to create a record of a module
	 *
	 * @param string $module - module name
	 * @param array  $data - data to be created
	 * @return array | \WP_Error
	 */
	public function cmbird_create_record( $module, $data ) {
		// add delay of 0.5 seconds to avoid rate limiting.
		usleep( 500000 ); // 0.5 seconds.
		$zoho_crm_url             = get_option( 'cmbird_zoho_crm_url' );
		$url                      = $zoho_crm_url . 'crm/v7/' . $module;
		$execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
		// convert the data to JSON format.
		if ( is_array( $data ) ) {
			$data = json_encode( $data );
		}
		if ( ! $data ) {
			return new \WP_Error( 'cmbird_no_data', __( 'No data provided for creating record.', 'commercebird' ) );
		}
		$json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
		if ( ! isset( $json->data[0]->code ) || 'SUCCESS' !== $json->data[0]->code ) {
			return new \WP_Error( '500', $json->data[0]->message, array( 'status' => 500 ) );
		}
		return $json;
	}

	/**
	 * create API Get call to Zoho CRM to update a record of a module
	 *
	 * @param string $module - module name
	 * @param string $record_id - record id
	 * @param array  $data - data to be updated
	 * @return array | \WP_Error
	 */
	public function cmbird_update_record( $module, $record_id, $data ) {
		// add delay of 0.5 seconds to avoid rate limiting.
		usleep( 500000 ); // 0.5 seconds.
		if ( empty( $record_id ) ) {
			return new \WP_Error( 'cmbird_no_record_id', __( 'No record ID provided.', 'commercebird' ) );
		}
		if ( empty( $data ) ) {
			return new \WP_Error( 'cmbird_no_data', __( 'No data provided for updating record.', 'commercebird' ) );
		}
		$zoho_crm_url             = get_option( 'cmbird_zoho_crm_url' );
		$url                      = $zoho_crm_url . 'crm/v7/' . $module . '/' . $record_id;
		$execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
		// convert the data to JSON format.
		if ( is_array( $data ) ) {
			$data = json_encode( $data );
		}
		$json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
		// response has data in the format of {"data":[{"code":"SUCCESS","details":{}}],"info":{"per_page":200,"count":1,"page":1}}.
		// if code is not SUCCESS, return error.
		if ( ! isset( $json->data[0]->code ) || 'SUCCESS' !== $json->data[0]->code ) {
			return new \WP_Error( '500', $json->data[0]->message, array( 'status' => 500 ) );
		}
		return $json;
	}
}
