<?php
/**
 * REST URL Helper
 *
 * Provides utility functions for WordPress REST API URL generation.
 * Automatically detects permalink structure and generates correct URLs
 * for both pretty permalinks and plain permalink formats.
 *
 * @package FleekDash V2
 * @since 2.2.0
 */

namespace FleekDash\Core;

defined('ABSPATH') || exit;

class RestUrlHelper
{
    /**
     * Get the correct REST URL based on permalink structure
     * Automatically detects if site uses pretty permalinks or plain
     *
     * @param string $endpoint The REST endpoint (e.g., 'fleekdash/v1/search')
     * @param array $args Query arguments to append
     * @return string The correct REST URL for current permalink structure
     */
    public static function get_rest_url($endpoint = '', $args = array())
    {
        // Check if pretty permalinks are enabled
        $permalink_structure = \get_option('permalink_structure');
        $has_pretty_permalinks = !empty($permalink_structure);

        if ($has_pretty_permalinks) {
            // Use pretty permalinks format: /wp-json/endpoint
            $url = rest_url(FLEEKDASH_ROUTE_PREFIX . '/' . ltrim($endpoint, '/'));
        } else {
            // Use plain permalinks format: /?rest_route=/endpoint
            $home_url = \home_url('/');
            $url = add_query_arg('rest_route', '/' . ltrim($endpoint, '/'), $home_url);
        }

        // Add query arguments if provided
        if (!empty($args)) {
            $url = add_query_arg($args, $url);
        }

        return $url;
    }

    /**
     * Get REST API configuration for JavaScript
     * Returns the correct endpoint format and nonce
     *
     * @return array REST API configuration
     */
    public static function get_rest_config()
    {
        $permalink_structure = \get_option('permalink_structure');
        $has_pretty_permalinks = !empty($permalink_structure);

        return array(
            'root' => $has_pretty_permalinks ? rest_url() : \home_url('/'),
            'namespace' => FLEEKDASH_ROUTE_PREFIX,
            'nonce' => \wp_create_nonce('wp_rest'),
            'format' => $has_pretty_permalinks ? 'pretty' : 'plain',
            'endpoint_format' => $has_pretty_permalinks ? 'wp-json' : 'rest_route'
        );
    }

    /**
     * Print REST API configuration script for frontend
     * Makes the REST configuration available to JavaScript
     *
     * @return void
     */
    public static function print_rest_config()
    {
        $config = self::get_rest_config();
        echo '<script type="text/javascript">';
        echo 'window.fleekDashRestConfig = ' . \wp_json_encode($config) . ';';
        echo '</script>';
    }

    /**
     * Initialize hooks for REST config
     * Register actions to print REST config in admin and frontend
     *
     * @return void
     */
    public static function init()
    {
        \add_action('wp_head', [self::class, 'print_rest_config']);
        \add_action('admin_head', [self::class, 'print_rest_config']);
    }
}
