<?php

/**
 * Cache Invalidator - Handles cache invalidation for property updates
 *
 * Responsible for clearing relevant caches when property data changes.
 * Ensures users always see up-to-date property information by invalidating
 * cached search results, filters, and sidebar widgets when properties are updated.
 *
 * @package     Havenlytics
 * @subpackage  Core
 * @copyright   Copyright (c) 2025, Havenlytics
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       2.0.0
 */

namespace HvnlyNab\Core;

// Prevent direct access to WordPress files
defined('ABSPATH') || exit;

/**
 * Import required classes for cache invalidation
 * These handle specific cache clearing for different components
 */

use HvnlyNab\Frontend\ViewModels\SearchFilters;
use HvnlyNab\Frontend\ViewModels\SidebarSearchFilters;
use Throwable;

/**
 * Cache Invalidator Class
 *
 * Handles coordinated cache invalidation across all plugin components
 * when property data changes. This ensures data consistency and prevents
 * stale cache from being served to users.
 *
 * Key responsibilities:
 * - Clear search result caches when properties are updated
 * - Invalidate sidebar widget caches
 * - Clear transients with specific patterns
 * - Coordinate with main engine for complete cache clearance
 *
 * @since 2.0.0
 */
class CacheInvalidator
{
    /**
     * Invalidate all relevant caches for a property update
     *
     * Main method called when a property is created, updated, or deleted.
     * Coordinates cache clearing across all plugin components to ensure
     * data consistency and fresh content delivery.
     *
     * @param int $post_id The ID of the property post being updated
     * @return void
     */
    public static function invalidate($post_id)
    {
        /**
         * Only proceed if the updated post is a property listing
         * Prevents unnecessary cache clearing for other post types
         */
        if (get_post_type($post_id) !== 'hvnly_property') return;

        try {
            /**
             * Step 1: Clear main search filter caches
             * This ensures search results are updated with latest property data
             */
            if (class_exists(SearchFilters::class)) {
                (new SearchFilters())->clear_cache();
            }

            /**
             * Step 2: Clear sidebar search widget caches
             * Sidebar widgets often show filtered property counts and need refresh
             */
            if (class_exists(SidebarSearchFilters::class)) {
                (new SidebarSearchFilters())->clear_sidebar_cache();
            }

            /**
             * Step 3: Clear transients with search-related patterns
             * Removes cached search queries and filtered results
             */
            CacheManager::clear_transients_by_pattern('search_');
            CacheManager::clear_transients_by_pattern('sidebar_');

            /**
             * Step 4: Clear all plugin cache via main engine if available
             * Provides comprehensive cache clearance as fallback
             */
            if (function_exists('HVN') && HVNLY_NAB()->engine()) {
                HVNLY_NAB()->engine()->clear_all_cache();
            }

            /**
             * Log successful cache invalidation for debugging and monitoring - REMOVED FOR PRODUCTION
             */
            // Debug code removed for production
        } catch (Throwable $e) {
            /**
             * Handle any errors during cache invalidation gracefully
             * Logs the error but doesn't break the property update process - REMOVED FOR PRODUCTION
             */
            // Debug code removed for production
        }
    }
}