<?php

namespace FleekDash\Core\Search;

/**
 * Trait for managing search index hooks
 */
trait SearchHooks
{
    /**
     * Initialize WordPress hooks for automatic indexing
     */
    protected function initHooks()
    {
        // Background indexing cron jobs
        \add_action('fleekdash_search_background_index', [$this, 'runBackgroundIndex']);
        \add_action('fleekdash_search_incremental_index', [$this, 'runIncrementalIndex']);

        // Real-time indexing hooks
        \add_action('save_post', [$this, 'indexPostOnSave'], 10, 3);
        \add_action('delete_post', [$this, 'removePostFromIndex']);
        \add_action('user_register', [$this, 'indexUserOnRegister']);
        \add_action('profile_update', [$this, 'indexUserOnUpdate']);
        \add_action('delete_user', [$this, 'removeUserFromIndex']);
        \add_action('wp_create_nav_menu', [$this, 'indexMenuOnCreate']);
        \add_action('wp_update_nav_menu', [$this, 'indexMenuOnUpdate']);
        \add_action('wp_delete_nav_menu', [$this, 'removeMenuFromIndex']);

        // Custom cron interval
        \add_filter('cron_schedules', [$this, 'addCustomCronInterval']);

        // Admin hooks
        \add_action('admin_init', [$this, 'maybeRunInitialIndex']);
        \add_action('wp_ajax_fleekdash_rebuild_search_index', [$this, 'ajaxRebuildIndex']);
    }

    /**
     * Index single post on save
     */
    public function indexPostOnSave($post_id, $post)
    {
        if (\wp_is_post_autosave($post_id) || \wp_is_post_revision($post_id)) {
            return;
        }

        if ($post->post_status !== 'publish') {
            $this->removePostFromIndex($post_id);
            return;
        }

        \wp_schedule_single_event(\time() + 5, 'fleekdash_index_single_post', [$post_id]);
        \add_action('fleekdash_index_single_post', [$this, 'indexSinglePost']);
        $this->cacheService->clearObjectCache('post', $post_id);
    }

    /**
     * Index user on registration
     */
    public function indexUserOnRegister($user_id)
    {
        \wp_schedule_single_event(\time() + 5, 'fleekdash_index_single_user', [$user_id]);
        \add_action('fleekdash_index_single_user', [$this, 'indexSingleUser']);
    }

    /**
     * Index user on profile update
     */
    public function indexUserOnUpdate($user_id)
    {
        $this->indexSingleUser($user_id);
        $this->cacheService->clearObjectCache('user', $user_id);
    }

    /**
     * Index menu on create/update
     */
    public function indexMenuOnCreate($menu_id)
    {
        $this->indexSingleMenu($menu_id);
    }

    public function indexMenuOnUpdate($menu_id)
    {
        $this->indexSingleMenu($menu_id);
        $this->cacheService->clearObjectCache('menu', $menu_id);
    }

    /**
     * AJAX handler for manual index rebuild
     */
    public function ajaxRebuildIndex()
    {
        \check_ajax_referer('fleekdash_admin', 'nonce');
        if (!\current_user_can('manage_options')) {
            \wp_die('Insufficient permissions');
        }
        $this->scheduleImmediateIndex();
        \wp_send_json_success(['message' => 'Search index rebuild scheduled.', 'scheduled' => true]);
    }

    /**
     * Add custom 15-minute cron interval
     */
    public function addCustomCronInterval($schedules)
    {
        $schedules['fleekdash_15min'] = [
            'interval' => 15 * \MINUTE_IN_SECONDS,
            'display' => \__('Every 15 Minutes', 'fleekdash')
        ];
        return $schedules;
    }
}
