<?php

if (!defined("ABSPATH")) {
    exit();
} // Exit if accessed directly

class wizit_hook_class
{
    private static $wizit_rating_notice_setting_name = "woocommerce_wizit_settings_rating";

    private static $instance = null;

    public static function initialize()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function __construct()
    {
        // load hook

        // admin message - plugin page only

        add_action("admin_notices", [
            $this,

            "display_plugin_rating_admin_notices",
        ]);

        add_action("wp_ajax_wizit_plugin_rating_did_callback", [
            $this,

            "wizit_plugin_rating_did_callback",
        ]);

        add_action("woocommerce_before_add_to_cart_quantity", [
            $this,

            "wizit_update_price_with_variation_price",
        ]);

        // // --- HPOS list table (WC 6.9+): filter wc_get_orders() args used by the Order
        add_filter(
            "woocommerce_order_list_table_prepare_items_query_args",
            [$this, "wizit_hide_order_HPOS"],
            20
        );

        // --- Extra fallback for other WC order queries (HPOS path) triggered on the orders screen.
        add_filter(
            "woocommerce_order_query_args",
            [$this, "wizit_hide_order"],
            20
        );

        // --- Legacy CPT storage (pre‑HPOS) admin list table: modify the main WP_Query for shop_order.
        add_action("pre_get_posts", [$this, "wizit_hide_order_legacy"]);
    }

    public function remove_hooks()
    {
        $log = new WC_Logger();

        // load setting

        $settings = get_option("woocommerce_wizit_settings", true);

        $payment_info_on_product_hook =
            is_array($settings) &&
            array_key_exists("payment_info_on_product_hook", $settings)
                ? $settings["payment_info_on_product_hook"]
                : "";

        $payment_info_on_product_hook_priority =
            is_array($settings) &&
            array_key_exists("payment_info_on_product_hook_priority", $settings)
                ? $settings["payment_info_on_product_hook_priority"]
                : ""; // $settings['payment_info_on_product_hook_priority'];

        $payment_info_on_product_cat_hook =
            is_array($settings) &&
            array_key_exists("payment_info_on_product_cat_hook", $settings)
                ? $settings["payment_info_on_product_cat_hook"]
                : ""; // $settings['payment_info_on_product_cat_hook'];

        $payment_info_on_product_cat_hook_priority =
            is_array($settings) &&
            array_key_exists(
                "payment_info_on_product_cat_hook_priority",
                $settings
            )
                ? $settings["payment_info_on_product_cat_hook_priority"]
                : ""; // $settings['payment_info_on_product_cat_hook_priority'];

        if (!isset($environments)) {
            include "wizit/wizit-default-value.php";
        }

        // remove old product hook

        if (
            isset($payment_info_on_product_hook) &&
            !empty($payment_info_on_product_hook) &&
            isset($payment_info_on_product_hook_priority) &&
            !empty($payment_info_on_product_hook_priority) &&
            !is_nan($payment_info_on_product_hook_priority)
        ) {
            remove_action(
                $payment_info_on_product_hook,
                [$this, "remove_info_for_product_detail_page"],

                (int) $payment_info_on_product_hook_priority,

                0
            );
        }

        // always remove default hooks

        remove_action(
            $def_payment_info_on_product_hook,
            [$this, "remove_info_for_product_detail_page"],

            (int) $def_payment_info_on_product_hook_priority,

            0
        );

        // remove product cart hook

        if (
            isset($payment_info_on_product_cat_hook) &&
            !empty($payment_info_on_product_cat_hook) &&
            isset($payment_info_on_product_cat_hook_priority) &&
            !empty($payment_info_on_product_cat_hook_priority) &&
            !is_nan($payment_info_on_product_cat_hook_priority)
        ) {
            remove_action(
                $payment_info_on_product_cat_hook,
                [$this, "remove_info_for_product_cat_page"],

                (int) $payment_info_on_product_cat_hook_priority,

                0
            );
        }

        // always remove default hooks

        remove_action(
            $def_payment_info_on_product_cat_hook,
            [$this, "remove_info_for_product_cat_page"],

            (int) $def_payment_info_on_product_cat_hook_priority,

            0
        );
    }

