<?php
/**
 * Plugin Settings REST API Endpoint
 *
 * @package HvnlyNab\Api\Type\Settings
 * @since 2.0.0
 */

namespace HvnlyNab\Api\Type\Settings;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class PluginSettingsAPI
 *
 * Handles REST API endpoints for plugin settings management.
 *
 * @package HvnlyNab\Api\Type\Settings
 * @since 2.0.0
 */
class PluginSettingsAPI {

	/**
	 * Storage key for WordPress options.
	 *
	 * @var string
	 */
	private $storage_key = 'hvnly_plugin_settings';

	/**
	 * REST API namespace.
	 *
	 * @var string
	 */
	private $namespace = 'hvnlynab/v1';

	/**
	 * REST API route base.
	 *
	 * @var string
	 */
	private $route_base = 'plugin-settings';

	/**
	 * Valid settings groups.
	 *
	 * @var array
	 */
	private $valid_groups = array( 'general', 'design', 'search', 'properties', 'shortcodes', 'property-details', 'preloader', 'map' );

	/**
	 * Boolean fields requiring proper conversion.
	 *
	 * @var array
	 */
	private $boolean_fields = array(
		'hvnly_hideTopSearchTitle',
		'hvnly_hideTopSearchBar',
		'hvnly_hideLeftSearchBar',
		'hvnly_enableAjaxLoadMore',
		'hvnly_EnableBreadcrumbs',
		'hvnly_EnableBackToTop',
		'hvnly_EnablePrintButton',
		'hvnly_EnableSaveProperty',
		'hvnly_EnableShareButton',
		'hvnly_EnabledCurrencyFormat',
		'hvnly_EnabledGutenbergEditor',
		'hvnly_EnabledSocialShare',
		// Preloader boolean fields
		'hvnly_enable_preloader',
		'hvnly_preloader_top_search',
		'hvnly_preloader_filter_sidebar',
		'hvnly_preloader_property_grid',
		'hvnly_preloader_property_list',
		'hvnly_preloader_view_controls',
		'hvnly_preloader_load_more',
		'hvnly_preloader_pagination',
	);

