<?php

/**
 * Plugin Name: Noesia Commerce Suite for WooCommerce
 * Description: Order Care Hub, reorder reminders, customer care, intelligence, impact maps and ecommerce health checks for WooCommerce stores.
 * Version: 0.7.34
 * Author: Noesiaswa
 * Author URI: https://noesia.io
 * Text Domain: noesia-commerce-suite
 * Domain Path: /languages
 * Requires at least: 6.2
 * Requires PHP: 7.4
 * WC requires at least: 8.0
 * WC tested up to: 9.9
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package Noesia_Commerce_Suite
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'noesia_cs_fs' ) ) {
    noesia_cs_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'noesia_cs_fs' ) ) {
        // Create a helper function for easy SDK access.
        function noesia_cs_fs() {
            global $noesia_cs_fs;
            if ( !isset( $noesia_cs_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $noesia_cs_fs = fs_dynamic_init( array(
                    'id'               => '31979',
                    'slug'             => 'noesia-commerce-suite',
                    'premium_slug'     => 'noesia-commerce-suite-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_9c11056f29a3f256386ad20ef062e',
                    'is_premium'       => false,
                    'premium_suffix'   => 'pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => false,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'    => 'noesia-commerce-suite',
                        'account' => true,
                        'support' => true,
                    ),
                    'is_live'          => true,
                ) );
            }
            return $noesia_cs_fs;
        }

        // Init Freemius.
        noesia_cs_fs();
        // Signal that SDK was initiated.
        do_action( 'noesia_cs_fs_loaded' );
    }
    if ( !function_exists( 'noesia_cs_uninstall_cleanup' ) ) {
        /**
         * Clean up plugin-owned data after Freemius has reported the uninstall event.
         *
         * By default Noesia Commerce Suite keeps plugin data on uninstall. Cleanup runs
         * only when the store owner explicitly enables the "Remove Noesia Data on
         * Uninstall" setting. WooCommerce orders, products, customers, coupons,
         * payments and WordPress users are never deleted here.
         *
         * @return void
         */
        function noesia_cs_uninstall_cleanup() {
            $noesia_cs_settings = get_option( 'noesia_cs_settings', array() );
            if ( !is_array( $noesia_cs_settings ) || !isset( $noesia_cs_settings['delete_data_on_uninstall'] ) || 'yes' !== $noesia_cs_settings['delete_data_on_uninstall'] ) {
                return;
            }
            delete_option( 'noesia_cs_settings' );
            delete_option( 'noesia_cs_db_version' );
            delete_option( 'noesia_cs_doctor_report' );
            delete_option( 'noesia_cs_reorder_queue' );
            delete_option( 'noesia_cs_reorder_logs' );
            delete_option( 'noesia_cs_reorder_sent_keys' );
            delete_option( 'noesia_cs_email_logs' );
            delete_option( 'noesia_cs_care_action_logs' );
            delete_option( 'noesia_cs_last_cleanup_summary' );
            delete_transient( 'noesia_cs_dashboard_stats_v1' );
            delete_transient( 'noesia_cs_care_action_summary_v1' );
            foreach ( array(
                7,
                30,
                90,
                365
            ) as $noesia_cs_range ) {
                delete_transient( 'noesia_cs_dashboard_stats_v2_' . $noesia_cs_range );
            }
        }

    }
    noesia_cs_fs()->add_action( 'after_uninstall', 'noesia_cs_uninstall_cleanup' );
    define( 'NOESIA_CS_VERSION', '0.7.34' );
    define( 'NOESIA_CS_FILE', __FILE__ );
    define( 'NOESIA_CS_PATH', plugin_dir_path( __FILE__ ) );
    define( 'NOESIA_CS_URL', plugin_dir_url( __FILE__ ) );
    define( 'NOESIA_CS_BASENAME', plugin_basename( __FILE__ ) );
    define( 'NOESIA_CS_CAPABILITY', 'manage_woocommerce' );
    /**
     * Load optional Pro package when present.
     *
     * The WordPress.org Free package must continue to work when /pro/ is missing.
     */
    if ( file_exists( NOESIA_CS_PATH . 'pro/class-noesia-cs-pro-loader.php' ) ) {
        require_once NOESIA_CS_PATH . 'pro/class-noesia-cs-pro-loader.php';
        if ( class_exists( 'Noesia_CS_Pro_Loader' ) ) {
            Noesia_CS_Pro_Loader::init();
        }
    }
    /**
     * Load a plugin file relative to the plugin root.
     *
     * @param string $relative_path Relative file path.
     * @param bool   $required      Whether the file is required.
     * @return bool True when loaded, false otherwise.
     */
    function noesia_cs_require_file(  $relative_path, $required = true  ) {
        $path = NOESIA_CS_PATH . ltrim( $relative_path, '/' );
        if ( file_exists( $path ) ) {
            require_once $path;
            return true;
        }
        if ( $required ) {
            do_action( 'noesia_cs_missing_required_file', sanitize_text_field( $relative_path ) );
        }
        return false;
    }

    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-activator.php';
    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-deactivator.php';
    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-settings.php';
    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-container.php';
    require_once NOESIA_CS_PATH . 'includes/helpers/class-noesia-cs-template-loader.php';
    require_once NOESIA_CS_PATH . 'includes/modules/class-noesia-cs-admin-module.php';
    foreach ( array(
        'modules/dashboard/class-noesia-cs-dashboard-module.php',
        'modules/order-care-hub/class-noesia-cs-order-care-hub-module.php',
        'modules/product-passport-qr/class-noesia-cs-product-passport-qr-module.php',
        'modules/order-care-page/class-noesia-cs-order-care-page-module.php',
        'modules/reorder-reminders/class-noesia-cs-reorder-reminders-module.php',
        'modules/reviews-support/class-noesia-cs-reviews-support-module.php',
        'modules/intelligence-analytics/class-noesia-cs-intelligence-analytics-module.php',
        'modules/impact-map/class-noesia-cs-impact-map-module.php',
        'modules/ecommerce-doctor/class-noesia-cs-ecommerce-doctor-module.php',
        'modules/emails/class-noesia-cs-emails-module.php',
        'modules/settings/class-noesia-cs-settings-module.php',
        'modules/upgrade-pro/class-noesia-cs-upgrade-pro-module.php'
    ) as $noesia_cs_module_file ) {
        noesia_cs_require_file( $noesia_cs_module_file, false );
    }
    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-module-registry.php';
    require_once NOESIA_CS_PATH . 'includes/repositories/class-noesia-cs-order-repository.php';
    require_once NOESIA_CS_PATH . 'includes/repositories/class-noesia-cs-product-repository.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-passport-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-warranty-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-analytics-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-doctor-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-impact-map-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-email-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-reorder-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-data-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-feature-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-system-status-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-care-action-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-settings-binding-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-service-boundary-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-weather-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-admin-search-service.php';
    require_once NOESIA_CS_PATH . 'includes/services/class-noesia-cs-commerce-view-service.php';
    require_once NOESIA_CS_PATH . 'includes/public/class-noesia-cs-public.php';
    noesia_cs_require_file( 'includes/admin/class-noesia-cs-admin-renderer.php', false );
    noesia_cs_require_file( 'includes/admin/class-noesia-cs-admin-action-router.php', false );
    require_once NOESIA_CS_PATH . 'includes/admin/class-noesia-cs-admin.php';
    require_once NOESIA_CS_PATH . 'includes/class-noesia-cs-plugin.php';
    register_activation_hook( __FILE__, array('Noesia_CS_Activator', 'activate') );
    register_deactivation_hook( __FILE__, array('Noesia_CS_Deactivator', 'deactivate') );
    add_action( 'before_woocommerce_init', function () {
        if ( class_exists( '\\Automattic\\WooCommerce\\Utilities\\FeaturesUtil' ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        }
    } );
    /**
     * Return the main plugin singleton.
     *
     * @return Noesia_CS_Plugin
     */
    function noesia_cs_plugin() {
        return Noesia_CS_Plugin::instance();
    }

    /**
     * Return the feature service.
     *
     * @return Noesia_CS_Feature_Service
     */
    function noesia_cs_feature_service() {
        return noesia_cs_plugin()->feature_service();
    }

    /**
     * Check whether premium features are available.
     *
     * @return bool
     */
    function noesia_cs_is_pro_active() {
        return noesia_cs_feature_service()->is_pro_active();
    }

    /**
     * Check whether a feature can be used.
     *
     * @param string $feature Feature key.
     * @return bool
     */
    function noesia_cs_can_use_feature(  $feature  ) {
        return noesia_cs_feature_service()->can_use( $feature );
    }

    add_action( 'plugins_loaded', array(noesia_cs_plugin(), 'init') );
}