<?php
/**
 * Template for Filter Bar with Search
 *
 * Reusable filter bar component with pill-style filter buttons and search field.
 *
 * @package     DiveWP
 * @subpackage  Templates
 * @since       2.3.0
 *
 * Variables available:
 * $filter_bar_id      - Unique ID for the bar instance (default: 'divewp-filter-bar')
 * $filters            - Array of filter definitions, each with 'key', 'label', 'count', and optional 'variant'
 * $search_placeholder - Placeholder text for the search field (default: 'Search...')
 * $show_search        - Whether to show the search field (default: true)
 * $default_filter     - Which filter key is active by default (default: 'all')
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    die( esc_html__( 'Direct access not permitted.', 'divewp-boost-site-performance' ) );
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template file with local variables only

// Ensure all variables are set with defaults
$filter_bar_id      = isset( $filter_bar_id ) ? $filter_bar_id : 'divewp-filter-bar';
$filters            = isset( $filters ) && is_array( $filters ) ? $filters : array();
$search_placeholder = isset( $search_placeholder ) ? $search_placeholder : __( 'Search...', 'divewp-boost-site-performance' );
$show_search        = isset( $show_search ) ? $show_search : true;
$default_filter     = isset( $default_filter ) ? $default_filter : 'all';

// Don't render if no filters provided
if ( empty( $filters ) ) {
    return;
}

?>
<div class="divewp-filter-bar" id="<?php echo esc_attr( $filter_bar_id ); ?>">
    <div class="divewp-filter-bar__filters">
        <?php foreach ( $filters as $filter ) :
            $key     = isset( $filter['key'] ) ? $filter['key'] : '';
            $label   = isset( $filter['label'] ) ? $filter['label'] : '';
            $count   = isset( $filter['count'] ) ? $filter['count'] : 0;
            $variant = isset( $filter['variant'] ) ? $filter['variant'] : '';

            // Build button classes
            $btn_classes = array( 'divewp-filter-bar__btn' );
            if ( $key === $default_filter ) {
                $btn_classes[] = 'divewp-filter-bar__btn--active';
            }
            if ( ! empty( $variant ) ) {
                $btn_classes[] = 'divewp-filter-bar__btn--' . sanitize_html_class( $variant );
            }
        ?>
        <button type="button" class="<?php echo esc_attr( implode( ' ', $btn_classes ) ); ?>" data-filter="<?php echo esc_attr( $key ); ?>">
            <?php echo esc_html( $label ); ?>
            <span class="divewp-filter-bar__btn-count"><?php echo esc_html( $count ); ?></span>
        </button>
        <?php endforeach; ?>
    </div>

    <?php if ( $show_search ) : ?>
    <div class="divewp-filter-bar__actions">
        <div class="divewp-filter-bar__search-wrapper">
            <span class="divewp-filter-bar__search-icon dashicons dashicons-search"></span>
            <input type="text" class="divewp-filter-bar__search" placeholder="<?php echo esc_attr( $search_placeholder ); ?>">
            <button type="button" class="divewp-filter-bar__search-clear" aria-label="<?php esc_attr_e( 'Clear search', 'divewp-boost-site-performance' ); ?>">
                <span class="dashicons dashicons-no-alt"></span>
            </button>
        </div>
    </div>
    <?php endif; ?>
</div>
