<?php

namespace Webnestors\Wsn\Snippets;

class GeneralChangeZeroPriceToFree extends \Webnestors\Wsn\Snippet {
    public $free_text = "Free";

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

    public function title(): string {
        return esc_html__("Change Zero Prices (0.00) to Free or Other", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Change the 0.00 price to a text that you prefer. For example, (Free Product).",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Zero Price Text", "enhancements-for-woocommerce"),
                "description" => esc_html__("Place what you like to show instead of zero price", "enhancements-for-woocommerce"),
                "name" => "free_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Change Zero Prices (0.00) to Free or Other module gives you the ability to replace the default "0.00" price display for free products with custom text, such as "Free" or any other label of your choice. This is particularly useful for stores that offer free products, samples, or digital downloads, as displaying "0.00" can sometimes be confusing or seem unprofessional. With this feature, you can improve clarity and enhance the overall user experience by displaying a more informative or engaging message where the price would typically appear.</p>
        <p>By changing the "0.00" to something like "Free Product" or "Available at No Cost," you can make your offers more appealing and easy to understand. This also allows you to tailor the messaging to better align with your store’s branding, marketing strategies, or the specific purpose of the free product.</p>
        <h3>Settings</h3>
        <h4>Zero Price Text</h4>
        <p>Write a text that will replace the 0.00 price tag</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_zero_price_display"], 10, 2);
    }

    function wn_change_zero_price_display($price, $product) {
        if (empty($product->get_price()) || $product->get_price() == 0) {
            // If price is not entered or set to 0
            $price = $this->free_text;
        }
        return $price;
    }
}
