<?php

namespace FleekDash\API\Comments;

// Load debug helpers
if (!function_exists('fleekdash_log_debug')) {
    require_once FLEEKDASH_PLUGIN_DIR . 'includes/Core/debug-helpers.php';
}

/**
 * Comments Helpers
 *
 * Utility methods for comments operations (cache management, headers)
 *
 * @package FleekDash\API\Comments
 */
class CommentsHelpers
{
    /**
     * Clear all comment-related caches
     *
     * @param int $comment_id Comment ID
     * @param int|null $post_id Optional post ID
     */
    public static function clearCommentCaches($comment_id, $post_id = null)
    {
        // Clear WordPress comment caches
        $clean_comment_cache = 'clean_comment_cache';
        $clean_comment_cache($comment_id);

        if ($post_id) {
            // Clear post comment count cache
            wp_cache_delete($post_id, 'posts');
            wp_cache_delete("comment_count_{$post_id}", 'counts');

            // Clear post-specific comment caches
            wp_cache_delete("comments_{$post_id}", 'posts');
        }

        // Clear FleekDash specific transients
        $cache_keys = [
            'fleekdash_comments_all',
            'fleekdash_comments_approved',
            'fleekdash_comments_pending',
            'fleekdash_comments_spam',
            'fleekdash_comments_trash',
            'fleekdash_comments_stats',
            'fleekdash_comments_metadata',
            'fleekdash_comments_count',
        ];

        foreach ($cache_keys as $key) {
            \delete_transient($key);
            // Also try with post ID suffix
            if ($post_id) {
                \delete_transient($key . '_' . $post_id);
            }
        }

        // Clear object cache groups
        wp_cache_flush_group('comments');
        wp_cache_flush_group('comment-queries');

        // Clear any comment-related user meta caches
        wp_cache_delete('last_comment_time', 'comment');

        // Log cache clearing for debugging
        fleekdash_log_debug("[FleekDash][debug] Cleared comment caches for comment_id: {$comment_id}, post_id: {$post_id}");
    }

    /**
     * Add no-cache headers to prevent browser/proxy caching
     *
     * @param \WP_REST_Response $response Response object
     * @return \WP_REST_Response Modified response
     */
    public static function addNoCacheHeaders($response)
    {
        $response->header('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0');
        $response->header('Pragma', 'no-cache');
        $response->header('Expires', '0');
        $response->header('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
        $response->header('ETag', '"' . md5(time() . rand()) . '"');

        // Add timestamp to help with debugging
        $response->header('X-FleekDash-Timestamp', time());

        return $response;
    }

    /**
     * Check permissions - always return true
     * Let WordPress capabilities handle access
     */
    public static function checkPermissions()
    {
        return true;
    }
}
