<?php
/**
 * The file that defines all the custom Twilio services REST API routes in the plugin
 *
 * @link       https://autoserve.io
 * @since      1.0.4
 *
 * @package    Messageflow
 * @subpackage Messageflow/includes
 */

/**
 * The custom Twilio services REST API routes plugin class.
 *
 * @since      1.0.4
 * @package    Messageflow
 * @subpackage Messageflow/includes
 * @author     Autoserve <support@autoserve.io>
 */
class Messageflow_Twilio_Services_Rest_Routes {

	private $utils;

	private $namespace;

	public function __construct( $utils ) {
		$this->utils     = $utils;
		$this->namespace = 'messageflow/v1';

		add_action( 'rest_api_init', array( $this, 'create_settings_rest_routes' ) );
	}

	// Register settings REST route
	public function create_settings_rest_routes() {

		register_rest_route( $this->namespace, '/send-sms', array(
			'methods'             => 'POST',
			'callback'            => [ $this, 'twilio_send_programmable_sms' ],
			'permission_callback' => function () {
				return true;
			}
		) );
	}

	/** Function using Twilio programmable SMS service
	 * @param $req
	 *
	 * @return mixed
	 */
	public function twilio_send_programmable_sms( $req ) {
		$to_phone_number = $req['to-phone-number'];
		$message         = $req['message'];
		$order_number    = $req['order-number'];

		$is_test_mode_enabled = get_option( 'messageflow-settings-enable-test-mode' );
		$account_sid          = $is_test_mode_enabled === 'true'
			? get_option( 'messageflow-settings-test-account-sid' )
			: get_option( 'messageflow-settings-account-sid' );
		$auth_token           = $is_test_mode_enabled === 'true'
			? get_option( 'messageflow-settings-test-auth-token' )
			: get_option( 'messageflow-settings-auth-token' );
		$phone_number         = $is_test_mode_enabled === 'true'
			? get_option( 'messageflow-settings-test-from-phone' )
			: get_option( 'messageflow-settings-from-phone' );

		$response = $this->utils->twilio_send_programmable_sms( $account_sid, $auth_token, $phone_number, $to_phone_number, $message );
		$order         = wc_get_order( $order_number );

		if ( ! is_wp_error( $response ) ) {
			$response_body = json_decode( wp_remote_retrieve_body( $response ) );
		}

		if ( $response_body->messages[0]->status === 'success' ) {
			$order->add_order_note( '<b>MessageFlow SMS</b><br />' . '<b>To:</b> ' . $to_phone_number . '<br /><b>Message:</b> ' . $message . '<br /><b>Status:</b> <span style=\'color: green;\'>Sent</span>' );

			return rest_ensure_response( array(
				'status'          => 201,
				'message'         => 'Programmable SMS has been successfully queued',
				'twilio_response' => $response_body
			) );
		} else {
			$order->add_order_note( '<b>MessageFlow SMS</b><br />' . '<b>To:</b> ' . $to_phone_number . '<br /><b>Message:</b> ' . $message . '<br /><b>Status:</b> <span style=\'color: red;\'>Failed</span>' );

			return new WP_Error( 'missing-argument', __( 'There is a problem with one of your arguments. Please check again.', 'messageflow' ), array(
				'status'          => 403,
				'twilio_response' => $response_body
			) );
		}
	}
}
