<?php

/**
 * Filter Taxonomy Template
 * 
 * This template displays a taxonomy filter for the property search.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/partials/filter-taxonomy.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/partials/filter-taxonomy.php
 * 3. Modify the copied file to customize the taxonomy filter display
 * 
 * @package     Havenlytics
 * @subpackage  Templates/partials
 * @since       2.0.0
 */

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

// Prefix all variables with hvnly_ to avoid global namespace conflicts
$hvnly_taxonomy = $args['taxonomy'] ?? '';
$hvnly_terms = $args['terms'] ?? [];
$hvnly_current_filters = $args['current_filters'] ?? [];
$hvnly_placeholder = $args['placeholder'] ?? '';
$hvnly_input_name = $args['input_name'] ?? '';

if (empty($hvnly_terms) || empty($hvnly_taxonomy)) {
    return;
}

// Check if we have a ViewModel to determine selection - FIXED: Use filter_input for security
$hvnly_is_term_selected = function ($term_slug, $taxonomy, $current_filters) use ($hvnly_taxonomy) {
    $selected = false;

    if (isset($current_filters[$taxonomy]) && is_array($current_filters[$taxonomy])) {
        $selected = in_array($term_slug, $current_filters[$taxonomy]);
    }

    // Also check URL parameters for backward compatibility - FIXED: Use filter_input
    if (!$selected) {
        $hvnly_url_terms_raw = filter_input(INPUT_GET, $taxonomy, FILTER_UNSAFE_RAW);
        if ($hvnly_url_terms_raw) {
            $hvnly_url_terms = is_array($hvnly_url_terms_raw) ? $hvnly_url_terms_raw : explode(',', $hvnly_url_terms_raw);
            $hvnly_url_terms = array_map('sanitize_text_field', $hvnly_url_terms);
            $selected = in_array($term_slug, $hvnly_url_terms);
        }
    }

    return $selected;
};
?>

<div class="hvnly-property-form-item hvnly-property-tax-multichebox">
    <input type="text" class="hvnly-property-form-input-multiselect hvnly-tax-search"
        placeholder="<?php echo esc_attr($hvnly_placeholder); ?>"
        data-taxonomy="<?php echo esc_attr($hvnly_taxonomy); ?>">

    <div class="hvnly-property-taxonomyDropdown-items">
        <!-- Selected items container -->
        <div class="hvnly-property-selected-items-container-tags"></div>
        <ul>
            <?php foreach ($hvnly_terms as $hvnly_term): ?>
                <li id="<?php echo esc_attr($hvnly_taxonomy . '-' . $hvnly_term->term_id); ?>">
                    <label class="hvnly-property-dropdown-checkbox">
                        <input type="checkbox" name="<?php echo esc_attr($hvnly_input_name); ?>"
                            value="<?php echo esc_attr($hvnly_term->slug); ?>"
                            data-term-id="<?php echo esc_attr($hvnly_term->term_id); ?>"
                            <?php checked($hvnly_is_term_selected($hvnly_term->slug, $hvnly_taxonomy, $hvnly_current_filters)); ?>>
                        <span class="hvnly-property-custom-mlt-checkbox"></span>
                        <span class="hvnly-property-checkbox-mlt-label"><?php echo esc_html($hvnly_term->name); ?></span>
                    </label>
                </li>
            <?php endforeach; ?>

            <?php if (!empty($hvnly_terms)): ?>
                <div class="hvnly-property-dropdown-reset"><?php esc_html_e('Reset', 'havenlytics'); ?></div>
            <?php endif; ?>
        </ul>
    </div>
    <div class="hvnly-property-dropdown-tags-close"><span class="dashicons dashicons-no-alt"></span></div>
</div>