<?php
/**
 * Taxonomy Header Widget — render functions
 * All functions are standalone and prefixed ggmc_taxonomy_header_.
 * The $widget parameter replaces $this from the original class methods.
 */

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

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter

/**
 * Main render entry point.
 *
 * @param \GGM_Commerce_Base_Widget $widget The widget instance.
 */
function ggmc_taxonomy_header_render( $widget ) {
    $settings = $widget->get_settings_for_display();

    // Determine taxonomy type and get data
    $taxonomy_type = $settings['taxonomy_type'];
    $taxonomy_data = null;
    $product_count = 0;
    $image_url     = null;

    // Auto-detect from URL or use specified type
    if ($taxonomy_type === 'auto' || $taxonomy_type === 'category') {
        $category_id = GGM_Commerce_Helpers::get_current_category_id();
        if ($category_id) {
            $taxonomy      = GGM_Commerce_Taxonomy::get_instance();
            $taxonomy_data = $taxonomy->get_category($category_id);
            $taxonomy_type = 'category';

            if ($taxonomy_data) {
                // Get product count
                global $wpdb;
                $product_count = (int) $wpdb->get_var($wpdb->prepare(
                    "SELECT COUNT(DISTINCT pc.product_id)
                    FROM {$wpdb->prefix}ggmc_product_categories pc
                    INNER JOIN {$wpdb->prefix}ggmc_products p ON pc.product_id = p.id
                    WHERE pc.category_id = %d AND (p.status = 'active' OR p.status IS NULL)",
                    $category_id
                ));

                // Get category image if available
                if (!empty($taxonomy_data->image_id) && $taxonomy_data->image_id > 0) {
                    $image_url = wp_get_attachment_image_url($taxonomy_data->image_id, 'large');
                }
            }
        }
    }

    if (!$taxonomy_data && ($taxonomy_type === 'auto' || $taxonomy_type === 'tag')) {
        $tag_id = GGM_Commerce_Helpers::get_current_tag_id();
        if ($tag_id) {
            $taxonomy      = GGM_Commerce_Taxonomy::get_instance();
            $taxonomy_data = $taxonomy->get_tag($tag_id);
            $taxonomy_type = 'tag';

            if ($taxonomy_data) {
                // Get product count
                global $wpdb;
                $product_count = (int) $wpdb->get_var($wpdb->prepare(
                    "SELECT COUNT(DISTINCT pt.product_id)
                    FROM {$wpdb->prefix}ggmc_product_tags pt
                    INNER JOIN {$wpdb->prefix}ggmc_products p ON pt.product_id = p.id
                    WHERE pt.tag_id = %d AND (p.status = 'active' OR p.status IS NULL)",
                    $tag_id
                ));
            }
        }
    }

    // Show placeholder in editor if no data
    if (!$taxonomy_data) {
        if (ggmc_is_editor_mode()) {
            echo '<div class="ggmc-taxonomy-header">';
            echo '<h1 class="ggmc-taxonomy-title">Sample Category Name</h1>';
            echo '<p class="ggmc-taxonomy-description">This is a sample description that will be replaced with the actual taxonomy description.</p>';
            echo '<span class="ggmc-taxonomy-count">12 products</span>';
            echo '</div>';
        }
        return;
    }

    // Determine layout classes
    $image_position = $settings['image_position'];
    $has_image      = $image_url && $settings['show_image'] === 'yes';
    $classes        = ['ggmc-taxonomy-header'];

    if ($has_image) {
        $classes[] = 'ggmc-taxonomy-header--has-image';
        $classes[] = 'ggmc-taxonomy-header--image-' . $image_position;
        if ($image_position === 'background') {
            $classes[] = 'ggmc-taxonomy-header--background';
        }
    }

    // Build background style for background image
    $background_style = '';
    if ($has_image && $image_position === 'background') {
        $background_style = ' style="background-image: url(' . esc_url($image_url) . ');"';
    }

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $background_style is built with esc_url() above
    echo '<div class="' . esc_attr(implode(' ', $classes)) . '"' . $background_style . '>';

    // Image above title or left position
    if ($has_image && in_array($image_position, ['above', 'left'])) {
        echo '<div class="ggmc-taxonomy-image">';
        echo '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($taxonomy_data->name) . '">';
        echo '</div>';
    }

    echo '<div class="ggmc-taxonomy-content">';

    // Title
    if ($settings['show_title'] === 'yes') {
        $tag    = $settings['title_tag'];
        $prefix = $settings['title_prefix'];
        echo '<' . esc_attr($tag) . ' class="ggmc-taxonomy-title">';
        if (!empty($prefix)) {
            echo '<span class="ggmc-taxonomy-prefix">' . esc_html($prefix) . '</span>';
        }
        echo esc_html($taxonomy_data->name);
        echo '</' . esc_attr($tag) . '>';
    }

    // Image below title
    if ($has_image && $image_position === 'below') {
        echo '<div class="ggmc-taxonomy-image">';
        echo '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($taxonomy_data->name) . '">';
        echo '</div>';
    }

    // Description
    if ($settings['show_description'] === 'yes' && !empty($taxonomy_data->description)) {
        echo '<div class="ggmc-taxonomy-description">';
        echo wp_kses_post($taxonomy_data->description);
        echo '</div>';
    }

    // Product count
    if ($settings['show_product_count'] === 'yes') {
        $format     = $settings['product_count_format'];
        $count_text = str_replace('{count}', $product_count, $format);
        echo '<span class="ggmc-taxonomy-count">' . esc_html($count_text) . '</span>';
    }

    echo '</div>'; // .ggmc-taxonomy-content

    // Image right position
    if ($has_image && $image_position === 'right') {
        echo '<div class="ggmc-taxonomy-image">';
        echo '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($taxonomy_data->name) . '">';
        echo '</div>';
    }

    echo '</div>'; // .ggmc-taxonomy-header

    ggmc_taxonomy_header_render_inline_styles( $widget );
}

/**
 * Render any inline styles (currently a no-op; styles live in CSS files).
 *
 * @param \GGM_Commerce_Base_Widget $widget The widget instance.
 */
function ggmc_taxonomy_header_render_inline_styles( $widget ) {
    // Styles moved to widgets.css (structural) and professional-defaults.css (design)
}