    public function register_hooks()
    {
        $log = new WC_Logger();

        $settings = get_option("woocommerce_wizit_settings", true);

        $payment_info_on_product_hook = "";

        $payment_info_on_product_hook_priority = 15;

        $payment_info_on_product_cat_hook = "";

        $payment_info_on_product_cat_hook_priority = 15;

        if (
            is_array($settings) &&
            array_key_exists("payment_info_on_product_hook", $settings) &&
            array_key_exists(
                "payment_info_on_product_hook_priority",
                $settings
            ) &&
            array_key_exists("payment_info_on_product_cat_hook", $settings) &&
            array_key_exists(
                "payment_info_on_product_cat_hook_priority",
                $settings
            ) &&
            isset($settings["payment_info_on_product_hook"]) &&
            isset($settings["payment_info_on_product_hook_priority"]) &&
            isset($settings["payment_info_on_product_cat_hook"]) &&
            isset($settings["payment_info_on_product_cat_hook_priority"])
        ) {
            $payment_info_on_product_hook =
                $settings["payment_info_on_product_hook"];

            $payment_info_on_product_hook_priority =
                $settings["payment_info_on_product_hook_priority"];

            $payment_info_on_product_cat_hook =
                $settings["payment_info_on_product_cat_hook"];

            $payment_info_on_product_cat_hook_priority =
                $settings["payment_info_on_product_cat_hook_priority"];
        }

        if (!isset($environments)) {
            include "wizit/wizit-default-value.php";
        }

        // add action for print wizit on product detail page

        if (
            isset($payment_info_on_product_hook) &&
            !empty($payment_info_on_product_hook) &&
            isset($payment_info_on_product_hook_priority) &&
            !empty($payment_info_on_product_hook_priority) &&
            !is_nan($payment_info_on_product_hook_priority)
        ) {
            add_action(
                $payment_info_on_product_hook,
                [$this, "wizit_print_info_for_product_detail_page"],

                (int) $payment_info_on_product_hook_priority,

                0
            );
        } else {
            add_action(
                $def_payment_info_on_product_hook,
                [$this, "wizit_print_info_for_product_detail_page"],

                (int) $def_payment_info_on_product_hook_priority,

                0
            );
        }

        // add action for print wizit on product cat page

        if (
            isset($payment_info_on_product_cat_hook) &&
            !empty($payment_info_on_product_cat_hook) &&
            isset($payment_info_on_product_cat_hook_priority) &&
            !empty($payment_info_on_product_cat_hook_priority) &&
            !is_nan($payment_info_on_product_cat_hook_priority)
        ) {
            add_action(
                $payment_info_on_product_cat_hook,
                [$this, "wizit_print_info_for_product_cat_page"],

                (int) $payment_info_on_product_cat_hook_priority,

                0
            );
        } else {
            add_action(
                $def_payment_info_on_product_cat_hook,
                [$this, "wizit_print_info_for_product_cat_page"],

                (int) $def_payment_info_on_product_cat_hook_priority,

                0
            );
        }

        // add check out page

        add_action(
            "woocommerce_proceed_to_checkout",
            [$this, "wizit_print_info_for_cart_page"],
            15,
            0
        );

        // add hook to new woo block
        add_filter(
            "__experimental_woocommerce_blocks_add_data_attributes_to_block",
            function ($allowed_blocks) {
                $allowed_blocks[] = "wizit/checkout-totals-block";
                return $allowed_blocks;
            },
            10,
            1
        );
    }

    /*

     *  Print a paragraph of Wizit info onto the individual product pages if enabled and the product is valid.

     *  Note: Default Hooked onto the "woocommerce_single_product_summary" Action.

     *

    */

