<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://wpgeenie.org
 * @since      1.0.0
 *
 * @package    Spending_Limits_For_Woocommerce
 * @subpackage Spending_Limits_For_Woocommerce/includes
 */
/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Spending_Limits_For_Woocommerce
 * @subpackage Spending_Limits_For_Woocommerce/includes
 * @author     wpgeenie.org <info@wpgeenie.org>
 */
class Spending_Limits_For_Woocommerce {
    /**
     * The loader that's responsible for maintaining and registering all hooks that power
     * the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      Spending_Limits_For_Woocommerce_Loader    $loader    Maintains and registers all hooks for the plugin.
     */
    protected $loader;

    /**
     * The unique identifier of this plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $plugin_name    The string used to uniquely identify this plugin.
     */
    protected $plugin_name;

    /**
     * The current version of the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $version    The current version of the plugin.
     */
    protected $version;

    /**
     * Define the core functionality of the plugin.
     *
     * Set the plugin name and the plugin version that can be used throughout the plugin.
     * Load the dependencies, define the locale, and set the hooks for the admin area and
     * the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function __construct() {
        if ( defined( 'SPENDING_LIMITS_FOR_WOOCOMMERCE_VERSION' ) ) {
            $this->version = SPENDING_LIMITS_FOR_WOOCOMMERCE_VERSION;
        } else {
            $this->version = '1.0.0';
        }
        $this->plugin_name = 'spending-limits-for-woocommerce';
        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
    }

    /**
     * Load the required dependencies for this plugin.
     *
     * Include the following files that make up the plugin:
     *
     * - Spending_Limits_For_Woocommerce_Loader. Orchestrates the hooks of the plugin.
     * - Spending_Limits_For_Woocommerce_i18n. Defines internationalization functionality.
     * - Spending_Limits_For_Woocommerce_Admin. Defines all hooks for the admin area.
     * - Spending_Limits_For_Woocommerce_Public. Defines all hooks for the public side of the site.
     *
     * Create an instance of the loader which will be used to register the hooks
     * with WordPress.
     *
     * @since    1.0.0
     * @access   private
     */
    private function load_dependencies() {
        /**
         * The class responsible for orchestrating the actions and filters of the
         * core plugin.
         */
        require_once plugin_dir_path( __DIR__ ) . 'includes/class-spending-limits-for-woocommerce-loader.php';
        /**
         * The class responsible for defining all actions that occur in the admin area.
         */
        require_once plugin_dir_path( __DIR__ ) . 'admin/class-spending-limits-for-woocommerce-admin.php';
        /**
         * The class responsible for defining all actions that occur in the public-facing
         * side of the site.
         */
        require_once plugin_dir_path( __DIR__ ) . 'public/class-spending-limits-for-woocommerce-public.php';
        $this->loader = new Spending_Limits_For_Woocommerce_Loader();
    }

    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks() {
        $plugin_admin = new Spending_Limits_For_Woocommerce_Admin($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
        $this->loader->add_action( 'show_user_profile', $plugin_admin, 'add_user_fields_in_admin' );
        $this->loader->add_action( 'edit_user_profile', $plugin_admin, 'add_user_fields_in_admin' );
        $this->loader->add_action( 'personal_options_update', $plugin_admin, 'save_user_profile_fields' );
        $this->loader->add_action( 'edit_user_profile_update', $plugin_admin, 'save_user_profile_fields' );
        $this->loader->add_filter(
            'plugin_row_meta',
            $plugin_admin,
            'add_plugin_row_meta',
            10,
            2
        );
    }

    /**
     * Register all of the hooks related to the public-facing functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_public_hooks() {
        $plugin_public = new Spending_Limits_For_Woocommerce_Public($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
        $this->loader->add_action(
            'wp_enqueue_scripts',
            $plugin_public,
            'enqueue_scripts',
            99
        );
        $this->loader->add_action(
            'wc_get_template',
            $plugin_public,
            'wc_get_template',
            12,
            5
        );
        $this->loader->add_action(
            'init',
            $plugin_public,
            'add_my_account_endpoint',
            20
        );
        $this->loader->add_filter(
            'woocommerce_account_menu_items',
            $plugin_public,
            'add_my_account_tab',
            12,
            5
        );
        $this->loader->add_action(
            'woocommerce_account_spending-limits_endpoint',
            $plugin_public,
            'my_account_content',
            12,
            5
        );
        $this->loader->add_action( 'template_redirect', $plugin_public, 'process_form_submission' );
        $this->loader->add_action( 'template_redirect', $plugin_public, 'show_lock_notice' );
        $this->loader->add_action( 'woocommerce_checkout_process', $plugin_public, 'prevent_checkout_if_over_limit' );
        $this->loader->add_action( 'wp', $plugin_public, 'show_spending_limit_notice' );
        $this->loader->add_action(
            'woocommerce_add_to_cart',
            $plugin_public,
            'prevent_add_to_cart_if_locked',
            10,
            2
        );
        $this->loader->add_action( 'woocommerce_checkout_process', $plugin_public, 'prevent_checkout_if_locked' );
        /** For store API */
        $this->loader->add_action( 'woocommerce_store_api_checkout_update_order_from_request', $plugin_public, 'prevent_checkout_if_over_limit' );
        $this->loader->add_action( 'woocommerce_store_api_checkout_update_order_from_request', $plugin_public, 'prevent_checkout_if_locked' );
        $this->loader->add_action(
            'woocommerce_available_payment_gateways',
            $plugin_public,
            'remove_gateways_if_over_limit',
            999
        );
        $this->loader->add_action(
            'woocommerce_available_payment_gateways',
            $plugin_public,
            'remove_gateways_if_locked',
            999
        );
        $this->loader->add_filter(
            'woocommerce_no_available_payment_methods_message',
            $plugin_public,
            'no_available_payment_message_if_locked',
            999
        );
        $this->loader->add_filter(
            'woocommerce_no_available_payment_methods_message',
            $plugin_public,
            'no_available_payment_message_if_over_limit',
            999
        );
        $this->loader->add_filter(
            'wc_stripe_payment_request_cart_details',
            $plugin_public,
            'prevent_checkout_if_over_limit',
            999
        );
        $this->loader->add_filter(
            'wc_stripe_payment_request_cart_details',
            $plugin_public,
            'prevent_checkout_if_locked',
            999
        );
        $this->loader->add_action(
            'woocommerce_proceed_to_checkout',
            $plugin_public,
            'disable_checkout_button_if_limit_exceeded',
            1
        );
        $this->loader->add_action(
            'woocommerce_check_cart_items',
            $plugin_public,
            'prevent_checkout_if_over_limit',
            1
        );
        $this->loader->add_action(
            'woocommerce_check_cart_items',
            $plugin_public,
            'prevent_checkout_if_locked',
            1
        );
    }

