<?php
/**
 * EverestForms Brevo API Class.
 *
 * @package EverestForms\Pro\Addons\Brevo\API\API
 * @since   1.7.7
 */

namespace EverestForms\Pro\Addons\Brevo\API;

defined( 'ABSPATH' ) || exit;


/**
 * Class EVF_Sendinblue.
 */
class API {
	/**
	 * API key.
	 *
	 * @var String
	 */
	private $api_key;

	/**
	 * API end point.
	 *
	 * @var string
	 */
	private $endpoint = 'https://api.sendinblue.com/v3/';

	/**
	 * Timeout.
	 */
	const TIMEOUT = 30;

	/**
	 * SSL Verification
	 *
	 * @var boolean
	 */
	public $verify_ssl = true;

	/**
	 * Create a new instance
	 *
	 * @param string $api_key Your Sendinblue API key.
	 */
	public function __construct( $api_key ) {
		$this->api_key = $api_key;
	}

	/**
	 * Validate API Token.
	 *
	 * @since 1.7.7
	 *
	 * @return bool
	 */
	public function validate_api_key() {

		$is_valid_key = false;

		$account = $this->get_account();

		if ( isset( $account['email'] ) && ! empty( $account['email'] ) ) {
			$is_valid_key = true;
		}

		return $is_valid_key;
	}

	/**
	 * Get account.
	 *
	 * @since 1.7.7
	 */
	public function get_account() {
		return $this->make_request( 'account/', array(), 'GET' );
	}

	/**
	 * Send request to Sendinblue API.
	 *
	 * @param String  $path API Path.
	 * @param Array   $body Body.
	 * @param String  $method Method.
	 * @param  Integer $form_id The form id.
	 * @param Integer $entry_id The entry id.
	 */
	public function make_request( $path, $body, $method, $form_id = '', $entry_id = '' ) {

		$response     = array();
		$query_args   = array(
			'limit'  => 50,
			'offset' => 0,
		);
		$query_string = http_build_query( $query_args );
		$api_url      = $this->endpoint . $path . '?' . $query_string;
		$args         = array(
			'method'    => $method,
			'headers'   => array(
				'accept'       => 'application/json',
				'content-type' => 'application/json',
				'api-key'      => $this->api_key,
			),
			'body'      => empty( $body ) ? array() : wp_json_encode( $body ),
			'sslverify' => $this->verify_ssl,
			'timeout'   => self::TIMEOUT,
		);

		switch ( $method ) {
			case 'GET':
				$raw_response = wp_remote_get( $api_url, $args );
				break;
			case 'POST':
				$raw_response = wp_remote_post( $api_url, $args );
				/**
				 * Action to track the api after submission.
				 *
				 * @since 1.7.8
				 */
				do_action( 'evf_track_api_logs', $form_id, $entry_id, 'brevo', $body, $raw_response );
				break;
			default:
				$raw_response = wp_remote_request( $api_url, $args );
				break;
		}

		if ( is_wp_error( $response ) ) {
			return $raw_response;
		}

		return json_decode( wp_remote_retrieve_body( $raw_response ), true );
	}

	/**
	 * Get all lists.
	 *
	 * @since 1.7.7
	 */
	public function get_lists() {
		return $this->make_request( 'contacts/lists', array(), 'GET' );
	}

	/**
	 * Get all attributes.
	 *
	 * @since 1.7.7
	 */
	public function get_attributes() {
		return $this->make_request( 'contacts/attributes', array(), 'GET' );
	}

	/**
	 * Get all SMTP templates.
	 *
	 * @since 1.0.1
	 */
	public function get_smtp_templates() {
		return $this->make_request( 'smtp/templates', array(), 'GET' );
	}
}
