<?php

namespace Webnestors\Wsn\Snippets;

class SingleAddBackToCategory extends \Webnestors\Wsn\Snippet {
    public $button_text = "Back to Category";

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

    public function title(): string {
        return esc_html__("Add Back to Category Button", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Add a button that leads to the category of the product.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Button Text", "enhancements-for-woocommerce"),
                "name" => "button_text",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Add Back to Category Button module provides a convenient button on WooCommerce product pages that directs customers back to the product’s category. This feature improves site navigation by allowing users to easily return to the category they were browsing, encouraging further exploration of similar products and enhancing the overall user experience.</p>
        <p>By simplifying navigation between products and categories, this module helps reduce bounce rates and keeps customers engaged, especially on sites with large catalogs where shoppers may want to explore multiple products within the same category.</p>
        <h3>Settings</h3>
        <p>Just enter the text you want to display on the button.</p>
        <h3>Appearance</h3>
        <p>You can style this with the css class <strong>wn-back-to-category</strong>.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action("woocommerce_share", [$this, "wn_add_back_product_category_button"], 5);
    }

    function wn_add_back_product_category_button() {
        $terms = wp_get_post_terms(get_the_id(), "product_cat");

        if (sizeof($terms) > 0) {

            // Get the first product category WP_Term object
            $term = reset($terms);
            // Get the term link (button link)
            $link = get_term_link($term, "product_cat");
            ?>
            <div class="wn-back-to-category">
                    <a href="<?php echo esc_url($link); ?>">
                    <?php echo esc_html($this->button_text); ?>
                    <strong><?php echo esc_html($term->name); ?></strong>
                </a>
            </div>
            <?php
        }
    }
}
