<?php

/**
 * Plugin Name: Dobare Customer Loyalty Club | باشگاه مشتریان دوباره
 * Version: 1.3.7
 * Author: Idekavan Group, dobare
 * Description: Dobare Customer Loyalty Club boosts WooCommerce loyalty with SMS-based cashback reminders tailored for Iranian stores.
 * Author URI: https://dobare.me
 * Text Domain: dobare
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 * WC requires at least: 7.1.0
 * @package Dobare
 */

defined('ABSPATH') || exit;
$dobare_version = '1.3.7';
if (! defined('DOBARE_MAIN_PLUGIN_FILE')) {
	define('DOBARE_MAIN_PLUGIN_FILE', __FILE__);
}
if (! defined('DOBARE_BASE_URL')) {
	define('DOBARE_BASE_URL', "https://api.dobare.me/");
}
if (! defined('DOBARE_API_KEY_OPTION_KEY')) {
	define('DOBARE_API_KEY_OPTION_KEY', "dobare_api_key");
}
if (! defined('DOBARE_OLD_API_KEY_OPTION_KEY')) {
	define('DOBARE_OLD_API_KEY_OPTION_KEY', "dobare_old_api_key");
}
if (! defined('DOBARE_VERSION')) {
	define('DOBARE_VERSION', $dobare_version);
}

require_once plugin_dir_path(__FILE__) . '/vendor/autoload.php';

use Dobare\Admin\API\Dashboard\Dobare_Dashboard_REST_Controller;
use Dobare\Admin\API\Merchant_Journey\Dobare_Merchant_Journey_REST_Controller;
use Dobare\Admin\Dobare_Notices;
use Dobare\Admin\Dobare_Setup;
use Dobare\Admin\Dobare_Plugin_Action_Links;
use Dobare\Checkout\API\Dobare_Checkout_REST_Controller;
use Dobare\Core\Dobare_Core;
use Dobare\Core\Dobare_Digits_Integration;
use Dobare\Core\Dobare_Enterx_Integration;
use Dobare\Core\Dobare_Mobits_Integration;
use Dobare\Core\Dobare_Voorodak_Integration;
use Dobare\Lead_Magnet\Dobare_Lead_Magnet_REST_Controller;
use Dobare\Customer_Credit\API\Dobare_Customer_Credit_REST_Controller;
use Dobare\Settings\API\Dobare_Plugin_Settings_REST_Controller;
use Dobare\My_Account\Dobare_My_Account;
use Dobare\Utils\Dobare_Utils;
use Dobare\Abandoned_Cart\Dobare_Abandoned_Cart_Service;
use Dobare\Abandoned_Cart\Dobare_Abandoned_Cart_Migrator;



/**
 * WooCommerce fallback notice.
 *
 * @since 0.1.0
 */
function dobare_missing_wc_notice()
{
	/* translators: %s WC download URL link. */
	echo '<div class="error"><p><strong>' . sprintf(esc_html__('Dobare requires WooCommerce to be installed and active. You can download %s here.', 'dobare'), '<a href="https://woo.com/" target="_blank">WooCommerce</a>') . '</strong></p></div>';
}

register_activation_hook(__FILE__, 'dobare_activate');
register_deactivation_hook(__FILE__, 'dobare_deactivate');
register_uninstall_hook(__FILE__, 'dobare_uninstall');
/**
 * Activation hook.
 *
 * @since 0.1.0
 */
function dobare_activate()
{
	if (! class_exists('WooCommerce')) {
		add_action('admin_notices', 'dobare_missing_wc_notice');

		return;
	}
	$ship_to_destination = get_option('woocommerce_ship_to_destination');
	if (! $ship_to_destination != 'billing_only') {
		update_option('woocommerce_ship_to_destination', 'billing_only', 'no');
	}
	Dobare_Abandoned_Cart_Migrator::maybe_migrate();
	Dobare_Utils::set_state('active');
}


function dobare_deactivate()
{
	Dobare_Utils::set_state('inactive');
}

function dobare_uninstall()
{
	Dobare_Abandoned_Cart_Migrator::drop_table();
}

