<?php
/**
 * Freemius SDK bootstrap (CF16/F56).
 *
 * Loads the vendored Freemius WordPress SDK (at /freemius/start.php)
 * with the public identity constants. Pattern mirrors the snippet
 * "Copy the code" on https://dashboard.freemius.com/#!/live/stores/18704/plugins/36025/integration/
 *
 * SECURITY NOTE:
 *   The only secrets stored here are PUBLIC identity constants:
 *     - SWAPADS_FS_PRODUCT_ID (Freemius product number — PUBLIC)
 *     - SWAPADS_FS_PUBLIC_KEY (Freemius public key — PUBLIC, safe to commit)
 *   NO server-side secret_key. The secret_key is held by Freemius's
 *   server and used only to sign webhooks they send to US.
 *   Our server (swapads-server) validates those webhooks via shared
 *   HMAC using its own local key, not the Freemius secret.
 *
 * For local development, define in wp-config.php:
 *     define('WP_FS__DEV_MODE', true);
 *     define('WP_FS__SKIP_EMAIL_ACTIVATION', true);
 *
 * THIS FILE IS NOT NAMESPACE-SCOPED.
 *   The Freemius helper function swa_fs() MUST live in the global
 *   namespace (matching Freemius's own dashboard snippet), otherwise
 *   it cannot be invoked from other plugins, hooks, or the SDK itself.
 *   All our application code lives in SwapAds\Client\*; only this
 *   single bootstrap file is global.
 *
 * @package SwapAds\Client
 * @since   1.1.0
 */

declare(strict_types=1);

/**
 * Global execution guard: only run when WordPress is bootstrapped.
 * The plugin file (swapads-client.php) checks file_exists() before
 * including this, so we won't get here from PHP CLI, PHPUnit, etc.
 */
if ( ! defined( 'ABSPATH' ) ) {
    return;
}

// Freemius identity constants — verified from dashboard on 2026-07-28.
// These are PUBLIC values (Freemius themselves expose them in the
// integration snippet you "Copy the code" from). Safe to commit.
\define('SWAPADS_FS_PRODUCT_ID',  36025);
\define('SWAPADS_FS_PLUGIN_SLUG', 'swapads');
\define('SWAPADS_FS_PUBLIC_KEY',  'pk_87dba808720228fe7dc59ae226384');
\define('SWAPADS_FS_TYPE',        'plugin');
\define('SWAPADS_FS_HELPER',      'swa_fs');

if ( ! function_exists( SWAPADS_FS_HELPER ) ) {
    /**
     * Helper function for easy SDK access.
     *
     * Singleton initialization of the Freemius SDK instance.
     * Pattern copied from the Freemius dashboard integration snippet.
     *
     * @return \Freemius The SDK instance.
     */
    function swa_fs() {
        global $swa_fs;

        if ( ! isset( $swa_fs ) ) {
            // Include Freemius SDK.
            require_once __DIR__ . '/freemius/start.php';

            $swa_fs = fs_dynamic_init( array(
                'id'                => SWAPADS_FS_PRODUCT_ID,
                'slug'              => SWAPADS_FS_PLUGIN_SLUG,
                'type'              => SWAPADS_FS_TYPE,
                'public_key'        => SWAPADS_FS_PUBLIC_KEY,
                'is_premium'        => false,
                'has_addons'        => false,
                'has_paid_plans'    => false,
                'is_org_compliant'  => true,
                'menu'              => array(
                    'slug'    => 'swapads-client',
                    'account' => false,
                    'support' => false,
                    'contact' => false,
                ),
            ) );
        }

        return $swa_fs;
    }

    // Init Freemius.
    swa_fs();

    // Signal that SDK was initiated.
    do_action( SWAPADS_FS_HELPER . '_loaded' );
}
