<?php
/**
 * Displays all tsProducts of a specific category, including filtering and sorting options.
 *
 * This function retrieves all products of the specified category (custom post type 'boxyfy_tsprodukte')
 * and renders them in a dynamic comparison table with extensive filtering, sorting,
 * and AJAX-based pagination.
 *
 * Features:
 * - Product filters for various attributes (price, features, deals, etc.)
 * - Dynamic sorting by price or ranking
 * - AJAX-based pagination for better usability
 * - Responsive design for mobile devices
 *
 * @package boxyfy
 * @param string $kategorie The slug or name of the product category to be displayed.
 * @return void Outputs the complete HTML structure for the product list.
 */

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

function boxyfy_anzeigen_produkte_kategorie( $kategorie ) {
    // Sanitize the category input
    $kategorie = sanitize_text_field( $kategorie );

    // Lizenzprüfung (falls Funktion vorhanden)
    if ( function_exists( 'boxyfy_is_license_valid' ) ) {
        if ( ! boxyfy_is_license_valid() ) {
            echo '<p>Keine Produktbox-Übersicht inkl. Filter gefunden. Bitte die Seite aktualisieren oder den Lizenzschlüssel-Status unter Einstellungen überprüfen.</p>';
            return;
        }
    }
    
        // Öffnen eines Wrapper-Containers, der Filter und Produkte umschließt
    echo '<div class="filter-and-products-wrapper">';

    // Auswahlmenü und Akkordeon in den Filter-Header einbetten
    echo '<!-- Sortier-Button und Dropdown -->
<div class="filter-header">

<div class="sort-container">
  <div class="sort-button-wrapper">
    <button class="sort-button" id="boxyfy-sort-toggle" aria-label="Sortierung öffnen" type="button">
	
      <!-- SVG-Icon -->
      <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M6.5 19.7499C6.31 19.7499 6.12 19.6799 5.97 19.5299L3.47 17.0299C3.18 16.7399 3.18 16.2599 3.47 15.9699C3.76 15.6799 4.24 15.6799 4.53 15.9699L6.5 17.9399L8.47 15.9699C8.76 15.6799 9.24 15.6799 9.53 15.9699C9.82 16.2599 9.82 16.7399 9.53 17.0299L7.03 19.5299C6.88 19.6799 6.69 19.7499 6.5 19.7499Z" fill="currentColor"/>
        <path d="M6.5 19.75C6.09 19.75 5.75 19.41 5.75 19V5C5.75 4.59 6.09 4.25 6.5 4.25C6.91 4.25 7.25 4.59 7.25 5V19C7.25 19.41 6.91 19.75 6.5 19.75Z" fill="currentColor"/>
        <path d="M20 8.25H12C11.59 8.25 11.25 7.91 11.25 7.5C11.25 7.09 11.59 6.75 12 6.75H20C20.41 6.75 20.75 7.09 20.75 7.5C20.75 7.91 20.41 8.25 20 8.25Z" fill="currentColor"/>
        <path d="M16 14.25H12C11.59 14.25 11.25 13.91 11.25 13.5C11.25 13.09 11.59 12.75 12 12.75H16C16.41 12.75 16.75 13.09 16.75 13.5C16.75 13.91 16.41 14.25 16 14.25Z" fill="currentColor"/>
        <path d="M14 17.25H12C11.59 17.25 11.25 16.91 11.25 16.5C11.25 16.09 11.59 15.75 12 15.75H14C14.41 15.75 14.75 16.09 14.75 16.5C14.75 16.91 14.41 17.25 14 17.25Z" fill="currentColor"/>
        <path d="M18 11.25H12C11.59 11.25 11.25 10.91 11.25 10.5C11.25 10.09 11.59 9.75 12 9.75H18C18.41 9.75 18.75 10.09 18.75 10.5C18.75 10.91 18.41 11.25 18 11.25Z" fill="currentColor"/>
      </svg>
	   <span class="sort-tslabel">' . esc_html__( 'Sortierung', 'boxyfy' ) . '</span>
    </button>
    <div class="sort-menu" id="boxyfy-sort-menu">
        <div class="sort-option" data-sort="rank_asc">' . esc_html__( 'Ranking aufsteigend', 'boxyfy' ) . '</div>
    	<div class="sort-option" data-sort="rank_desc">' . esc_html__( 'Ranking absteigend', 'boxyfy' ) . '</div>
    	<div class="sort-option" data-sort="asc">'        . esc_html__( 'Preis aufsteigend',   'boxyfy' ) . '</div>
    	<div class="sort-option" data-sort="desc">'       . esc_html__( 'Preis absteigend',    'boxyfy' ) . '</div>
    </div>
  </div>
</div>

    <button class="accordion">
    <span class="accordion-tsicon">
    <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="style=stroke">
<g id="filter-circle">
<path id="vector (Stroke)" fill-rule="evenodd" clip-rule="evenodd" d="M7.75 17.5C7.75 17.0858 7.41421 16.75 7 16.75H2C1.58579 16.75 1.25 17.0858 1.25 17.5C1.25 17.9142 1.58579 18.25 2 18.25H7C7.41421 18.25 7.75 17.9142 7.75 17.5Z" fill="currentColor"/>
<path id="vector (Stroke)_2" fill-rule="evenodd" clip-rule="evenodd" d="M16.25 6.5C16.25 6.08579 16.5858 5.75 17 5.75H22C22.4142 5.75 22.75 6.08579 22.75 6.5C22.75 6.91421 22.4142 7.25 22 7.25H17C16.5858 7.25 16.25 6.91421 16.25 6.5Z" fill="currentColor"/>
<path id="vector (Stroke)_3" fill-rule="evenodd" clip-rule="evenodd" d="M22.75 17.5C22.75 17.0858 22.4142 16.75 22 16.75H13C12.5858 16.75 12.25 17.0858 12.25 17.5C12.25 17.9142 12.5858 18.25 13 18.25H22C22.4142 18.25 22.75 17.9142 22.75 17.5Z" fill="currentColor"/>
<path id="vector (Stroke)_4" fill-rule="evenodd" clip-rule="evenodd" d="M1.25 6.5C1.25 6.08579 1.58579 5.75 2 5.75H11C11.4142 5.75 11.75 6.08579 11.75 6.5C11.75 6.91421 11.4142 7.25 11 7.25H2C1.58579 7.25 1.25 6.91421 1.25 6.5Z" fill="currentColor"/>
<path id="vector (Stroke)_5" fill-rule="evenodd" clip-rule="evenodd" d="M10 15.1499C11.2426 15.1499 12.25 16.1573 12.25 17.3999C12.25 18.6425 11.2426 19.6499 10 19.6499C8.75736 19.6499 7.75 18.6425 7.75 17.3999C7.75 16.1573 8.75736 15.1499 10 15.1499ZM13.75 17.3999C13.75 15.3288 12.0711 13.6499 10 13.6499C7.92893 13.6499 6.25 15.3288 6.25 17.3999C6.25 19.471 7.92893 21.1499 10 21.1499C12.0711 21.1499 13.75 19.471 13.75 17.3999Z" fill="currentColor"/>
<path id="vector (Stroke)_6" fill-rule="evenodd" clip-rule="evenodd" d="M14 4.1499C12.7574 4.1499 11.75 5.15726 11.75 6.3999C11.75 7.64254 12.7574 8.6499 14 8.6499C15.2426 8.6499 16.25 7.64254 16.25 6.3999C16.25 5.15726 15.2426 4.1499 14 4.1499ZM10.25 6.3999C10.25 4.32883 11.9289 2.6499 14 2.6499C16.0711 2.6499 17.75 4.32883 17.75 6.3999C17.75 8.47097 16.0711 10.1499 14 10.1499C11.9289 10.1499 10.25 8.47097 10.25 6.3999Z" fill="currentColor"/>
</g></g></svg></span>
	<span class="accordion-tslabel">' . esc_html__( 'Filter', 'boxyfy' ) . '</span>
  </button>
</div>';

    // Produktdetails aus allen Produkten sammeln
    $produkt_infos = [];

    // WP_Query für Produkte
    $args = array(
        'post_type'      => 'boxyfy_tsprodukte',
        'posts_per_page' => -1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'boxyfy_tsprodukt_kategorie',
                'field'    => 'slug',
                'terms'    => sanitize_title( $kategorie ),
            ),
        ),
        'meta_key' => 'tsprodukt_ranking',
        'orderby'  => 'meta_value_num',
        'order'    => 'ASC',
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $produkt_info = get_post_meta( get_the_ID(), 'tsprodukt_info', true );

            if ( ! empty( $produkt_info ) ) {
                foreach ( $produkt_info as $index => $info ) {
                    $display_name = isset( $info['name'] ) ? $info['name'] : 'Unbekannt';
                    $safe_name    = sanitize_title( $display_name );
                    $unit         = isset( $info['unit'] ) ? $info['unit'] : '';

                    // Prüfen ob Zahl
                    if ( isset( $info['value'] ) && preg_match( '/^\d+(?:\.\d+)?(?:\s?\w+)?$/', $info['value'] ) ) {
                        $clean_value = str_replace( ',', '.', preg_replace( '/[^\d.,]/', '', $info['value'] ) );
                        $wert        = floatval( $clean_value );
                        $produkt_infos[ $safe_name ]['type'] = 'number';

                        if ( false !== strpos( $clean_value, '.' ) ) {
                            $produkt_infos[ $safe_name ]['decimal'] = true;
                        } else {
                            $produkt_infos[ $safe_name ]['decimal'] = false;
                        }
                    } else {
                        $wert = isset( $info['value'] ) ? $info['value'] : '';
                        $produkt_infos[ $safe_name ]['type'] = 'text';
                    }

                    $produkt_infos[ $safe_name ]['unit']         = $unit;
                    $produkt_infos[ $safe_name ]['display_name'] = $display_name;
                    $produkt_infos[ $safe_name ]['values'][]     = $wert;
                }
            }
        }
        wp_reset_postdata();
    }

    // Akkordeon-Container
    echo '<div class="accordion-content">';
    echo '<div class="filter-container">';
    
    // Füge das Kreuz hinzu
    echo '<button class="close-cross" aria-label="Filter schließen">&#10005;</button>';

    echo '<div class="deal-filter-container">';
    echo '<label for="dealFilterCheckbox"><input type="checkbox" id="dealFilterCheckbox" class="deal-filter-checkbox"> Nur 🔥 Amazon-Deals anzeigen</label>';
    echo '</div>';
    
    // Listen-Container
    echo '<div class="listen-container">';
    foreach ( $produkt_infos as $safe_name => $info ) {
        $display_name = esc_html( $info['display_name'] );

        if ( 'text' === $info['type'] ) {
            // Zählungen pro Wert
            $value_counts = array_count_values( $info['values'] );
            
            echo '<div class="listen-liste">';
            echo '<strong>' . esc_html( $display_name ) . '</strong>';
            echo '<ul id="filter_' . esc_attr( $safe_name ) . '">';
            foreach ( array_unique( $info['values'] ) as $wert ) {
                $count = isset( $value_counts[ $wert ] ) ? intval( $value_counts[ $wert ] ) : 0;
                
                echo '<li>
                        <label class="filter-label">
                            <input type="checkbox" class="filter-checkbox" data-filter="' . esc_attr( $wert ) . '" data-name="' . esc_attr( $safe_name ) . '">
                            <span class="filter-text">' . esc_html( $wert ) . '</span>
                            <span class="tsfilter-count">' . (int) $count . '</span>
                        </label>
                      </li>';
            }
            echo '</ul>';
            echo '</div>';
        }
    }
    echo '</div>'; // Ende des Listen-Container

    // Slider-Container
    echo '<div class="slider-container-group">';
    foreach ( $produkt_infos as $safe_name => $info ) {
        $display_name = esc_html( $info['display_name'] );

        if ( 'number' === $info['type'] ) {
            $min_value = min( $info['values'] );
            $max_value = max( $info['values'] );

            $is_decimal = ( isset( $info['decimal'] ) && $info['decimal'] ) ? true : false;

            echo '<div class="slider-container" data-decimal="' . ( $is_decimal ? '1' : '0' ) . '">';
            echo '<span class="slider-label"><strong>' . esc_html( $display_name ) . '</strong></span>';
            echo '<div class="range-output">';
            
            if ( $is_decimal ) {
                echo '<span class="range-label" id="rangeMinValue_' . esc_attr( $safe_name ) . '" data-unit="' . esc_attr( $info['unit'] ) . '">' . esc_html( number_format( $min_value, 2, '.', '' ) ) . ' ' . esc_html( $info['unit'] ) . '</span>';
                echo '<span class="range-label" id="rangeMaxValue_' . esc_attr( $safe_name ) . '" data-unit="' . esc_attr( $info['unit'] ) . '">' . esc_html( number_format( $max_value, 2, '.', '' ) ) . ' ' . esc_html( $info['unit'] ) . '</span>';
            } else {
                echo '<span class="range-label" id="rangeMinValue_' . esc_attr( $safe_name ) . '" data-unit="' . esc_attr( $info['unit'] ) . '">' . esc_html( number_format( $min_value, 0, '', '' ) ) . ' ' . esc_html( $info['unit'] ) . '</span>';
                echo '<span class="range-label" id="rangeMaxValue_' . esc_attr( $safe_name ) . '" data-unit="' . esc_attr( $info['unit'] ) . '">' . esc_html( number_format( $max_value, 0, '', '' ) ) . ' ' . esc_html( $info['unit'] ) . '</span>';
            }
            
            echo '</div>';
            echo '<div class="range-slider">';
            echo '<input type="range" min="' . esc_attr( $min_value ) . '" max="' . esc_attr( $max_value ) . '" step="' . ( $is_decimal ? '0.01' : '1' ) . '" value="' . esc_attr( $min_value ) . '" id="minRangeSlider_' . esc_attr( $safe_name ) . '" class="slider-range">';
            echo '<input type="range" min="' . esc_attr( $min_value ) . '" max="' . esc_attr( $max_value ) . '" step="' . ( $is_decimal ? '0.01' : '1' ) . '" value="' . esc_attr( $max_value ) . '" id="maxRangeSlider_' . esc_attr( $safe_name ) . '" class="slider-range">';
            echo '</div>';
            echo '</div>';
        }
    }
    
    echo '</div>'; // Ende der Slider-Container-Gruppe

    // Suchfeld für Produktfilter
    echo '<div class="suche-container">';
    echo '<input type="text" id="produktSuche" placeholder="Suche nach Marke, Modell etc.">';
    echo '</div>';

    echo '</div>'; // Ende der Filter-Container
    
    // Zurücksetzen und Schließen-Button nebeneinander
    echo '<div class="button-group">';
	echo '<button class="reset-filter-button" id="resetFilters" type="button"
        aria-label="' . esc_attr__( 'Filter zurücksetzen', 'boxyfy' ) . '">'
    	. esc_html__( 'Zurücksetzen', 'boxyfy' ) .
    '</button>';
	echo '<button class="close-filter-button" type="button"
        aria-label="' . esc_attr__( 'Filter schließen', 'boxyfy' ) . '">'
     	. esc_html__( 'Filter schließen', 'boxyfy' ) .
    '</button>';
    echo '</div>';
    echo '</div>'; // Ende des Akkordeon-Containers

    // Produktboxen generieren
    if ( $query->have_posts() ) {
        echo '<div class="produkt-container" id="productListTop">';

        while ( $query->have_posts() ) {
            $query->the_post();

            // Initialisierung der Preis-Arrays für jede Produktbox
            $all_prices_product_box_vergleich   = array();
            $price_sources_product_box_vergleich = array();

            $produkt_info = get_post_meta( get_the_ID(), 'tsprodukt_info', true );

            $price_amazon                  = get_post_meta( get_the_ID(), 'tsprodukt_price1', true );
            $preis_uvp_produkteseite       = get_post_meta( get_the_ID(), 'tsprodukt_saving_basis', true );
			
			// Deal-Ausgaben-Schwelle
			$product_boxes_compare_deal_ausgabe_schwelle       = get_option( 'boxyfy_vergleich_boxen_deal_ausgabe_schwelle' );
            
			// Konvertiere den Amazon-Preis
            $price_amazon_float            = boxyfy_convert_price_to_float( $price_amazon );
            $preis_uvp_produkteseite_float = (float) $preis_uvp_produkteseite;
            $preis_ersparnis_prozent_vergleich = ( $price_amazon_float > 0 && $preis_uvp_produkteseite_float > 0 ) ? round( 100 - ( ( $price_amazon_float / $preis_uvp_produkteseite_float ) * 100 ) ) : 0;
            $asin          = get_post_meta( get_the_ID(), 'tsprodukt_asin_price', true );
            $ranking_value = get_post_meta( get_the_ID(), 'tsprodukt_ranking', true );
            
            // ***** STERNE-BEWERTUNG holen
            $tsproduct_compare_starrating       = get_post_meta( get_the_ID(), 'tsprodukte_star_rating', true );
            $tsproduct_compare_star_rating_size = get_post_meta( get_the_ID(), 'tsprodukte_star_rating_size', true ) ?: '14';
            $hide_rating_compare                = get_post_meta( get_the_ID(), 'tsprodukte_hide_rating', true );
            $show_rating_compare                = ( empty( $hide_rating_compare ) || '1' !== $hide_rating_compare ) ? '1' : '0';	
            $bewertung_produktboxvergleich = get_post_meta( get_the_ID(), '_tsprodukt_bewertung', true );
            $green_min  = get_option( 'boxyfy_green_rating_min' );
            $green_max  = get_option( 'boxyfy_green_rating_max' );
            $yellow_min = get_option( 'boxyfy_yellow_rating_min' );
            $yellow_max = get_option( 'boxyfy_yellow_rating_max' );
            $red_min    = get_option( 'boxyfy_red_rating_min' );
            $red_max    = get_option( 'boxyfy_red_rating_max' );
            
            $amazon_settings = get_option(
                'boxyfy_amazon_settings',
                array(
                    'partner_tag' => ''
                )
            );
            $partner_tag = isset( $amazon_settings['partner_tag'] ) ? $amazon_settings['partner_tag'] : '';
            
            // Sicherstellen, dass ASIN und Partner-Tag vorhanden sind
            if ( ! empty( $asin ) && ! empty( $partner_tag ) ) {
                $purchase_url_vergleich = 'https://www.amazon.de/dp/' . esc_attr( $asin ) . '?tag=' . esc_attr( $partner_tag ) . '&linkCode=ogi&th=1';
            } else {
                $purchase_url_vergleich = '#'; // Fallback-URL
            }

            // Sammle den Amazon-Preis
            if ( $price_amazon_float > 0 ) {
                $all_prices_product_box_vergleich[]                                    = $price_amazon_float;
                $price_sources_product_box_vergleich[ $price_amazon_float ]            = 'Amazon';
            }

            // Weitere Preise abrufen und konvertieren
            $weitere_preise_product_box_vergleich = get_post_meta( get_the_ID(), 'boxyfy_weitere_preise', true ); // Passen Sie den Metaschlüssel an
            if ( ! empty( $weitere_preise_product_box_vergleich ) && is_array( $weitere_preise_product_box_vergleich ) ) {
                foreach ( $weitere_preise_product_box_vergleich as $preis_info ) {
                    if ( isset( $preis_info['preis'] ) ) { // Stelle sicher, dass der Schlüssel 'preis' existiert
                        $current_preis = boxyfy_convert_price_to_float( $preis_info['preis'] );
                        if ( $current_preis > 0 ) {
                            $source = isset( $preis_info['shop'] ) ? $preis_info['shop'] : 'Anbieter';
                            $all_prices_product_box_vergleich[]                             = $current_preis;
                            $price_sources_product_box_vergleich[ $current_preis ]          = $source;
                        }
                    }
                }
            }

            // Niedrigsten Preis finden
            if ( ! empty( $all_prices_product_box_vergleich ) ) {
                $min_price_product_box_vergleich       = min( $all_prices_product_box_vergleich );
                $formatted_price_product_box_vergleich = number_format( $min_price_product_box_vergleich, 2, ',', '.' ) . ' €';
                $min_price_source                      = isset( $price_sources_product_box_vergleich[ $min_price_product_box_vergleich ] ) ? $price_sources_product_box_vergleich[ $min_price_product_box_vergleich ] : 'Anbieter';
            } else {
                $min_price_product_box_vergleich       = 0; // oder eine andere Fallback-Option
                $formatted_price_product_box_vergleich = 'Preis nicht verfügbar';
                $min_price_source                      = 'Anbieter';
            }
            
            // Anzahl der Angebote ermitteln
            $anzahl_angebote = count( $all_prices_product_box_vergleich );

            // Beginne die Produktbox mit den Daten-Attributen
            echo '<div class="produkt-box"';
            if ( ! empty( $produkt_info ) ) {
                foreach ( $produkt_info as $info ) {
                    $safe_name = sanitize_title( $info['name'] );
                    echo ' data-' . esc_attr( $safe_name ) . '="' . esc_attr( $info['value'] ) . '"';
                }
            }
            // Setze data-price und data-ranking auf die Produktbox
            echo ' data-price="' . esc_attr( $min_price_product_box_vergleich ) . '"';
            echo ' data-ranking="' . esc_attr( $ranking_value ) . '"';
            echo '>';
            
            // Ausgabe der Produktlabels
$ts_productsite_labels = get_post_meta( get_the_ID(), '_boxyfy_produktlabels', true );
$ts_productsite_tscustomlabel = get_post_meta( get_the_ID(), '_boxyfy_custom_label', true );

// 1) Zähler für vordefinierte Labels
$label_counter = 0;

if ( ! empty( $ts_productsite_labels ) && is_array( $ts_productsite_labels ) ) {
    foreach ( $ts_productsite_labels as $tslabel_key ) {
        
        // 2) Prüfen, ob erstes oder zweites Label
        if ( 0 === $label_counter ) {
            $pos_class = 'label22-first';
        } elseif ( 1 === $label_counter ) {
            $pos_class = 'label22-second';
        } else {
            // Falls du mal mehr als 2 vordefinierte Labels erlaubst
            $pos_class = '';
        }

        // 3) HTML-Ausgabe mit jeweiliger Klasse
		switch ( $tslabel_key ) {
    		case 'preissieger':
        		echo '<div class="produkt-label-tsboxes ' . esc_attr( $pos_class ) . '">' . esc_html__( 'Preissieger', 'boxyfy' ) . '</div>';
        		break;
    		case 'vergleichssieger':
        		echo '<div class="produkt-label-tsboxes ' . esc_attr( $pos_class ) . '">' . esc_html__( 'Vergleichssieger', 'boxyfy' ) . '</div>';
        		break;
    		case 'highlight':
        		echo '<div class="produkt-label-tsboxes ' . esc_attr( $pos_class ) . '">' . esc_html__( 'Highlight', 'boxyfy' ) . '</div>';
        		break;
    		case 'testsieger':
        		echo '<div class="produkt-label-tsboxes ' . esc_attr( $pos_class ) . '">' . esc_html__( 'Testsieger', 'boxyfy' ) . '</div>';
        		break;
		}
		
        $label_counter++;
	}
}

// Ausgabe des benutzerdefinierten Labels
if ( ! empty( $ts_productsite_tscustomlabel ) ) {
    echo '<div class="produkt-label-tsboxes label33">' . esc_html( $ts_productsite_tscustomlabel ) . '</div>';
}


            if ( ! empty( $ranking_value ) ) {
                echo '<div class="produkt-ranking-compare">' . esc_html( $ranking_value ) . '</div>';
            }

            // Deal-Ribbon
            if ( $preis_ersparnis_prozent_vergleich >= $product_boxes_compare_deal_ausgabe_schwelle ) {
                echo '<div class="produkt-deal-ribbon">
                <svg fill="currentColor" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 35.418 35.418">
<g>
    <path d="M20.948,9.891c-0.857,0.068-1.847,0.136-2.837,0.269c-1.516,0.195-3.032,0.461-4.284,1.053
        c-2.439,0.994-4.088,3.105-4.088,6.209c0,3.898,2.506,5.875,5.669,5.875c1.057,0,1.913-0.129,2.703-0.328
        c1.255-0.396,2.31-1.123,3.562-2.441c0.727,0.99,0.923,1.453,2.177,2.509c0.329,0.133,0.658,0.133,0.922-0.066
        c0.791-0.659,2.174-1.848,2.901-2.508c0.328-0.267,0.263-0.66,0.066-0.992c-0.727-0.924-1.45-1.718-1.45-3.498v-5.943
        c0-2.513,0.195-4.822-1.647-6.537c-1.518-1.391-3.891-1.916-5.735-1.916c-0.264,0-0.527,0-0.792,0
        c-3.362,0.197-6.921,1.647-7.714,5.811c-0.13,0.525,0.267,0.726,0.53,0.793l3.691,0.464c0.396-0.07,0.593-0.398,0.658-0.73
        c0.333-1.449,1.518-2.176,2.836-2.309c0.067,0,0.133,0,0.265,0c0.79,0,1.646,0.332,2.109,0.987
        c0.523,0.795,0.461,1.853,0.461,2.775L20.948,9.891L20.948,9.891z M20.223,17.749c-0.461,0.925-1.253,1.519-2.11,1.718
        c-0.131,0-0.327,0.068-0.526,0.068c-1.45,0-2.31-1.123-2.31-2.775c0-2.11,1.254-3.104,2.836-3.565
        c0.857-0.197,1.847-0.265,2.836-0.265v0.793C20.948,15.243,21.01,16.43,20.223,17.749z M35.418,26.918v0.215
        c-0.035,1.291-0.716,3.768-2.328,5.131c-0.322,0.25-0.645,0.107-0.503-0.254c0.469-1.145,1.541-3.803,1.04-4.412
        c-0.355-0.465-1.826-0.43-3.079-0.322c-0.572,0.072-1.075,0.105-1.469,0.183c-0.357,0.033-0.431-0.287-0.071-0.537
        c0.466-0.323,0.969-0.573,1.541-0.756c2.039-0.608,4.406-0.25,4.729,0.146C35.348,26.414,35.418,26.629,35.418,26.918z
         M32.016,29.428c-0.466,0.357-0.965,0.682-1.468,0.973c-3.761,2.261-8.631,3.441-12.856,3.441c-6.807,0-12.895-2.512-17.514-6.709
        c-0.396-0.324-0.073-0.789,0.393-0.539C5.549,29.5,11.709,31.26,18.084,31.26c4.013,0,8.342-0.754,12.463-2.371
        c0.285-0.104,0.608-0.252,0.895-0.356C32.087,28.242,32.661,28.965,32.016,29.428z"/>
</g>
</svg>Deal -' . esc_html( $preis_ersparnis_prozent_vergleich ) . '%</div>';
            }

            if ( has_post_thumbnail() ) {
                echo '<div class="produktvergleich-bild">' . get_the_post_thumbnail( get_the_ID(), 'full' ) . '</div>';
            }

            echo '<div class="produkt-details">';
            echo '<span class="produkt-titel"><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></span>';
            
            // =====================
// FARBE + STERNE in EINEM Wrapper
// =====================
if ( ( ! empty( $bewertung_produktboxvergleich ) || ! empty( $tsproduct_compare_starrating ) ) && '1' === $show_rating_compare ) {

    // Farbe für die Box bestimmen
    if ( $bewertung_produktboxvergleich >= $green_min && $bewertung_produktboxvergleich <= $green_max ) {
        $rating_class = 'boxyfy-rating-green';
    } elseif ( $bewertung_produktboxvergleich >= $yellow_min && $bewertung_produktboxvergleich <= $yellow_max ) {
        $rating_class = 'boxyfy-rating-yellow';
    } else {
        $rating_class = 'boxyfy-rating-red';
    }

    echo '<div class="produkt-rating-row">';

    // ►► Farbige Zahl
    if ( ! empty( $bewertung_produktboxvergleich ) ) {
        echo '<span class="produkt-bewertung-zahl ' . esc_attr( $rating_class ) . '">' .
             esc_html( $bewertung_produktboxvergleich ) .
             '</span>';
    }

    // ►► Sterne + Bewertungszahl (z. B. 4.4 / 5)
    if ( ! empty( $tsproduct_compare_starrating ) ) {
        $full  = floor( $tsproduct_compare_starrating );
        $half  = ( $tsproduct_compare_starrating - $full ) >= 0.5 ? 1 : 0;
        $empty = 5 - $full - $half;

        echo '<span class="produkt-bewertung-gruppe">';

            echo '<span class="produkt-bewertung-sterne">';
                for ( $i = 0; $i < $full; $i++ )  { echo '<i class="fas fa-star"></i>'; }
                if ( $half )                     { echo '<i class="fas fa-star-half-alt"></i>'; }
                for ( $i = 0; $i < $empty; $i++ ) { echo '<i class="far fa-star"></i>'; }
            echo '</span>';

            echo '<span class="produkt-bewertung-wert">' .
                 esc_html( $tsproduct_compare_starrating ) . '/5' .
                 '</span>';

        echo '</span>'; // .produkt-bewertung-gruppe
    }

    echo '</div>'; // .produkt-rating-row
}
            
            echo '<div class="produkt-preis">';
			// Angebotsanzahl einfügen
			if ( $anzahl_angebote > 1 ) {
    			echo '<div class="tsboxes-compare-offercount">' . sprintf( esc_html__( '%d Angebote', 'boxyfy' ), (int) $anzahl_angebote ) . '</div>';
			} elseif ( 1 === $anzahl_angebote ) {
    			echo '<div class="tsboxes-compare-offercount">' . esc_html__( '1 Angebot', 'boxyfy' ) . '</div>';
			} else {
    			echo '<div class="tsboxes-compare-offercount">' . esc_html__( 'Keine Angebote', 'boxyfy' ) . '</div>';
			}
            
            // Preisausgabe
            if ( 'Preis nicht verfügbar' === $formatted_price_product_box_vergleich ) {
                // Falls kein Preis verfügbar
                echo '<div class="price-container">
                    <strong>' . esc_html( $formatted_price_product_box_vergleich ) . '</strong>
                </div>';
                echo '</div>';
            } else {
            // Normale Ausgabe mit "ab ..." übersetzbar
				echo '<div class="price-container">
    				' . esc_html__( 'ab', 'boxyfy' ) . '&nbsp;<strong>' . esc_html( $formatted_price_product_box_vergleich ) . '</strong>
					</div>';
				echo '</div>';
            }
                     
            // Pro und Kontra Liste
            $vorteile         = get_post_meta( get_the_ID(), 'boxyfy_produkt_vorteile', true );
            $nachteile        = get_post_meta( get_the_ID(), 'boxyfy_produkt_nachteile', true );
            $show_pro_contra  = get_option( 'boxyfy_show_pro_contra', 'yes' );

            if ( 'yes' === $show_pro_contra && ( ! empty( $vorteile ) || ! empty( $nachteile ) ) ) {
                echo '<ul class="box-pro-kontra-list">';
                if ( ! empty( $vorteile ) ) {
                    foreach ( $vorteile as $vorteil ) {
                        echo '<li class="box-pro-item">';
                        echo '<img src="' . esc_url( plugin_dir_url( BOXYFY_PLUGIN_FILE ) . 'assets/images/check-icon-green.png' ) . '" alt="Vorteile Icon">';
                        echo '<span>' . esc_html( $vorteil ) . '</span>';
                        echo '</li>';
                    }
                }
                if ( ! empty( $nachteile ) ) {
                    foreach ( $nachteile as $nachteil ) {
                        echo '<li class="box-kontra-item">';
                        echo '<img src="' . esc_url( plugin_dir_url( BOXYFY_PLUGIN_FILE ) . 'assets/images/cross-icon-red.png' ) . '" alt="Nachteile Icon">';
                        echo '<span>' . esc_html( $nachteil ) . '</span>';
                        echo '</li>';
                    }
                }
                echo '</ul>';
            }

            echo '</div>'; // produkt-details
            echo '</div>'; // produkt-box
        }

        echo '</div>'; // .produkt-container

        // PAGINATION-CONTAINER (für Links unten)
        echo '<div class="tspagination-container"></div>';

        wp_reset_postdata();
    } else {
        echo '<p>Keine Produkte gefunden.</p>';
    }
        
    echo '</div>'; // Ende .filter-and-products-wrapper
}