<?php 

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Shortcode for All Featured Items (Ignoring Categories)
 * Usage: [afdp_best_sellers_items]
 */
add_shortcode('afdp_best_sellers_items', 'afdp_items_best_sellers_shortcode');

function afdp_items_best_sellers_shortcode() {
    ob_start();

    // --- DYNAMIC CURRENCY LOGIC ---
    $currency_code = get_option('afdp_currency', 'USD');
    $symbols = ['USD'=>'$', 'GBP'=>'£', 'EUR'=>'€', 'INR'=>'₹', 'AED'=>'د.إ', 'BDT'=>'৳', 'CAD'=>'C$', 'SAR'=>'SR'];
    $currency = isset($symbols[$currency_code]) ? $symbols[$currency_code] : '$';

    // 1. Fetch ALL items marked as featured (no category restriction)
    $products = new WP_Query([
        'post_type'      => 'afdp_food_item', 
        'posts_per_page' => 8, // Adjust number of items to show
        'orderby'        => 'date',
        'order'          => 'DESC',
        'meta_query'     => [[
            'key'     => 'afdp_item_featured',
            'value'   => 'yes',
            'compare' => '='
        ]]
    ]);
    ?>

    <div class="afdp-best-seller-container container pb-5">

        <div class="row">
            <?php if ($products->have_posts()) : ?>
                <div class="col-lg-12">
                    <div class="afdp-menu-column">
                        <ul class="afdp-meal-list" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(450px, 1fr)); gap: 20px;">
                            <?php 
                            while ($products->have_posts()) : $products->the_post(); 
                                $price = get_post_meta(get_the_ID(), 'afdp_food_price', true); 
                            ?>
                                <li class="afdp-meal-item" style="list-style: none;">
                                    <div class="afdp-meal-thumb">
                                        <?php if (has_post_thumbnail()) : 
                                            the_post_thumbnail([80, 80], ['class' => 'afdp-img-fit']); 
                                        else : ?>
                                            <div class="afdp-thumb-placeholder">
                                                <span class="dashicons dashicons-format-image"></span>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="afdp-meal-content">
                                        <div class="afdp-meal-header">
                                            <h4 class="afdp-meal-title"><?php the_title(); ?></h4>
                                            <span class="afdp-meal-price">
                                                <?php echo $price ? esc_html($currency) . number_format(floatval($price), 2) : ''; ?>
                                            </span>
                                        </div>
                                        <div class="afdp-meal-desc">
                                            <p><?php echo esc_html(wp_trim_words(get_the_excerpt(), 12)); ?></p>
                                        </div>
                                    </div>
                                </li>
                            <?php 
                                endwhile; 
                                wp_reset_postdata(); 
                            ?>
                        </ul>
                    </div>
                </div>
            <?php else : ?>
                <div class="col-12 text-center">
                    <p>No featured items found.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <?php
    return ob_get_clean();
}