    /**
     * Run the loader to execute all of the hooks with WordPress.
     *
     * @since    1.0.0
     */
    public function run() {
        $this->loader->run();
    }

    /**
     * The name of the plugin used to uniquely identify it within the context of
     * WordPress and to define internationalization functionality.
     *
     * @since     1.0.0
     * @return    string    The name of the plugin.
     */
    public function get_plugin_name() {
        return $this->plugin_name;
    }

    /**
     * The reference to the class that orchestrates the hooks with the plugin.
     *
     * @since     1.0.0
     * @return    Spending_Limits_For_Woocommerce_Loader    Orchestrates the hooks of the plugin.
     */
    public function get_loader() {
        return $this->loader;
    }

    /**
     * Retrieve the version number of the plugin.
     *
     * @since     1.0.0
     * @return    string    The version number of the plugin.
     */
    public function get_version() {
        return $this->version;
    }

    /**
     *  Audit log
     *
     * @param int    $user_id user id.
     * @param string $action action.
     * @param string $type type.
     * @param int    $curent_user_id current user id.
     * @return void
     */
    public static function insert_log(
        $user_id,
        $action,
        $type,
        $curent_user_id
    ) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'slfw_log';
        $wpdb->insert( 
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $table_name,
            array(
                'user_id'    => $user_id,
                'action'     => $action,
                'type'       => $type,
                'by_user_id' => $curent_user_id,
            )
         );
    }

}
