<?php
/**
 * Freemius SDK Integration (shared free + pro).
 *
 * Moved out of pro/ so the free wp.org build (which .distignore strips pro/ from)
 * also boots Freemius. This is what lets free users reach checkout/upgrade in wp-admin.
 * is_premium is derived from lean_csns_is_pro_active() so one file serves both builds.
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'lean_csns_fs' ) ) {
	/**
	 * Create/return the global Freemius instance for this plugin.
	 *
	 * @return Freemius
	 */
	function lean_csns_fs() {
		global $lean_csns_fs;

		if ( ! isset( $lean_csns_fs ) ) {
			// Include Freemius SDK (SDK now lives beside this file in includes/).
			require_once dirname( __FILE__ ) . '/freemius/start.php';

			// Set custom icon image for freemius opt-in screen
			add_filter( 'fs_plugin_icon_lean-cart-share-and-save', function () {
				$plugin_folder = dirname( plugin_basename( LEAN_CSNS_PL_FILE ) );
				return WP_PLUGIN_DIR . '/' . $plugin_folder . '/assets/img/icon-300x300.png';
			} );

			$is_pro = function_exists( 'lean_csns_is_pro_active' ) && lean_csns_is_pro_active();

			// Freemius config
			$lean_csns_fs = fs_dynamic_init( array(
				'id'                  => '33307',
				'slug'                => 'lean-cart-share-and-save',  // free wp.org slug
				'premium_slug'        => 'lean-cart-share-and-save-pro',
				'type'                => 'plugin',
				'public_key'          => 'pk_f94070826689f7b13363f9ffb9951',
				'is_premium'          => $is_pro,
				'premium_suffix'      => 'Premium',
				'has_premium_version' => true,
				'is_premium_only'     => false,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'is_org_compliant'    => true,
				// Automatically removed in the free version. If you're not using the
				// auto-generated free version, delete this line before uploading to wp.org.
				'menu'                => array(
					// Port with our existing menu
					'slug'       => 'lean-csns-settings',
					'capability' => 'manage_options',
					'support'    => false,
					'pricing'    => true,               // free users need the pricing/upgrade page
					'contact'    => false,
				),
			) );
		}

		return $lean_csns_fs;
	}

	// Init Freemius
	lean_csns_fs();

	// Data wipe on uninstall goes through Freemius' after_uninstall hook, NOT an
	// uninstall.php. Freemius runs its own uninstall via register_uninstall_hook,
	// and WP skips uninstall.php when a registered hook exists, so shipping
	// uninstall.php would block Freemius' own cleanup.
	lean_csns_fs()->add_action( 'after_uninstall', 'lean_csns_uninstall_cleanup' );

	// Signal that SDK was initiated
	do_action( 'lean_csns_fs_loaded' );

	// Sends opt-in/diagnostic data
	require_once LEAN_CSNS_PL_DIR . 'includes/diagnostic-data.php';
}

if ( ! function_exists( 'lean_csns_uninstall_cleanup' ) ) {
	/**
	 * Delete plugin data on uninstall. Only wipes when the user opted in via
	 * data_management.delete_on_uninstall. Kept dependency-light (just $wpdb +
	 * options); table name mirrors class-database.php.
	 */
	function lean_csns_uninstall_cleanup() {
		$settings = get_option( 'lean_csns_settings', array() );

		// Bail unless the user explicitly opted in to data deletion.
		$opted_in = isset( $settings['data_management']['delete_on_uninstall'] )
			&& $settings['data_management']['delete_on_uninstall'] === '1';

		if ( ! $opted_in ) {
			return;
		}

		global $wpdb;

		$table_name = $wpdb->prefix . 'lean_csns_carts';

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Plugin uninstall cleanup
		$wpdb->query( "DROP TABLE IF EXISTS `{$table_name}`" );

		delete_option( 'lean_csns_settings' );
		delete_option( 'lean_csns_db_version' );
		delete_option( 'lean_csns_first_install' );
		wp_cache_delete( 'lean_csns_settings', 'options' );
	}
}