if (! class_exists('Dobare')) :
	/**
	 * The Dobare class.
	 */
	class Dobare
	{
		/**
		 * This class instance.
		 *
		 * @var Dobare single instance of this class.
		 */
		private static $instance;

		/**
		 * Constructor.
		 */
		public function __construct()
		{
		$merchant_journey_controller = new Dobare_Merchant_Journey_REST_Controller();
		$dashboard_controller        = new Dobare_Dashboard_REST_Controller();
		$checkout_controller         = new Dobare_Checkout_REST_Controller();
		$lead_magnet_controller      = new Dobare_Lead_Magnet_REST_Controller();
		$customer_credit_controller  = new Dobare_Customer_Credit_REST_Controller();
		$plugin_settings_controller = new Dobare_Plugin_Settings_REST_Controller();
		$core_class                  = new Dobare_Core();
		$digits_integration_class    = new Dobare_Digits_Integration();
		$mobits_integration_class    = new Dobare_Mobits_Integration();
		$voorodak_integration_class  = new Dobare_Voorodak_Integration();
		$enterx_integration_class    = new Dobare_Enterx_Integration();
		$my_account_class            = new Dobare_My_Account();
		$notices                     = new Dobare_Notices();
		$plugin_action_links         = new Dobare_Plugin_Action_Links();
		Dobare_Abandoned_Cart_Service::instance()->bootstrap();
		add_action('rest_api_init', array($merchant_journey_controller, "register_routes"));
		add_action('rest_api_init', array($dashboard_controller, "register_routes"));
		add_action('rest_api_init', array($checkout_controller, "register_routes"));
		add_action('rest_api_init', array($lead_magnet_controller, "register_routes"));
		add_action('rest_api_init', array($customer_credit_controller, "register_routes"));
		add_action('rest_api_init', array($plugin_settings_controller, "register_routes"));
		add_action(
			'woocommerce_blocks_checkout_block_registration',
			function ($integration_registry) {
				$integration_registry->register(new Dobare_Integration());
			}
		);

		if (is_admin()) {
			new Dobare_Setup();
		}
	}

		/**
		 * Cloning is forbidden.
		 */
		public function __clone()
		{
			wc_doing_it_wrong(__FUNCTION__, __('Cloning is forbidden.', 'dobare'), $this->version);
		}

		/**
		 * Unserializing instances of this class is forbidden.
		 */
		public function __wakeup()
		{
			wc_doing_it_wrong(__FUNCTION__, __('Unserializing instances of this class is forbidden.', 'dobare'), $this->version);
		}

		/**
		 * Gets the main instance.
		 *
		 * Ensures only one instance can be loaded.
		 *
		 * @return Dobare
		 */
		public static function instance()
		{

			if (null === self::$instance) {
				self::$instance = new self();
			}

			return self::$instance;
		}
	}
endif;

add_action('plugins_loaded', 'dobare_init', 10);
add_action('wp_enqueue_scripts', 'dobare_plugin_enqueue_api_script');
add_action('admin_enqueue_scripts', 'dobare_plugin_enqueue_api_script');
add_action('login_enqueue_scripts', 'dobare_plugin_enqueue_api_script');

function dobare_plugin_enqueue_api_script()
{
	wp_enqueue_script('wp-api');
}

/**
 * Initialize the plugin.
 *
 * @since 0.1.0
 */
function dobare_init()
{
	if (! class_exists('WooCommerce')) {
		add_action('admin_notices', 'dobare_missing_wc_notice');

		return;
	}
	dobare::instance();
}

add_action(
	'dobare_check_remote_sync_status',
	function () {
		// Check if the Dobare utilities class is available
		$progress = Dobare_Utils::get_sync_customers_progress();

		// Check remote status
		Dobare_Utils::check_remote_sync_status();

		// Get updated progress after check
		$updated_progress = Dobare_Utils::get_sync_customers_progress();

		// If not complete or error, schedule another check
		if ($updated_progress['status'] !== 'completed' && $updated_progress['status'] !== 'error') {
			// Schedule next check in 30 seconds
			wp_schedule_single_event(time() + 30, 'dobare_check_remote_sync_status');
		}
	}
);
