<?php
// If this file is called directly, abort.
if (! defined('ABSPATH')) {
    exit;
}

/**
 * Main render entry point for the Product Description widget.
 *
 * @param \GGM_Commerce_Base_Widget $widget
 */
function ggmc_product_description_render( $widget ) {
    // Get product data (includes preview product support)
    $product_data = $widget->get_product_data();

    if (!$product_data) {
        if (ggmc_is_editor_mode()) {
            echo '<p style="color: #999; font-style: italic;">Product description will appear here when product is selected.</p>';
        }
        return;
    }

    $settings = $widget->get_settings_for_display();
    $description_type = $settings['description_type'];
    $content = '';
    $show_read_more = false;
    $full_content = '';

    switch ($description_type) {
        case 'short':
            $content = $product_data->short_description ?? '';
            break;
        case 'excerpt':
            $excerpt_length = intval($settings['excerpt_length']);
            $description = $product_data->description ?? '';
            $short_description = $product_data->short_description ?? '';
            $full_description = !empty($description) ? $description : $short_description;

            // Strip tags and get word count of full description
            $stripped_full = wp_strip_all_tags($full_description);
            $full_word_count = str_word_count($stripped_full);

            // Create excerpt
            $excerpt = wp_trim_words($stripped_full, $excerpt_length, '');
            $content = $excerpt;

            // Only show read more if there's actually more content to show
            if ($settings['show_read_more'] === 'yes'
                && !empty($settings['read_more_text'])
                && $full_word_count > $excerpt_length
                && !empty($full_description)) {
                $show_read_more = true;
                $full_content = $full_description;
            }
            break;
        default:
            $content = $product_data->description ?? '';
            break;
    }

    if (empty($content)) {
        if (ggmc_is_editor_mode()) {
            echo '<p style="color: #999; font-style: italic;">No product description available.</p>';
        }
        return;
    }

    $widget_id = $widget->get_id();
    ?>
    <div class="ggmc-product-description" id="ggmc-desc-<?php echo esc_attr($widget_id); ?>">
        <?php if ($description_type === 'excerpt' && $show_read_more): ?>
            <div class="ggmc-description-excerpt">
                <?php echo esc_html($content); ?>...
                <a href="#" class="ggmc-read-more" data-widget-id="<?php echo esc_attr($widget_id); ?>">
                    <?php echo esc_html($settings['read_more_text']); ?>
                </a>
            </div>
            <div class="ggmc-description-full" style="display: none;">
                <?php echo wp_kses_post(wpautop($full_content)); ?>
                <a href="#" class="ggmc-read-less" data-widget-id="<?php echo esc_attr($widget_id); ?>">
                    <?php echo esc_html($settings['read_less_text'] ?? __('Read Less', 'ggm-commerce')); ?>
                </a>
            </div>
        <?php elseif ($description_type === 'full' || $description_type === 'short'): ?>
            <?php echo wp_kses_post(wpautop($content)); ?>
        <?php else: ?>
            <?php echo esc_html($content); ?>
        <?php endif; ?>
    </div>
    <?php if ($show_read_more): ?>
    <script>
    (function() {
        var wrapper = document.getElementById('ggmc-desc-<?php echo esc_js($widget_id); ?>');
        if (!wrapper) return;

        var readMoreLink = wrapper.querySelector('.ggmc-read-more');
        var readLessLink = wrapper.querySelector('.ggmc-read-less');
        var excerptDiv = wrapper.querySelector('.ggmc-description-excerpt');
        var fullDiv = wrapper.querySelector('.ggmc-description-full');

        if (readMoreLink && excerptDiv && fullDiv) {
            readMoreLink.addEventListener('click', function(e) {
                e.preventDefault();
                excerptDiv.style.display = 'none';
                fullDiv.style.display = 'block';
            });
        }

        if (readLessLink && excerptDiv && fullDiv) {
            readLessLink.addEventListener('click', function(e) {
                e.preventDefault();
                fullDiv.style.display = 'none';
                excerptDiv.style.display = 'block';
            });
        }
    })();
    </script>
    <?php endif; ?>
    <?php
}
