<?php

/**
 * Rivio  Cart Abandon, Recovery & More
 * Plugin Name: Rivio – Customer Retention, AI Emails & Cart Recovery for WooCommerce (Beta)
 * Version:     0.5.3
 * Description: Rivio (Beta) helps WooCommerce stores increase customer retention, recover abandoned carts, automate AI-powered emails, gain actionable insights, and grow revenue with advanced marketing automation.
 * Author:      Pixelio
 * Author URI:  https://www.rivio.be/
 * Text Domain: rivio-ai-cart-recovery-marketing-email-automation
 * Domain Path: /languages/
 * License:     GPL v3
 * Requires at least: 6.3
 * Tested up to: 7.0
 * Requires PHP: 8.0
 *
 * WC requires at least: 7.9.0
 * WC tested up to: 10.0
 *  Requires Plugins: woocommerce
 *  Commerce-HPOS-Compatible: yes
 *
 * @package  PixelioCartAbandonRecoveryMore
 **/
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;
if ( function_exists( 'pixelio_carm_fss' ) ) {
    pixelio_carm_fss()->set_basename( false, __FILE__ );
} else {
    if ( !defined( 'WP_PIXELIO_TEST' ) ) {
        require_once __DIR__ . '/vendor/autoload.php';
        /**
         * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
         * `function_exists` CALL ABOVE TO PROPERLY WORK.
         */
        if ( !function_exists( 'pixelio_carm_fss' ) ) {
            // Create a helper function for easy SDK access.
            function pixelio_carm_fss() {
                global $pixelio_carm_fss;
                if ( !isset( $pixelio_carm_fss ) ) {
                    // Include Freemius SDK.
                    // SDK is auto-loaded through Composer
                    $pixelio_carm_fss = fs_dynamic_init( array(
                        'id'               => '16794',
                        'slug'             => 'rivio-ai-cart-recovery-marketing-email-automation',
                        'premium_slug'     => 'rivio-ai-cart-recovery-marketing-email-automation',
                        'type'             => 'plugin',
                        'public_key'       => 'pk_08b0ed0120c37ee855829a7001339',
                        'is_premium'       => false,
                        'has_addons'       => false,
                        'has_paid_plans'   => true,
                        'is_org_compliant' => true,
                        'menu'             => array(
                            'slug'       => "rivio-ai-cart-recovery-marketing-email-automation",
                            'first-path' => 'admin.php?page=rivio-first-config',
                            'contact'    => false,
                            'support'    => false,
                        ),
                        'is_live'          => true,
                    ) );
                }
                return $pixelio_carm_fss;
            }

            // Init Freemius.
            pixelio_carm_fss();
            // Signal that SDK was initiated.
            do_action( 'pixelio_carm_fss_loaded' );
        }
    }
    /**
     * Initializes and starts the Pixelio Cart Abandon Recovery More plugin.
     *
     * Loads the autoloader, registers it, and initializes the main plugin class.
     * Sets plugin version and base name. Registers activation, deactivation,
     * and uninstall hooks. Ensures a single global instance of the plugin.
     *
     * PHP version 8.0+
     *
     * @return PixelioCartAbandonRecoveryMore\Pixelio_Cart_Abandon_Recovery_More_Init Global plugin instance.
     * @version  0.1
     * @author   Pixelio
     *
     * @package  PixelioCartAbandonRecoveryMore
     */
    function pixelio_cart_abandon_recovery_more_start_plugin() {
        require_once plugin_dir_path( __FILE__ ) . 'class-pixelio-cart-abandon-recovery-more-autoloader.php';
        Pixelio_Cart_Abandon_Recovery_More_Autoloader::pixelio_cart_abandon_recovery_more_register();
        global $pixelio_cart;
        Pixelio_Cart_Abandon_Recovery_More_Util::$plugin_version = 0.1;
        if ( !isset( $pixelio_cart ) ) {
            PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util::$plugin_base_name = plugin_basename( __FILE__ );
            $pixelio_cart = new PixelioCartAbandonRecoveryMore\Pixelio_Cart_Abandon_Recovery_More_Init();
            $pixelio_cart->pixelio_cart_abandon_recovery_more_init();
            register_uninstall_hook( __FILE__, array($pixelio_cart, 'pixelio_cart_abandon_recovery_more_uninstall') );
            register_deactivation_hook( __FILE__, array($pixelio_cart, 'pixelio_cart_abandon_recovery_more_deactivation') );
            register_activation_hook( __FILE__, array($pixelio_cart, 'pixelio_cart_abandon_recovery_more_activation') );
        }
        return $pixelio_cart;
    }

    add_action( 'woocommerce_init', function () {
        // Déclare la compatibilité avec les custom order tables si WC version récente
        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        }
        // Instancie ton wrapper et util
        $wordpress_wrapper = new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper();
        $woocommerce_wrapper = new Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper();
        $pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util($wordpress_wrapper);
        $uuid_repo = new Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository($wordpress_wrapper);
        $user_consent_repo = new Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository($wordpress_wrapper);
        $uuid_visit = $wordpress_wrapper->pixelio_sanitize_text_field( $wordpress_wrapper->pixelio_wc_get_session( 'uuid' ) );
        $user_id = $uuid_repo->pixelio_get_user_id_by_uuid( $uuid_visit );
        $user_email = $user_consent_repo->pixelio_get_email_by_user_id( $user_id );
        $consent = $user_consent_repo->pixelio_is_subscribed( $user_email, Pixelio_Cart_Abandon_Recovery_More_Util::$finalite_cart );
        if ( !$wordpress_wrapper->pixelio_is_user_logged_in() && !$pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( 'pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox' ) || $pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked( 'pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox' ) ) {
            $woocommerce_wrapper->pixelio_woocommerce_register_additional_checkout_field( $pixelio_util->pixelio_cart_abandon_recovery_more_get_rgpd_field_config( $consent ) );
        }
    } );
    pixelio_cart_abandon_recovery_more_start_plugin();
    register_activation_hook( __FILE__, 'pixelio_plugin_activate' );
    /**
     * Handles plugin activation tasks.
     *
     * Sets a flag in the database on the first activation to trigger
     * a redirect or other setup actions. This ensures that any
     * initialization needed on first activation can be performed.
     *
     * @return void
     */
    function pixelio_plugin_activate() {
        if ( !get_option( 'pixelio_first_activation_done' ) ) {
            add_option( 'pixelio_do_activation_redirect', true );
        }
    }

}
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
    }
} );