<?php

namespace Webnestors\Wsn\Snippets;

class ArchiveUnitsSold extends \Webnestors\Wsn\Snippet {
    public $heading_text = "";
    public $position_archive = "";

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

    public function title(): string {
        return esc_html__("Show Units Sold", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Show how many units have been sold for every product on the archive pages", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Heading Text", "enhancements-for-woocommerce"),
                "name" => "heading_text",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Position on page", "enhancements-for-woocommerce"),
                "name" => "position_archive",
                "type" => "select",
                "options" => [
                    "woocommerce_before_shop_loop_item_title",
                    "woocommerce_shop_loop_item_title",
                    "woocommerce_after_shop_loop_item_title",
                    "woocommerce_after_shop_loop_item",
                ],
                "option_labels" => [
                    "Before Product Image",
                    "Before Product Title",
                    "After Product Title",
                    "Before Add to Cart Button",
                ],
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Show Units Sold module displays the number of units sold for each product on the WooCommerce archive pages, giving customers valuable social proof and insight into product popularity. This feature showcases how well a product has been received by other customers, which can build trust and encourage more sales, especially for high-demand items.</p>
        <p>This module is particularly useful for stores looking to leverage social proof as part of their sales strategy, as it can create a sense of urgency and motivate customers to make a purchase, especially when they see that others are buying the product.</p>
        <h3>Settings</h3>
        <h4>Heading Text</h4>
        <p>Write the text that you want to appear before the number of units that was sold</p>
        <h4>Position on page</h4>
        <p>Choose where you want to display the number of units that was sold</p>
        <h3>Appearance</h3>
        <p>You can use the "wn-units-sold" class to customize the appearence</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action($this->position_archive, [$this, "wp_show_units_sold_product_archive"]);
    }

    function wp_show_units_sold_product_archive() {
        global $product;

        echo '<div class="wn-units-sold">' .
            esc_html($this->heading_text . $product->get_total_sales()) .
            "</div>";
    }
}
