<?php

namespace Webnestors\Wsn\Snippets;

class SingleStockProgressBar extends \Webnestors\Wsn\Snippet {

    public function tab(): string {
        return "product";
    }

    public function title(): string {
        return esc_html__("Stock status progress bar", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Adds a visual progress bar to single product pages that shows how many items are left in stock.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Stock Status Progress Bar module adds a visually engaging progress bar to single product pages, displaying how many items are currently in stock. This progress bar dynamically adjusts to show remaining quantities, creating a sense of urgency when stock levels are low and highlighting availability when stock is abundant. By giving customers a clear visual of stock status, this feature encourages quicker purchasing decisions, particularly for items with limited quantities.</p>
        <p>This module is especially effective for high-demand products, seasonal items, or limited releases, where low stock visuals can help boost conversions by conveying a "buy before it’s gone" message.</p>
        <h3>Settings</h3>
        <p>There are no settings for this module.</p>
        <h3>Appearance</h3>
        <p>You can style this with the css class <strong>wn-stock-bar</strong>.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action( 'woocommerce_before_add_to_cart_form', [$this, 'wn_stock_bar'], 10, 0 );
        add_filter( 'woocommerce_get_availability', [$this, 'wn_hide_default_stock_notice'], 10, 2 );
    }

    function wn_stock_bar() {
            global $product;
        
            // Remove the default WooCommerce stock notice
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        
            // Handle product stock status
            if ( ! $product->managing_stock() ) {
                return; // Don't show the progress bar if stock isn't being managed
            }
        
            // If the product is out of stock, display default "Out of stock" message
            if ( ! $product->is_in_stock() ) {
                echo '<p class="stock out-of-stock">' . esc_html__( 'Out of stock', 'woocommerce' ) . '</p>';
                return; // Stop further execution if the product is out of stock
            }
        
            // If the product is on backorder, display default "Available on backorder" message
            if ( $product->is_on_backorder() ) {
                echo '<p class="stock backorder">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
                return; // Stop further execution if the product is on backorder
            }
        
            // Get the current stock quantity and ensure the progress bar reflects it
            $stock_quantity = $product->get_stock_quantity();
            $max_stock = 100; // Define a maximum stock limit for the progress bar
        
            // Ensure $stock_quantity doesn't exceed the defined maximum stock
            $progress_value = min( $stock_quantity, $max_stock );
        
            // Output the custom stock progress bar with appropriate styling and alignment
            echo '<div class="wn-stock-bar">';
            echo esc_html( 'Only ' . $stock_quantity . ' in stock!' ) . '<br>';
            echo '<progress max="' . esc_attr( $max_stock ) . '" value="' . esc_attr( $progress_value ) . '" style="width: 100%;"></progress>';
            echo '</div>';
        }
    
    // Hide the default stock notice completely if the progress bar is displayed
    
    
    function wn_hide_default_stock_notice( $availability, $product ) {
        // If stock is being managed and product is in stock, hide the default stock message
        if ( $product->managing_stock() && $product->is_in_stock() && ! $product->is_on_backorder() ) {
            $availability['availability'] = ''; // Empty the availability message
        }
    
        return $availability;
    }
}
