<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Main render entry point for the Product Categories widget.
 *
 * @param \GGM_Commerce_Widget_Product_Categories $widget The widget instance.
 */
function ggmc_product_categories_render( $widget ) {
    $settings = $widget->get_settings_for_display();
    $product_data = $widget->get_product_data();

    if (!$product_data) {
        if (ggmc_is_editor_mode()) {
            echo '<div class="ggmc-product-categories"><span class="ggmc-categories-label">Categories:</span> <span class="ggmc-category-link">Sample Category</span></div>';
        }
        return;
    }

    // Get categories for this product
    $taxonomy = GGM_Commerce_Taxonomy::get_instance();
    $categories = $taxonomy->get_product_categories($product_data->id);

    if (empty($categories)) {
        return;
    }

    $layout     = $settings['display_layout'];
    $show_label = $settings['show_label'] === 'yes';
    $label_text = $settings['label_text'];
    $separator  = $settings['separator'];
    $show_image = $settings['show_image'] === 'yes' && $layout !== 'inline';

    echo '<div class="ggmc-product-categories ggmc-categories-' . esc_attr($layout) . '">';

    if ($show_label && !empty($label_text)) {
        echo '<span class="ggmc-categories-label">' . esc_html($label_text) . '</span>';
    }

    if ($layout === 'badges') {
        echo '<span class="ggmc-categories-badges">';
    } elseif ($layout === 'list') {
        echo '<ul class="ggmc-categories-list">';
    }

    $category_links = ggmc_product_categories_build_links( $categories, $layout, $show_image );

    if ($layout === 'inline') {
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $category_links contains pre-escaped HTML link elements
        echo implode(esc_html($separator), $category_links);
    } else {
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $category_links contains pre-escaped HTML link elements
        echo implode('', $category_links);
    }

    if ($layout === 'badges') {
        echo '</span>';
    } elseif ($layout === 'list') {
        echo '</ul>';
    }

    echo '</div>';
}

/**
 * Build an array of HTML link strings for each category.
 *
 * @param array  $categories  Array of category objects.
 * @param string $layout      The display layout (inline|list|badges).
 * @param bool   $show_image  Whether to include the category image.
 * @return string[]
 */
function ggmc_product_categories_build_links( $categories, $layout, $show_image ) {
    $category_links = [];

    foreach ($categories as $category) {
        $url = GGM_Commerce_Helpers::get_category_url($category);

        $link_html = '';

        if ($layout === 'list') {
            $link_html .= '<li class="ggmc-category-item">';
        }

        $link_html .= '<a href="' . esc_url($url) . '" class="ggmc-category-link">';

        if ($show_image && !empty($category->image_id)) {
            $image = wp_get_attachment_image($category->image_id, 'thumbnail', false, ['class' => 'ggmc-category-image']);
            if ($image) {
                $link_html .= $image;
            }
        }

        $link_html .= esc_html($category->name);
        $link_html .= '</a>';

        if ($layout === 'list') {
            $link_html .= '</li>';
        }

        $category_links[] = $link_html;
    }

    return $category_links;
}
