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

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

    // Get product data (includes preview product support)
    $product_data = $widget->get_product_data();

    // Determine stock quantity
    $stock_quantity = null;
    if ($product_data && isset($product_data->stock)) {
        $stock_quantity = intval($product_data->stock);
    }

    // In editor mode with no product, show placeholder
    $is_editor = ggmc_is_editor_mode();
    if ($stock_quantity === null) {
        if ($is_editor) {
            $stock_quantity = 25; // Placeholder value for editor
        } else {
            return; // Don't render anything on frontend without product
        }
    }

    // Determine stock status
    $low_stock_threshold = isset($settings['low_stock_threshold']) ? intval($settings['low_stock_threshold']) : 10;

    if ($stock_quantity <= 0) {
        $status_class = 'out-of-stock';
        $icon_setting = $settings['out_of_stock_icon'] ?? null;
        $status_text = $settings['out_of_stock_text'] ?? __('Out of Stock', 'ggm-commerce');
    } elseif ($stock_quantity <= $low_stock_threshold) {
        $status_class = 'low-stock';
        $icon_setting = $settings['low_stock_icon'] ?? null;
        $status_text = $settings['low_stock_text'] ?? __('Low Stock:', 'ggm-commerce');
    } else {
        $status_class = 'in-stock';
        $icon_setting = $settings['in_stock_icon'] ?? null;
        $status_text = $settings['in_stock_text'] ?? __('In Stock:', 'ggm-commerce');
    }

    // Check for restock date — replaces "Out of Stock" when available
    $restock_text = '';
    if ( $status_class === 'out-of-stock' && ( $settings['show_restock_date'] ?? 'yes' ) === 'yes' ) {
        $restock_date = $product_data->restock_date ?? null;
        $restock_format = $settings['restock_display_format'] ?? 'descriptive';
        $restock_text = GGM_Commerce_Helpers::get_restock_display_text( $restock_date, $restock_format );
    }

    // Build display text based on format
    $display_format = $settings['display_format'] ?? 'text_number';
    $show_icon = $settings['show_icon'] === 'yes';

    // If restock text is available, use it instead of out-of-stock text
    if ( $restock_text !== '' ) {
        $display_text = $restock_text;
        $status_class = 'out-of-stock restock-expected';
        $icon_setting = $settings['restock_icon'] ?? $icon_setting;
    } else {
        $display_text = ggmc_product_stock_build_display_text( $display_format, $status_text, $stock_quantity );
    }

    // Render the widget
    echo '<div class="ggmc-product-stock ' . esc_attr($status_class) . '">';

    if ($show_icon && !empty($icon_setting)) {
        echo '<span class="ggmc-stock-icon">';
        ggmc_render_icon($icon_setting, ['aria-hidden' => 'true']);
        echo '</span>';
    }

    echo '<span class="ggmc-stock-text">' . esc_html($display_text) . '</span>';
    echo '</div>';
}

/**
 * Build the display text string based on format, status text and stock quantity.
 *
 * @param string $display_format  The chosen display format setting.
 * @param string $status_text     The localised status label.
 * @param int    $stock_quantity  The numeric stock count.
 * @return string
 */
function ggmc_product_stock_build_display_text( $display_format, $status_text, $stock_quantity ) {
    switch ($display_format) {
        case 'number_only':
            return ($stock_quantity > 0) ? $stock_quantity : $status_text;

        case 'status_only':
            return ($stock_quantity > 0) ? rtrim($status_text, ':') : $status_text;

        case 'text_number':
        default:
            if ($stock_quantity > 0) {
                return $status_text . ' ' . $stock_quantity;
            }
            return $status_text;
    }
}
