<?php

namespace wpsolr\core\classes\hosting_api;

use wpsolr\core\classes\extensions\managed_solr_servers\OptionManagedSolrServer;
use wpsolr\core\classes\WPSOLR_Events;

abstract class WPSOLR_Hosting_Api_WpSolr_Abstract extends WPSOLR_Hosting_Api_Abstract {

	const HOSTING_API_URL = 'https://api.wpsolr.com/v1/providers/8c25d2d6-54ae-4ff6-a478-e2c03f1e08a4/accounts/24b7729e-02dc-47d1-9c15-f1310098f93f/addons/b553e78c-3af8-4c97-9157-db77bfa6d909/hosting-manager/83e214e6-54f8-4f59-ba95-889de756ebee/licenses';

	/**
	 * Get licanse activation api url
	 */
	protected function get_license_api_url(): string {

		return apply_filters( WPSOLR_Events::WPSOLR_FILTER_ENV_HOSTING_API_URL, static::HOSTING_API_URL );
	}

	public function get_wpsolr_hosting_infos( string $subscription_number, $extra_params = [] ): array {
		$managed_solr_server = new OptionManagedSolrServer();
		$response_object     = $managed_solr_server->call_rest_activate_license(
			$this->get_license_api_url(), 'HOSTING', $subscription_number, $extra_params );

		if ( ! ( isset( $response_object ) && OptionManagedSolrServer::is_response_ok( $response_object ) ) ) {
			throw new \Exception( esc_html( OptionManagedSolrServer::get_response_error_message( $response_object ) ) );
		}

		return (array) $managed_solr_server->get_response_results( $response_object )[0];
	}

	public function get_is_wpsolr_hosting(): bool {
		return true;
	}

	public function get_label() {
		return 'WPSOLR hosting';
	}

	public function get_ui_fields_child() {

		return [
			static::FIELD_NAME_FIELDS_INDEX_LABEL_DEFAULT,
			static::FIELD_NAME_FIELDS_INDEX_LICENSE_CODE_DEFAULT,
		];
	}
}