<?php

namespace Webnestors\Wsn\Snippets;

class SingleAutoApplyCoupon extends \Webnestors\Wsn\Snippet {
    public $coupon_text = "Text";
    public $coupon_code = "Code";

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

    public function title(): string {
        return esc_html__("Auto Apply a Coupon with a Checkbox", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__(
            "Enable to show your customer a checkbox that will automatically apply a specific coupon code on the single product page. You will have to create a coupon for this to work.",
            "enhancements-for-woocommerce"
        );
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Coupon Text", "enhancements-for-woocommerce"),
                "description" => esc_html__(
                    "Place here the text that you like to show, for example 'Save 10% Getting this Product'",
                    "enhancements-for-woocommerce"
                ),
                "name" => "coupon_text",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Coupon Code", "enhancements-for-woocommerce"),
                "name" => "coupon_code",
                "type" => "text",
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        
        <h3>Description</h3>
        <p>The Auto Apply a Coupon with a Checkbox module allows you to display a checkbox on the single product page that, when checked, automatically applies a specific coupon code to the customer’s cart. This feature provides an easy way for customers to receive discounts without needing to manually enter a coupon code, improving the user experience and encouraging conversions by making discounts more accessible.</p>
        <p>This module is ideal for targeted promotions or encouraging upsells, as it gives customers an effortless way to redeem discounts directly from the product page. To use this feature, you simply create the coupon code in WooCommerce, and the module will handle applying it when the checkbox is selected.</p>
        <h3>Settings</h3>
        <p>Just enter the text you want to display on the checkbox and the coupon code you want to apply.</p>
        <h3>Appearance</h3>
        <p>There are no customization options for this module.</p>
        <?php return ob_get_clean();
    }

    public function run() {
        add_action("woocommerce_after_add_to_cart_button", [$this, "wn_coupon_checbox"], 10);
        add_action("woocommerce_add_to_cart", [$this, "wn_apply_coupon_on_checkbox_check"], 10);
    }

    function wn_coupon_checbox() {
        echo '<p><label><input type="checkbox" name="apply_coupon" value="1"/> ' .
            esc_html($this->coupon_text) .
            "</label></p>";
    }

    function wn_apply_coupon_on_checkbox_check() {
        if (isset($_POST["apply_coupon"]) && $_POST["apply_coupon"] == 1) {
            WC()->cart->apply_coupon($this->coupon_code);
        }
    }
}
