<?php
/**
 * Plugin Name: Webshoptiger Catalog Sync for WooCommerce
 * Plugin URI:  https://shop-sync.nl
 * Description: Sync products, categories, and brands between multiple WooCommerce stores. Upgrade to Pro for AI-powered description rewriting, price adjustments per site, selective field sync, and more.
 * Version:     1.2.0
 * Author:      Webshoptiger B.V.
 * Author URI:  https://shop-writer.com
 * Text Domain: catalog-sync-for-woocommerce
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires Plugins: woocommerce
 * WC requires at least: 7.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Core constants — always defined regardless of Freemius state
if ( ! defined( 'WEBSHOPTIGERCATALOGSYNC_VERSION' ) ) {
    define( 'WEBSHOPTIGERCATALOGSYNC_VERSION',     '1.2.0' );
    define( 'WEBSHOPTIGERCATALOGSYNC_EDITION',     'pro' );   // Actual Pro behavior gated by Freemius license check via Settings::is_pro()
    define( 'WEBSHOPTIGERCATALOGSYNC_FILE',        __FILE__ );
    define( 'WEBSHOPTIGERCATALOGSYNC_PATH',        plugin_dir_path( __FILE__ ) );
    define( 'WEBSHOPTIGERCATALOGSYNC_URL',         plugin_dir_url( __FILE__ ) );
    define( 'WEBSHOPTIGERCATALOGSYNC_PLUGIN_NAME', 'Webshoptiger Catalog Sync for WooCommerce' );
    define( 'WEBSHOPTIGERCATALOGSYNC_PLUGIN_SLUG', 'catalog-sync-for-woocommerce' );
    define( 'WEBSHOPTIGERCATALOGSYNC_LITE_PRODUCT_LIMIT', 30 );
    define( 'WEBSHOPTIGERCATALOGSYNC_LITE_SITE_LIMIT',    1  );
}

if ( function_exists( 'webshoptigercatalogsync_fs' ) ) {
    webshoptigercatalogsync_fs()->set_basename( true, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    if ( ! function_exists( 'webshoptigercatalogsync_fs' ) ) {
        // Create a helper function for easy SDK access.
        function webshoptigercatalogsync_fs() {
            global $webshoptigercatalogsync_fs;

            if ( ! isset( $webshoptigercatalogsync_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

                $webshoptigercatalogsync_fs = fs_dynamic_init( array(
                    'id'                  => '33075',
                    'slug'                => 'catalog-sync-for-woocommerce',
                    'premium_slug'        => 'catalog-sync-for-woocommerce-premium',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_ffca25952593712f042f717c017b6',
                    'is_premium'          => false,
                    'premium_suffix'      => 'Professional',
                    // If your plugin is a serviceware, set this option to false.
                    'has_premium_version' => true,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'is_org_compliant'    => true,
                    'trial'               => array(
                        'days'               => 7,
                        'is_require_payment' => false,
                    ),
                    'menu'                => array(
                        'slug'           => 'webshoptigercatalogsync-sync',
                        'first-path'     => 'admin.php?page=webshoptigercatalogsync-sync',
                        'support'        => false,
                    ),
                    'navigation'          => 'menu',
                    'tos_url'             => 'https://shop-sync.nl/terms',
                    'privacy_policy_url'  => 'https://shop-sync.nl/privacy',
                ) );
            }

            return $webshoptigercatalogsync_fs;
        }

        // Init Freemius.
        webshoptigercatalogsync_fs();
        // Signal that SDK was initiated.
        do_action( 'webshoptigercatalogsync_fs_loaded' );

        // Auto-restore license after reinstall.
        // When the plugin is deleted and reinstalled, Freemius loses its
        // local license state. This saves the license key to a separate
        // wp_options entry on every successful activation, and silently
        // restores it on the next activation without requiring the user
        // to re-enter the license key manually.
        add_action( 'admin_init', function () {
            if ( ! function_exists( 'webshoptigercatalogsync_fs' ) ) return;
            $fs = webshoptigercatalogsync_fs();

            // Save the license key whenever the license is active
            if ( $fs->is_paying() || $fs->is_trial() ) {
                $license = $fs->_get_license();
                if ( is_object( $license ) && ! empty( $license->secret_key ) ) {
                    update_option(
                        'webshoptigercatalogsync_saved_license_key',
                        base64_encode( $license->secret_key ),
                        false
                    );
                }
            }

            // Restore the license key on a fresh install if not yet activated
            if ( ! $fs->is_registered() && ! $fs->is_anonymous() ) {
                $stored = get_option( 'webshoptigercatalogsync_saved_license_key', '' );
                if ( ! empty( $stored ) ) {
                    $license_key = base64_decode( $stored );
                    if ( ! empty( $license_key ) ) {
                        $fs->activate_migrated_license( $license_key );
                    }
                }
            }
        } );
    }
}

// Autoload classes — always registered
spl_autoload_register( function ( $class ) {
    $prefix = 'WebshoptigerCatalogSync\\';
    if ( strpos( $class, $prefix ) !== 0 ) return;
    $relative = str_replace( [ $prefix, '\\' ], [ '', '/' ], $class );

    $locations = [
        WEBSHOPTIGERCATALOGSYNC_PATH . 'includes/' . $relative . '.php',
        WEBSHOPTIGERCATALOGSYNC_PATH . 'admin/' . str_replace( 'Admin/', '', $relative ) . '.php',
    ];
    foreach ( $locations as $file ) {
        if ( file_exists( $file ) ) {
            require $file;
            return;
        }
    }
} );

register_activation_hook( __FILE__, [ 'WebshoptigerCatalogSync\\Installer', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'WebshoptigerCatalogSync\\Installer', 'deactivate' ] );

add_action( 'plugins_loaded', function () {
    if ( ! class_exists( 'WooCommerce' ) ) {
        add_action( 'admin_notices', function () {
            echo '<div class="notice notice-error"><p><strong>' . esc_html( WEBSHOPTIGERCATALOGSYNC_PLUGIN_NAME ) . '</strong> requires WooCommerce.</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        } );
        return;
    }

    \WebshoptigerCatalogSync\Plugin::get_instance();
}, 20 );