    //add_action('woocommerce_single_product_summary', 'wizit_print_info_for_product_detail_page', 15);

    public static function wizit_print_info_for_product_detail_page()
    {
        if (!function_exists("process_and_print_wizit_paragraph")) {
            include "wizit/wizit-helper.php";
        }

        $settings = get_option("woocommerce_wizit_settings", true);

        if (
            !isset($settings) ||
            !isset($settings["payment_info_on_product"]) ||
            $settings["payment_info_on_product"] != "yes"
        ) {
            # Don't display anything on product pages unless the "Payment info on individual product pages"

            # box is ticked and there is a message to display.

            return;
        }

        global $post, $product;

        // if (is_null($product))

        // {

        //     $product = get_wizit_product_from_the_post();

        // }

        $price = $product->get_price();
        $is_on_sale = false;

        if (
            $product->get_sale_price() != "" &&
            is_numeric($product->get_sale_price())
        ) {
            $price = $product->get_sale_price();
            $is_on_sale = true;
        }

        wizit_hook_class::load_required_css_js_file();

        process_and_print_wizit_paragraph(
            "[wizit_logo] Payments starting from [MIN_LIMIT] per week. [Learn_More]",
            "wizit_html_on_individual_product_pages",
            $price,
            plugin_dir_url(__FILE__),
            "wizit-logo-inline",
            true,
            $product->is_type("variable"),
            $product->get_id(),
            $is_on_sale
        );
    }

    public static function remove_info_for_product_detail_page()
    {
    }

    /*

     *  Print a paragraph of Wizit info onto the cart pages if enabled.

     *  Note: Default Hooked onto the "woocommerce_proceed_to_checkout" Action.

     *

    */

    public static function wizit_print_info_for_cart_page()
    {
        if (!function_exists("process_and_print_wizit_paragraph")) {
            include "wizit/wizit-helper.php";
        }

        $settings = get_option("woocommerce_wizit_settings", true);

        if (
            !isset($settings) ||
            !isset($settings["payment_info_on_cart"]) ||
            $settings["payment_info_on_cart"] != "yes"
        ) {
            # Don't display anything on product pages unless the "Payment info on individual product pages"

            # box is ticked and there is a message to display.

            return;
        }

        global $woocommerce;

        if (!isset(WC()->cart)) {
            return;
        }

        $price = WC()->cart->total;

        wizit_hook_class::load_required_css_js_file();

        process_and_print_wizit_paragraph(
            "[wizit_logo] Payments starting from [MIN_LIMIT] per week. [Learn_More]",
            "wizit_html_on_cart_pages",
            $price,
            plugin_dir_url(__FILE__),
            "wizit-logo-inline"
        );
    }

    /*

     *  Print a paragraph of Wizit info onto the product cat pages if enabled.

     *  Note: Default Hooked onto the "woocommerce_after_shop_loop_item_title" Action.

     *

    */

    //add_action('woocommerce_after_shop_loop_item_title', 'wizit_print_info_for_product_cat_page', 15);

    public static function wizit_print_info_for_product_cat_page()
    {
        if (!function_exists("process_and_print_wizit_paragraph")) {
            include "wizit/wizit-helper.php";
        }

        $settings = get_option("woocommerce_wizit_settings", true);

        if (
            !isset($settings) ||
            !isset($settings["payment_info_on_product_cat"]) ||
            $settings["payment_info_on_product_cat"] != "yes"
        ) {
            # Don't display anything on product pages unless the "Payment info on individual product pages"

            # box is ticked and there is a message to display.

            return;
        }

        global $post, $product;

        $price = $product->get_price();

        wizit_hook_class::load_required_css_js_file();

        process_and_print_wizit_paragraph(
            "[wizit_logo] Payments starting from [MIN_LIMIT] per week.",
            "wizit_html_on_product_cat_pages",
            $price,
            plugin_dir_url(__FILE__),
            "wizit-logo-block"
        );
    }

