<?php

namespace Webnestors\Wsn\Snippets;

class GeneralChangeVariablePriceRangeText extends \Webnestors\Wsn\Snippet {
    public $from_text = "Text";
    public $to_text = "Text";

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

    public function title(): string {
        return __("Variable Product Price Range Text", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return __(
            "Change the variable product price range prefixes. For example (From 10 to 50). Works for archives and single product pages.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => __("From Prefix", "enhancements-for-woocommerce"),
                "name" => "from_text",
                "type" => "text",
            ],
            [
                "label" => __("To Prefix", "enhancements-for-woocommerce"),
                "name" => "to_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Variable Product Price Range Text module allows you to customize the price range display for variable products in your WooCommerce store. By default, WooCommerce shows a price range for products that have multiple pricing options—like different sizes, colors, or other variations—with the format "$X - $Y." However, this default display may not always fit your branding or the way you want to present your products. With this module, you can enable prefix texts that appears before the variable low and High prices, tailoring it to better match your messaging, promotional language, or business model.</p>
        <h3>Settings</h3>
        <h4>From Prefix</h4>
        <p>Write a text that will be before the low price range, for example "Starts with"</p>
        <h4>To Prefix</h4>
        <p>Write a text that will be before the high price range, for example "To"</p>
        <h3>Appearance</h3>
        <p>There are no customization options for this module.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_filter("woocommerce_get_price_html", [$this, "wn_change_variable_price_range_display"], 10, 2);
    }

    function wn_change_variable_price_range_display($price, $product) {
        if (!$product->is_type("variable")) {
            return $price;
        }

        $prices = $product->get_variation_prices(true);

        if (empty($prices["price"])) {
            return "";
        }

        $min_price = current($prices["price"]);
        $max_price = end($prices["price"]);

        return "$this->from_text " .
            wc_price($min_price) .
            $product->get_price_suffix() .
            " $this->to_text " .
            $max_price;
    }
}
