<?php
/**
 * The request to the EDD Services API.
 *
 * @package   EDD\Pro\Utilities\Services
 * @copyright Copyright (c) 2025, Sandhills Development, LLC
 * @license   https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since     3.6.2
 */

namespace EDD\Pro\Utilities\Services;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

/**
 * Request class
 *
 * @since 3.6.2
 */
class Request {

	/**
	 * The API URL.
	 *
	 * @since 3.6.2
	 * @var string
	 */
	private const API_URL = 'https://services.easydigitaldownloads.com/';

	/**
	 * The endpoint to request.
	 *
	 * @since 3.6.2
	 * @var string
	 */
	private $endpoint;

	/**
	 * The request arguments.
	 *
	 * @since 3.6.2
	 * @var array
	 */
	private $args;

	/**
	 * The response from the API request.
	 *
	 * @since 3.6.2
	 * @var Response
	 */
	private $response;

	/**
	 * Constructor.
	 *
	 * @since 3.6.2
	 * @param string $endpoint The endpoint to request.
	 * @return void
	 */
	public function __construct( string $endpoint, array $args = array() ) {
		$this->endpoint = $endpoint;
		$this->args     = $args;
	}

	/**
	 * Make the API request.
	 *
	 * @since 3.6.2
	 * @return void
	 */
	public function make_request() {
		if ( empty( $this->get_site_license_key() ) ) {
			$this->response = new Response(
				new \WP_Error(
					'missing_license_key',
					__( 'A license key is required to make this request.', 'easy-digital-downloads' ),
					array( 'status' => 401 )
				)
			);
			return;
		}

		$request = wp_remote_request(
			esc_url_raw( $this->build_request_url() ),
			$this->get_request_args()
		);

		$this->response = new Response( $request );
	}

	/**
	 * Get the response from the API request.
	 *
	 * @since 3.6.2
	 * @return Response
	 */
	public function get_response() {
		return $this->response;
	}

	/**
	 * Build the request URL.
	 *
	 * @since 3.6.2
	 * @return string
	 */
	private function build_request_url() {
		return trailingslashit( self::API_URL ) . $this->endpoint;
	}

	/**
	 * Build the request headers.
	 *
	 * @since 3.6.2
	 * @return array
	 */
	private function get_request_args() {
		return array(
			'headers'            => $this->get_headers(),
			'timeout'            => $this->args['timeout'] ?? 15,
			'sslverify'          => true,
			'user-agent'         => $this->get_user_agent(),
			'reject_unsafe_urls' => true,
			'method'             => $this->args['method'] ?? 'GET',
			'body'               => $this->args['body'] ?? array(),
		);
	}

	/**
	 * Get the request headers.
	 *
	 * @since 3.6.2
	 * @return array
	 */
	private function get_headers() {
		$headers = array(
			'X-License-Key' => $this->get_site_license_key(),
		);

		if ( isset( $this->args['headers'] ) ) {
			$headers = wp_parse_args( $this->args['headers'], $headers );
		}

		return $headers;
	}

	/**
	 * Get the site license key.
	 *
	 * @since 3.6.2
	 * @return string
	 */
	private function get_site_license_key() {
		$license_key = ! empty( $this->args['license_key'] ) ? $this->args['license_key'] : trim( get_site_option( 'edd_pro_license_key' ) );

		return sanitize_text_field( $license_key );
	}

	/**
	 * Gets the default user agent string.
	 *
	 * @since 3.6.2
	 * @return string
	 */
	private function get_user_agent() {
		$user_agent = array(
			'WordPress/' . get_bloginfo( 'version' ),
			'EDDPro/' . EDD_VERSION,
			get_bloginfo( 'url' ),
		);

		return implode( '; ', $user_agent );
	}
}
