<?php
namespace BWFAN\Importers\AC\Calls;

use WFCO_Call;
use BWF_CO;

/**
 * Class Call
 *
 * This class extends the WFCO_Call class and represents a call object.
 * It provides methods to retrieve the instance, get the endpoint URL, and set required fields.
 */
class Call extends WFCO_Call {
	/**
	 * The required fields for the Call class.
	 *
	 * @var array
	 */
	protected $required_fields = array( 'api_key', 'api_url' );

	/**
	 * Represents a singleton instance of the Call class.
	 *
	 * @var null|Call
	 */
	private static $instance = null;

	/**
	 * Get the instance of the Call class.
	 * If the instance does not exist, create a new one.
	 *
	 * @return Call The instance of the Call class.
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Get the endpoint URL for the API call.
	 *
	 * @param string $api_url The base API URL.
	 * @param string $api_action The API action.
	 * @return string The complete endpoint URL.
	 */
	public function get_endpoint_url( $api_url, $api_action = '' ) {
		return $api_url . '/api/3/' . $api_action;
	}

	/**
	 * Returns the endpoint URL for making a request.
	 *
	 * @return string The endpoint URL.
	 */
	public function request_endpoint_url() {
		return $this->get_endpoint_url( $this->data['api_url'] );
	}

	/**
	 * Returns an array of request parameters for the call.
	 *
	 * @return array The request parameters.
	 */
	public function request_params() {
		return array(
			'limit'  => 100,
			'offset' => 0,
		);
	}

	/**
	 * Process the call data.
	 *
	 * This method checks if the required fields are present in the data and makes the necessary API requests.
	 *
	 * @return array
	 */
	public function process() {
		$is_required_fields_present = $this->check_fields( $this->data, $this->required_fields );
		if ( false === $is_required_fields_present ) {
			return $this->show_fields_error();
		}

		return $this->make_wp_requests(
			$this->request_endpoint_url(),
			$this->request_params(),
			array(
				'Api-Token' => $this->data['api_key'],
			),
			BWF_CO::$GET
		);
	}
}
