<?php 

class ST_Rest_Api {

	/**
	 * Spacetree version.
	 *
	 * @var string
	 */
	public $version = '1.0.0';

    protected static $_instance = null;

    /**
	 * Main Spacetree Instance.
	 *
	 * Ensures only one instance of Spacetree is loaded or can be loaded.
	 *
	 * @since 1.0
	 * @static
	 * @see ST()
	 * @return Spacetree - Main instance.
	 */
	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

    public function __construct() {
        $this->includes();
    }

    public function includes(){
        require_once ST_ABSPATH . "includes/rest-api/private.php";
    }

    public function spacetree_api_permissions_callback() {
        // Check if the authorization header is present in the request
        $auth_header = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
        $host_header = $_SERVER['HTTP_X_HOST'] ?? '';

    
        if (empty($auth_header)) {
            return new WP_Error('rest_forbidden', __('Authorization header is missing.'), array('status' => 401));
        }
    
        // Extract the token from the authorization header
        $token = str_replace('Bearer ', '', $auth_header);
    
        // Perform token verification (replace this with your actual token verification logic)
        if (!$this->verify_token($token)) {
            return new WP_Error('rest_forbidden', __('Invalid or expired token.'), array('status' => 401));
        }

        if (empty($host_header)) {
            return new WP_Error('rest_forbidden', __('Host is missing.'), array('status' => 401));
        }
    
        // Return true if the token is valid
        return true;
    }
    
    // Function to verify the token (replace this with your actual token verification logic)
    private function verify_token($token) {
        // Implement your token verification logic here
        // For example, check if the token is valid and not expired
        // Return true if the token is valid, otherwise return false
        // Define your option key
        $option_key = 'st_site-unique-identity';

        // Get the value of the option
        $option_value = get_option($option_key, false);
        
        if(!$option_value){
            return false;
        }
        // Check if the option exists
        if ($option_value && $option_value !== false) {
            
            if( $option_value === $token ) {
                return true;
            } else {
                wp_send_json([
                    "error" => (object) [
                        "message" => "It looks like site verification id is invalid",
                        "status"    => 401,
                        "code"      => "ERR::wrong-params",
                        "res_type"  => "warning" 
                    ]
                ]);
            }
            
        } else {
            return new WP_Error('rest_forbidden', __('Identity token is missing by referer.'), array('status' => 401));
        }

    }



}

return ST_Rest_Api::instance();