	/**
	 * Constructor.
	 * Initializes REST API hooks.
	 */
	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'routes' ) );
	}

	/**
	 * Register REST API routes.
	 */
	public function routes() {
		// GET all settings.
		register_rest_route(
			$this->namespace,
			'/' . $this->route_base,
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'get_all_settings' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);

		// SPECIFIC ROUTES - Must be registered BEFORE parameterized routes.
		register_rest_route(
			$this->namespace,
			'/' . $this->route_base . '/save-all',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'save_all_settings' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->route_base . '/reset-all',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'reset_all_settings' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);
        
        // Route for syncing view type (GRID/LIST)
        register_rest_route(
            $this->namespace,
            '/' . $this->route_base . '/sync-view-type',
            array(
                'methods'             => 'POST',
                'callback'            => array($this, 'sync_view_type'),
                'permission_callback' => array($this, 'check_permission'),
            )
        );
        
        // Route for syncing AJAX Load More status with Preloader
        register_rest_route(
            $this->namespace,
            '/' . $this->route_base . '/sync-ajax-load-more',
            array(
                'methods'             => 'POST',
                'callback'            => array($this, 'sync_ajax_load_more'),
                'permission_callback' => array($this, 'check_permission'),
            )
        );
        
		// PARAMETERIZED ROUTES.
		register_rest_route(
			$this->namespace,
			'/' . $this->route_base . '/(?P<group>[a-z-]+)',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'get_settings_by_group' ),
				'permission_callback' => array( $this, 'check_permission' ),
				'args'                => array(
					'group' => array(
						'required'          => true,
						'validate_callback' => array( $this, 'validate_group' ),
					),
				),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->route_base . '/(?P<group>[a-z-]+)',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'update_settings_group' ),
				'permission_callback' => array( $this, 'check_permission' ),
				'args'                => array(
					'group' => array(
						'required'          => true,
						'validate_callback' => array( $this, 'validate_group' ),
					),
				),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->route_base . '/(?P<group>[a-z-]+)/reset',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'reset_settings_group' ),
				'permission_callback' => array( $this, 'check_permission' ),
			)
		);
	}

	/**
	 * Validate settings group parameter.
	 *
	 * @param string $param The group parameter to validate.
	 * @return bool|WP_Error True if valid, WP_Error otherwise.
	 */
	public function validate_group( $param ) {
		if ( in_array( $param, $this->valid_groups, true ) ) {
			return true;
		}

		return new WP_Error(
			'invalid_group',
			sprintf(
				/* translators: 1: Invalid group name, 2: List of valid group names */
				__( 'Invalid settings group "%1$s". Valid groups: %2$s', 'havenlytics' ),
				$param,
				implode( ', ', $this->valid_groups )
			),
			array( 'status' => 400 )
		);
	}

	/**
	 * Check permissions for REST API requests.
	 *
	 * @param WP_REST_Request|null $request The request object.
	 * @return bool True if user has permission, false otherwise.
	 */
	public function check_permission( $request = null ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}

		if ( $request && in_array( $request->get_method(), array( 'POST', 'PUT', 'DELETE', 'PATCH' ), true ) ) {
			$nonce = $request->get_header( 'X-WP-Nonce' );
			if ( ! $nonce || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
				return false;
			}
		}

		return true;
	}

	/**
	 * Get all plugin settings.
	 *
	 * @return WP_REST_Response Response object containing all settings.
	 */
	public function get_all_settings() {
		$settings      = get_option( $this->storage_key, array() );
		$has_defaults  = false;
		$needs_update  = false;

		if ( empty( $settings ) || ! is_array( $settings ) ) {
			if ( class_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData' ) ) {
				$settings     = DefaultSettingsData::get_default_settings();
				$has_defaults = true;
				$needs_update = true;
			} else {
				$settings = array();
			}
		}

		// Ensure search fields exist.
		if ( ! isset( $settings['search'] ) || ! is_array( $settings['search'] ) ) {
			$settings['search'] = array();
			$needs_update       = true;
		}

		// Ensure hvnly_search_fields exists and has defaults if empty.
		if ( empty( $settings['search']['hvnly_search_fields'] ) ) {
			if ( class_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData' ) ) {
				$default_search = DefaultSettingsData::get_default_search_settings();
				if ( isset( $default_search['hvnly_search_fields'] ) ) {
					$settings['search']['hvnly_search_fields'] = $default_search['hvnly_search_fields'];
					$needs_update = true;
				}
			}
		}

		// Ensure preloader settings exist with defaults if empty.
		if ( ! isset( $settings['preloader'] ) || ! is_array( $settings['preloader'] ) || empty( $settings['preloader'] ) ) {
			if ( class_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData' ) ) {
				$default_preloader = DefaultSettingsData::get_default_settings();
				if ( isset( $default_preloader['preloader'] ) ) {
					$settings['preloader'] = $default_preloader['preloader'];
				} else {
					$settings['preloader'] = DefaultSettingsData::get_default_preloader_settings();
				}
				$needs_update = true;
			} else {
				$settings['preloader'] = array();
			}
		}

		// Save if we made changes.
		if ( $needs_update ) {
			update_option( $this->storage_key, $settings );
		}

		if ( class_exists( 'HvnlyNab\Api\Type\Settings\SettingsSchema' ) ) {
			$settings = SettingsSchema::merge_with_defaults( $settings );
		}

		return rest_ensure_response(
			array(
				'success'      => true,
				'data'         => $settings,
				'has_defaults' => $has_defaults,
			)
		);
	}

	/**
	 * Get settings by group
	 *
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_settings_by_group($request) {
		$group = $request->get_param('group');
		$settings = get_option($this->storage_key, []);
		
		if (!is_array($settings)) {
			$settings = [];
		}
		
		// Special handling for search group - ensure fields exist
		if ($group === 'search') {
			if (!isset($settings['search']) || !is_array($settings['search'])) {
				$settings['search'] = [];
			}
			
			// Ensure hvnly_search_fields exist (left sidebar filter fields)
			if (empty($settings['search']['hvnly_search_fields'])) {
				if (class_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData')) {
					$default_search = DefaultSettingsData::get_default_search_settings();
					if (isset($default_search['hvnly_search_fields'])) {
						$settings['search']['hvnly_search_fields'] = $default_search['hvnly_search_fields'];
						update_option($this->storage_key, $settings);
					}
				}
			}
			
			// Ensure hvnly_top_search_fields exist (top search additional fields)
			if (empty($settings['search']['hvnly_top_search_fields'])) {
				if (class_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData')) {
					$default_search = DefaultSettingsData::get_default_search_settings();
					if (isset($default_search['hvnly_top_search_fields'])) {
						$settings['search']['hvnly_top_search_fields'] = $default_search['hvnly_top_search_fields'];
						update_option($this->storage_key, $settings);
					}
				}
			}
		}
		
		// Special handling for preloader group - ensure defaults exist
		if ($group === 'preloader') {
			if (!isset($settings['preloader']) || !is_array($settings['preloader']) || empty($settings['preloader'])) {
				if (class_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData')) {
					$settings['preloader'] = DefaultSettingsData::get_default_preloader_settings();
					update_option($this->storage_key, $settings);
				}
			}
		}
		// Special handling for map group - ensure defaults exist
		if ($group === 'map') {
			if (!isset($settings['map']) || !is_array($settings['map']) || empty($settings['map'])) {
				if (class_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData')) {
					$settings['map'] = DefaultSettingsData::get_default_map_settings();
					update_option($this->storage_key, $settings);
				}
			}
		}
		// If settings exist for this group, return them
		if (isset($settings[$group])) {
			return rest_ensure_response([
				'success' => true,
				'data' => $settings[$group]
			]);
		}
		
		// No settings exist, return defaults
		$default_data = [];
		
		if (class_exists('HvnlyNab\Api\Type\Settings\SettingsSchema')) {
			$default_data = SettingsSchema::get_default_group($group);
		}
		
		if (empty($default_data) && class_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData')) {
			$method = 'get_default_' . str_replace('-', '_', $group) . '_settings';
			if (method_exists('HvnlyNab\Api\Type\Settings\DefaultSettingsData', $method)) {
				$default_data = DefaultSettingsData::$method();
			}
		}
		
		if (!empty($default_data)) {
			return rest_ensure_response([
				'success' => true,
				'data' => $default_data,
			]);
		}
		
		return new WP_Error('invalid_group', 'Invalid settings group', ['status' => 400]);
	}

	/**
	 * Update settings by group
	 *
	 * @param WP_REST_Request $request
	 * @return WP_REST_Response|WP_Error
	 */
	public function update_settings_group($request) {
		$group = $request->get_param('group');
		$params = $request->get_json_params();
		
		if (empty($params)) {
			return new WP_Error('invalid_data', 'Settings data is required', ['status' => 400]);
		}
		
		// Convert boolean values properly
		foreach ($params as $key => $value) {
			if (in_array($key, $this->boolean_fields, true)) {
				if (is_bool($value)) {
					$params[$key] = $value;
				} elseif (is_string($value)) {
					$params[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
				} else {
					$params[$key] = (bool) $value;
				}
			}
		}
		
		$all_settings = get_option($this->storage_key, []);
		if (!is_array($all_settings)) {
			$all_settings = [];
		}
		
		// For search group, merge the fields properly
		if ($group === 'search') {
			if (!isset($all_settings[$group]) || !is_array($all_settings[$group])) {
				$all_settings[$group] = [];
			}
			
			// Check if AJAX Load More status changed
			$old_ajax_load_more = isset($all_settings[$group]['hvnly_enableAjaxLoadMore']) 
			    ? (bool) $all_settings[$group]['hvnly_enableAjaxLoadMore'] 
			    : true;
			$new_ajax_load_more = isset($params['hvnly_enableAjaxLoadMore']) 
			    ? (bool) $params['hvnly_enableAjaxLoadMore'] 
			    : $old_ajax_load_more;
			
			// Handle hvnly_search_fields (left sidebar filter fields)
			if (isset($params['hvnly_search_fields'])) {
				$all_settings[$group]['hvnly_search_fields'] = $params['hvnly_search_fields'];
			}
			
			// Handle hvnly_top_search_fields (top search additional fields)
			if (isset($params['hvnly_top_search_fields'])) {
				$all_settings[$group]['hvnly_top_search_fields'] = $params['hvnly_top_search_fields'];
			}
			
			// Merge other fields (non-array fields)
			foreach ($params as $key => $value) {
				if (!in_array($key, ['hvnly_search_fields', 'hvnly_top_search_fields'])) {
					$all_settings[$group][$key] = $value;
				}
			}
			
			// If AJAX Load More status changed, sync with preloader
			if ($old_ajax_load_more !== $new_ajax_load_more) {
				$settings_manager = \HvnlyNab\Core\SettingsManager::get_instance();
				$settings_manager->sync_ajax_load_more_with_preloader($new_ajax_load_more);
				
				// Reload settings to get updated preloader values
				$updated_settings = get_option($this->storage_key, array());
				if (isset($updated_settings['preloader'])) {
					$all_settings['preloader'] = $updated_settings['preloader'];
				}
			}
        } elseif ($group === 'preloader') {
            // Track changes for syncing - store old values before updating
            $old_grid = isset($all_settings['preloader']['hvnly_preloader_property_grid']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_property_grid'] 
                : false;
            $old_list = isset($all_settings['preloader']['hvnly_preloader_property_list']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_property_list'] 
                : false;
            
            // Track Load More and Pagination changes
            $old_load_more = isset($all_settings['preloader']['hvnly_preloader_load_more']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_load_more'] 
                : false;
            $old_pagination = isset($all_settings['preloader']['hvnly_preloader_pagination']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_pagination'] 
                : false;
            
            // For preloader group, initialize if not exists
            if (!isset($all_settings[$group]) || !is_array($all_settings[$group])) {
                $all_settings[$group] = [];
            }
            
            // Update all preloader settings
            foreach ($params as $key => $value) {
                // Cast min_display to integer
                if ($key === 'hvnly_preloader_min_display') {
                    $all_settings[$group][$key] = (int) $value;
                } else {
                    $all_settings[$group][$key] = $value;
                }
            }
            
            // Get new values after update
            $new_grid = isset($all_settings['preloader']['hvnly_preloader_property_grid']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_property_grid'] 
                : false;
            $new_list = isset($all_settings['preloader']['hvnly_preloader_property_list']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_property_list'] 
                : false;
            $new_load_more = isset($all_settings['preloader']['hvnly_preloader_load_more']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_load_more'] 
                : false;
            $new_pagination = isset($all_settings['preloader']['hvnly_preloader_pagination']) 
                ? (bool) $all_settings['preloader']['hvnly_preloader_pagination'] 
                : false;
            
            // Sync GRID/LIST with Search Property settings
            if (($new_grid && !$old_grid) || ($new_list && !$old_list)) {
                $new_view_type = $new_grid ? 'GRID' : 'LIST';
                $settings_manager = \HvnlyNab\Core\SettingsManager::get_instance();
                $settings_manager->sync_property_view_type($new_view_type);
                
                // After sync, reload search settings to keep them in sync
                $updated_settings = get_option($this->storage_key, array());
                if (isset($updated_settings['search'])) {
                    $all_settings['search'] = $updated_settings['search'];
                }
            }
            
            // Sync Load More/Pagination with AJAX Load More setting in Search
            if (($new_load_more && !$old_load_more) || ($new_pagination && !$old_pagination)) {
                $settings_manager = \HvnlyNab\Core\SettingsManager::get_instance();
                $new_ajax_status = $new_load_more ? true : false;
                $settings_manager->sync_preloader_with_ajax_load_more($new_ajax_status);
                
                // Reload search settings to get updated AJAX Load More value
                $updated_settings = get_option($this->storage_key, array());
                if (isset($updated_settings['search'])) {
                    $all_settings['search'] = $updated_settings['search'];
                }
            }
		} elseif ($group === 'map') {
			// For map group, initialize if not exists
			if (!isset($all_settings[$group]) || !is_array($all_settings[$group])) {
				$all_settings[$group] = array();
			}
			
			// Update all map settings
			foreach ($params as $key => $value) {
				// Cast zoom to integer
				if ($key === 'hvnly_map_zoom') {
					$all_settings[$group][$key] = (int) $value;
				} else {
					$all_settings[$group][$key] = $value;
				}
			}
		}else {
			// For non-search/non-preloader groups, merge with existing settings
			if (isset($all_settings[$group]) && is_array($all_settings[$group])) {
				$all_settings[$group] = array_merge($all_settings[$group], $params);
			} else {
				$all_settings[$group] = $params;
			}
		}
		
		update_option($this->storage_key, $all_settings);
		
		do_action('hvnly_settings_updated', $group, $all_settings[$group]);
		
		return rest_ensure_response([
			'success' => true,
			'data' => $all_settings[$group],
			'message' => sprintf(
				/* translators: %s: Settings group name (e.g., General, Design, Search) */
				__('%s settings saved successfully', 'havenlytics'),
				ucfirst($group)
			)
		]);
	}

	/**
	 * Reset settings group to defaults.
	 *
	 * @param WP_REST_Request $request The request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function reset_settings_group( $request ) {
		$group = $request->get_param( 'group' );

		if ( ! in_array( $group, $this->valid_groups, true ) ) {
			return new WP_Error( 'invalid_group', 'Invalid settings group', array( 'status' => 400 ) );
		}

		$default_data = array();

		if ( class_exists( 'HvnlyNab\Api\Type\Settings\SettingsSchema' ) ) {
			$default_data = SettingsSchema::get_default_group( $group );
		}

		if ( empty( $default_data ) && class_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData' ) ) {
			$method = 'get_default_' . str_replace( '-', '_', $group ) . '_settings';
			if ( method_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData', $method ) ) {
				$default_data = DefaultSettingsData::$method();
			}
		}

		$all_settings = get_option( $this->storage_key, array() );
		if ( ! is_array( $all_settings ) ) {
			$all_settings = array();
		}

		$all_settings[ $group ] = $default_data;
		update_option( $this->storage_key, $all_settings );

		/**
		 * Fires after settings are updated.
		 *
		 * @since 2.0.0
		 *
		 * @param string $group The settings group that was reset.
		 * @param array  $data  The default settings data.
		 */
		do_action( 'hvnly_settings_updated', $group, $default_data );

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $default_data,
				'message' => sprintf(
					/* translators: %s: Settings group name */
					__( '%s settings reset to defaults', 'havenlytics' ),
					ucfirst( $group )
				),
			)
		);
	}

	/**
	 * Save all settings at once.
	 *
	 * @param WP_REST_Request $request The request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function save_all_settings( $request ) {
		$params = $request->get_json_params();

		if ( empty( $params ) ) {
			return new WP_Error( 'invalid_data', 'Settings data is required', array( 'status' => 400 ) );
		}

		if ( isset( $params['tabs'] ) && is_array( $params['tabs'] ) ) {
			$all_settings = $params['tabs'];
		} else {
			$all_settings = $params;
		}

		// Convert boolean values for all groups.
		foreach ( $all_settings as $group => $settings_data ) {
			if ( is_array( $settings_data ) ) {
				foreach ( $settings_data as $key => $value ) {
					if ( in_array( $key, $this->boolean_fields, true ) ) {
						$all_settings[ $group ][ $key ] = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
					}
				}
			}
		}

		// Validate each group if schema exists.
		if ( class_exists( 'HvnlyNab\Api\Type\Settings\SettingsSchema' ) ) {
			$validated_settings = array();
			foreach ( $this->valid_groups as $group ) {
				if ( isset( $all_settings[ $group ] ) ) {
					$validated_settings[ $group ] = SettingsSchema::validate_group( $group, $all_settings[ $group ] );
				}
			}
			$all_settings = $validated_settings;
		}

		update_option( $this->storage_key, $all_settings );

		/**
		 * Fires after all settings are updated.
		 *
		 * @since 2.0.0
		 *
		 * @param string $group The settings group identifier ('all' for all settings).
		 * @param array  $data  The complete settings data.
		 */
		do_action( 'hvnly_settings_updated', 'all', $all_settings );

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $all_settings,
				'message' => __( 'All settings saved successfully', 'havenlytics' ),
			)
		);
	}

	/**
	 * Reset all settings to defaults.
	 *
	 * @param WP_REST_Request $request The request object.
	 * @return WP_REST_Response Response object containing default settings.
	 */
	public function reset_all_settings( $request ) {
		$default_settings = array();

		if ( class_exists( 'HvnlyNab\Api\Type\Settings\SettingsSchema' ) ) {
			$default_settings = SettingsSchema::get_all_defaults();
		}

		if ( empty( $default_settings ) && class_exists( 'HvnlyNab\Api\Type\Settings\DefaultSettingsData' ) ) {
			$default_settings = DefaultSettingsData::get_default_settings();
		}

		update_option( $this->storage_key, $default_settings );

		/**
		 * Fires after all settings are reset to defaults.
		 *
		 * @since 2.0.0
		 *
		 * @param string $group The settings group identifier ('all' for all settings).
		 * @param array  $data  The default settings data.
		 */
		do_action( 'hvnly_settings_updated', 'all', $default_settings );

		return rest_ensure_response(
			array(
				'success' => true,
				'data'    => $default_settings,
				'message' => __( 'All settings reset to defaults', 'havenlytics' ),
			)
		);
	}

    /**
     * Sync property view type across settings.
     *
     * @since 2.2.0
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response|WP_Error Response object or error.
     */
    public function sync_view_type($request) {
        $params = $request->get_json_params();
        
        if (empty($params) || !isset($params['view_type'])) {
            return new WP_Error(
                'invalid_data',
                __('View type is required', 'havenlytics'),
                array('status' => 400)
            );
        }
        
        $view_type = sanitize_text_field($params['view_type']);
        $allowed_views = array('GRID', 'LIST');
        
        if (!in_array($view_type, $allowed_views, true)) {
            return new WP_Error(
                'invalid_view_type',
                __('Invalid view type. Must be GRID or LIST.', 'havenlytics'),
                array('status' => 400)
            );
        }
        
        $settings_manager = \HvnlyNab\Core\SettingsManager::get_instance();
        $synced = $settings_manager->sync_property_view_type($view_type);
        
        // Get updated settings after sync
        $all_settings = get_option('hvnly_plugin_settings', array());
        
        return rest_ensure_response(array(
            'success' => true,
            'synced' => $synced,
            'data' => array(
                'search' => isset($all_settings['search']) ? $all_settings['search'] : array(),
                'preloader' => isset($all_settings['preloader']) ? $all_settings['preloader'] : array(),
            ),
            'message' => sprintf(
                /* translators: %s: View type (GRID or LIST) */
                __('Property view synced to %s', 'havenlytics'),
                $view_type
            ),
        ));
    }
    
	/**
	 * Sync AJAX Load More status with Preloader settings.
	 *
	 * @since 2.2.0
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response|WP_Error Response object or error.
	 */
	public function sync_ajax_load_more($request) {
		$params = $request->get_json_params();
		
		if (empty($params) || !isset($params['ajax_load_more_enabled'])) {
			return new WP_Error(
				'invalid_data',
				__('AJAX Load More status is required', 'havenlytics'),
				array('status' => 400)
			);
		}
		
		$ajax_load_more_enabled = (bool) $params['ajax_load_more_enabled'];
		
		$settings_manager = \HvnlyNab\Core\SettingsManager::get_instance();
		
		// First, update the search setting directly
		$settings = get_option('hvnly_plugin_settings', array());
		if (!is_array($settings)) {
			$settings = array();
		}
		
		if (!isset($settings['search']) || !is_array($settings['search'])) {
			$settings['search'] = array();
		}
		
		// Update AJAX Load More setting
		$settings['search']['hvnly_enableAjaxLoadMore'] = $ajax_load_more_enabled;
		update_option('hvnly_plugin_settings', $settings);
		
		// Now sync with preloader based on the new value
		$synced = $settings_manager->sync_ajax_load_more_with_preloader($ajax_load_more_enabled);
		
		// Clear cache to ensure fresh data
		$settings_manager->clear_cache();
		
		// Get updated settings after sync
		$all_settings = get_option('hvnly_plugin_settings', array());
		
		// Ensure the search value is what we set
		if (isset($all_settings['search'])) {
			$all_settings['search']['hvnly_enableAjaxLoadMore'] = $ajax_load_more_enabled;
		}
		
		return rest_ensure_response(array(
			'success' => true,
			'synced' => $synced,
			'data' => array(
				'search' => isset($all_settings['search']) ? $all_settings['search'] : array(),
				'preloader' => isset($all_settings['preloader']) ? $all_settings['preloader'] : array(),
			),
			'message' => $ajax_load_more_enabled ? __('AJAX Load More enabled, Load More Preloader activated', 'havenlytics') : __('AJAX Load More disabled, Pagination Preloader activated', 'havenlytics'),
		));
	}

}