<?php

namespace Webnestors\Wsn\Snippets;

class CartBackorderNotification extends \Webnestors\Wsn\Snippet {
    public $min_amount = 100;
    public $notice = "Example Notice";
    public $btn_text = "Continue Shopping";
    public $notification_position = "woocommerce_after_cart_totals";

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

    public function title(): string {
        return esc_html__("Backorder Notification", "enhancements-for-woocommerce");
    }

    public function description(): string {
        return esc_html__("Displays a message when at least one product in the cart is on backorder", "enhancements-for-woocommerce");
    }

    public function meta(): array {
        return [
            [
                "label" => esc_html__("Notification Text", "enhancements-for-woocommerce"),
                "description" => esc_html__(
                    "The message you want to be displayed. For example, (One or more products in the cart are on backorder. Estimated delivery time is about 4-5 days)",
                    "enhancements-for-woocommerce"
                ),
                "name" => "notice",
                "type" => "text",
            ],
            [
                "label" => esc_html__("Notification Position", "enhancements-for-woocommerce"),
                "description" => esc_html__("The notification's position on the cart page.", "enhancements-for-woocommerce"),
                "name" => "notification_position",
                "type" => "select",
                "options" => [
                    "woocommerce_cart_totals_before_shipping",
                    "woocommerce_before_shipping_calculator",
                    "woocommerce_after_shipping_calculator",
                    "woocommerce_cart_totals_after_shipping",
                    "woocommerce_cart_totals_before_order_total",
                    "woocommerce_cart_totals_after_order_total",
                    "woocommerce_proceed_to_checkout",
                    "woocommerce_after_cart_totals",
                ],
                "option_labels" => [
                    "Before totals shipping",
                    "Before shipping calculator",
                    "After shipping calculator",
                    "After totals shipping",
                    "Before order total",
                    "After order total",
                    "Before checkout button",
                    "After checkout button",
                ],
            ],
        ];
    }

    public function tooltip(): ?string {
        ob_start(); ?>
        <h1><strong><?php echo esc_html($this->title()); ?></strong></h1>
        <h3>Description</h3>
        <p>The Backorder Notification module adds an important layer of transparency to your WooCommerce store by automatically notifying customers when at least one product in their cart is on backorder. When activated, this feature displays a clear message on both the cart and checkout pages, informing customers that one or more items in their cart are currently out of stock but will be shipped once available. This ensures that customers are fully aware of backordered products before they complete their purchase, helping to manage expectations and reduce potential frustration.</p>
        <p>Backorders allow customers to purchase items even when they’re not in stock, ensuring that they can still secure the product while you replenish inventory. However, it’s important to communicate this status clearly to avoid confusion and potential dissatisfaction. The Backorder Notification module helps you maintain this transparency, providing a straightforward message that keeps customers informed throughout the purchasing process.</p>
        <h3>Settings</h3>
        <h4>Notification Text</h4>
        <p>The text that will appear to the cart when at least one product is on backorder</p>
        <h4>Notification Position</h4>
        <p>Choose where you want to place the notification</p>
        <h4>Note: The notification position may not always work if your theme is not using the standard WooCommerce cart Positions</h4>
        <h3>Appearance</h3>
        <p>You can customize this module using custom css and the class "woocommerce-message".</p>
        <?php return ob_get_clean();
    }

    public function run() {
        if ($this->notification_position !== "") {
            add_action($this->notification_position, [$this, "wn_show_backordered_items_cart_notice"]);
        }
    }

    function wn_show_backordered_items_cart_notice() {
        $found = false;

        foreach (WC()->cart->get_cart() as $cart_item) {
            if ($cart_item["data"]->is_on_backorder($cart_item["quantity"])) {
                $found = true;
                break;
            }
        }

        if ($found) {
            wc_print_notice($this->notice);
        }
    }
}
