<?php
/**
 * Health check request.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Shift4_APM\Requests;

use Exception;
use Flo_Checkout\Gateways\Shift4_APM\Enums\Health_Check_API;
use Flo_Checkout\Gateways\Shift4_APM\Enums\Request_Method;
use Flo_Checkout\Gateways\Shift4_APM\Health_Check;
use Flo_Checkout\Gateways\Shift4_APM\Responses\Response_Factory;

defined( 'ABSPATH' ) || exit;

/**
 * Health check request.
 */
class Health_Check_Request extends Abstract_Request {

	/**
	 * Health_Check_Request constructor.
	 *
	 * @param Health_Check_API $api Type of API to health check.
	 * @param Response_Factory $response_factory Response factory.
	 * @param bool             $is_integration_mode Whether the check is for integration API.
	 */
	public function __construct(
		private readonly Health_Check_API $api,
		Response_Factory $response_factory,
		private readonly bool $is_integration_mode = true
	) {
		parent::__construct( $response_factory );
	}

	/**
	 * {@inheritDoc}
	 */
	protected function get_request_method(): Request_Method {
		return Request_Method::GET;
	}


	/**
	 * {@inheritDoc}
	 */
	protected function get_response_handler_type(): string {
		return Health_Check::SHIFT4_APM_API_RESPONSE_TYPE;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @throws Exception If API type is invalid.
	 */
	protected function get_url(): string {
		$base_urls = array();
		switch ( $this->api ) {
			case Health_Check_API::PAYMENT:
				$base_urls = array(
					'production'  => 'https://pay.sourcepayments.com/rest/health',
					'integration' => 'https://pay.int.sourcepayments.com/rest/health',
				);
				break;

			case Health_Check_API::NOTIFICATION:
				$base_urls = array(
					'production'  => 'https://notification.sourcepayments.com/rest/health',
					'integration' => 'https://notification.int.sourcepayments.com/rest/health',
				);
				break;
		}

		if ( empty( $base_urls ) ) {
			$this->log_and_throw_error(
				'Invalid API type for health check.',
				array(
					'api' => $this->api,
				),
			);
		}

		return $this->is_integration_mode ?
			$base_urls['integration'] :
			$base_urls['production'];
	}

	/**
	 * {@inheritDoc}
	 */
	protected function get_timeout(): int {
		return 20;
	}
}