    public static function remove_info_for_product_cat_page()
    {
    }

    public static function display_plugin_rating_admin_notices()
    {
        $html = '

        <div class="notice notice-success is-dismissible wizit-plugin-rating-admin-notices">

            <h2>Give wizit a review</h2>

            <p>Thank you for choosing wizit. We hope you love it. 

            Could you take a couple of seconds posting a nice review to share your happy experience?</p>

            <p>We will be forever grateful. Thank you in advance ;)</p>

            <p>

                <a href="https://wordpress.org/support/plugin/wizit-gateway-for-woocommerce/reviews/" target="_blank" class="button button-primary" id="wizit-review-rate-btn">Rate now</a>

                <a href="#" class="button" id="wizit-review-later-btn">Later</a>

                <a href="#" class="button" id="wizit-review-did-btn">Already did</a>

            </p>

        </div>

        ';

        $rating_setting = get_option(
            wizit_hook_class::$wizit_rating_notice_setting_name
        );

        $screen = get_current_screen();

        if (
            $screen->id == "plugins" &&
            (empty($rating_setting) || $rating_setting != 1)
        ) {
            // load admin js

            wizit_hook_class::load_required_css_js_file("admin");

            # Allow other plugins to maniplulate or replace the HTML echoed by this funtion.
            $html = apply_filters("display_plugin_rating_admin_notices", $html);

            echo wp_kses($html, [
                "div" => ["class" => true],
                "p" => [],
                "h2" => [],
                "a" => [
                    "href" => true,
                    "class" => true,
                    "target" => true,
                    "ref" => true,
                    "id" => true,
                ],
            ]);
        }
    }

    public static function wizit_plugin_rating_did_callback()
    {
        update_option(wizit_hook_class::$wizit_rating_notice_setting_name, 1);
    }

    public static function load_required_css_js_file($type = "public")
    {
        if ($type == "admin") {
            wp_enqueue_style(
                "wizit_admin_css",
                plugins_url("assets/css/admin.css", __FILE__),
                [],
                "1.0"
            );

            wp_enqueue_script(
                "wizit_admin_js",
                plugins_url("assets/js/admin.js", __FILE__),
                [],
                "1.0"
            );
        } else {
            $settings = get_option("woocommerce_wizit_settings", true);

            if (
                isset($settings) &&
                is_array($settings) &&
                array_key_exists("enabled", $settings)
            ) {
                $enabled = $settings["enabled"];

                if ($enabled === "yes") {
                    // load css and js

                    wp_enqueue_style(
                        "wizit_custom_css",
                        plugins_url("assets/css/custom-popup.css", __FILE__),
                        [],
                        "1.0"
                    );

                    wp_enqueue_style(
                        "wizit_checkout_css",
                        plugins_url("assets/css/checkout.css", __FILE__),
                        [],
                        "1.0"
                    );

                    wp_enqueue_script(
                        "wizit_custom_js",
                        plugins_url("assets/js/custom-popup.js", __FILE__),
                        [],
                        "1.0"
                    );
                }
            }
        }
    }

    public static function wizit_update_price_with_variation_price()
    {
        $log = new WC_Logger();

        global $product;

        $price = null;

        $is_on_sale = false;

        if (!$product->is_type("variable")) {
            $price = $product->get_price();
            if (
                $product->get_sale_price() != "" &&
                is_numeric($product->get_sale_price())
            ) {
                $price = $product->get_sale_price();
                $is_on_sale = true;
            }
        }

        if (!function_exists("process_and_print_wizit_paragraph")) {
            include "wizit/wizit-helper.php";
        }

        $settings = get_option("woocommerce_wizit_settings", true);

        if (
            !isset($settings) ||
            !isset($settings["payment_info_on_product"]) ||
            $settings["payment_info_on_product"] != "yes"
        ) {
            # Don't display anything on product pages unless the "Payment info on individual product pages"

            # box is ticked and there is a message to display.

            return;
        }

        wizit_hook_class::load_required_css_js_file();

        $html = wizit_get_display_html(
            "[wizit_logo] Payments starting from [MIN_LIMIT] per week. [Learn_More]",
            "wizit_html_on_individual_product_pages2",
            $price,
            plugin_dir_url(__FILE__),
            "wizit-logo-inline",
            true,
            $product->is_type("variable"),
            $product->get_id(),
            $is_on_sale
        );?>

        <script>

            jQuery(document).ready(function(){

               // jQuery('#wizit-price-range-holder').html(``);

            });

        </script>

        <?php
    }

    public static function wizit_hide_order_HPOS($args)
    {
        if (!wizit_hook_class::should_hide_orders()) {
            return $args;
        }

        // Initialize meta_query safely
        $meta_query = $args["meta_query"] ?? [];

        // CRITICAL: Wrap existing conditions + our rule in top-level AND
        $new_condition = [
            "relation" => "OR",
            ["key" => "_wizit_wovt_hidden", "compare" => "NOT EXISTS"],
            ["key" => "_wizit_wovt_hidden", "value" => "1", "compare" => "!="],
        ];

        // Preserve existing meta_query structure
        if (!empty($meta_query)) {
            $args["meta_query"] = [
                "relation" => "AND",
                $meta_query,
                $new_condition,
            ];
        } else {
            $args["meta_query"] = [$new_condition];
        }

        return $args;
    }

    public static function wizit_hide_order($args)
    {
        if (!wizit_hook_class::should_hide_orders()) {
            return $args;
        }

        // Initialize meta_query safely
        $meta_query = $args["meta_query"] ?? [];

        // CRITICAL: Wrap existing conditions + our rule in top-level AND
        $new_condition = [
            "relation" => "OR",
            ["key" => "_wizit_wovt_hidden", "compare" => "NOT EXISTS"],
            ["key" => "_wizit_wovt_hidden", "value" => "1", "compare" => "!="],
        ];

        // Preserve existing meta_query structure
        if (!empty($meta_query)) {
            $args["meta_query"] = [
                "relation" => "AND",
                $meta_query,
                $new_condition,
            ];
        } else {
            $args["meta_query"] = [$new_condition];
        }

        return $args;
    }

    public static function wizit_hide_order_legacy($query)
    {
        if (!is_admin() || !$query->is_main_query()) {
            return;
        }

        $screen = get_current_screen();
        // CORRECT screen ID for legacy orders screen
        if (
            !$screen ||
            "shop_order" !== $screen->post_type ||
            "edit.php" !== $screen->base
        ) {
            return;
        }

        if (!wizit_hook_class::should_hide_orders()) {
            return;
        }

        $meta_query = (array) $query->get("meta_query");
        $new_condition = [
            "relation" => "OR",
            ["key" => "_wizit_wovt_hidden", "compare" => "NOT EXISTS"],
            ["key" => "_wizit_wovt_hidden", "value" => "1", "compare" => "!="],
        ];

        if (!empty($meta_query)) {
            $query->set("meta_query", [
                "relation" => "AND",
                $meta_query,
                $new_condition,
            ]);
        } else {
            $query->set("meta_query", [$new_condition]);
        }
    }

    /**
     * SAFE settings validation helper
     * Returns true ONLY when feature should be ACTIVE
     */
    private static function should_hide_orders(): bool
    {
        $settings = get_option("woocommerce_wizit_settings", []);

        // Only hide orders when setting explicitly DISABLES "show all incomplete"
        // (Matches your original logic: hide when setting = "yes")
        return is_array($settings) &&
            isset($settings["show_all_incomplete_orders_in_order_list"]) &&
            "no" === $settings["show_all_incomplete_orders_in_order_list"];
    }
}
