<?php

/**
 * Freemius SDK integration and licensing functions.
 *
 * @package Carticy\Conversions
 */
declare (strict_types = 1);
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( !defined( 'CARTICY_CONVERSIONS_FS_ID' ) ) {
    // Real Carticy Conversions Freemius app credentials. The public key is
    // safe to commit; the matching secret key lives only in the local
    // wp-config.php (WP_FS__carticy-conversions-for-woocommerce_SECRET_KEY).
    define( 'CARTICY_CONVERSIONS_FS_ID', '32563' );
    define( 'CARTICY_CONVERSIONS_FS_PUBLIC_KEY', 'pk_6109bd6a8bab4a9054c3ce3f195f3' );
}
// Opt the product into the Freemius multisite network integration. The SDK
// reads this constant (never a fs_dynamic_init array key) to enable the
// network-level settings page; the Freemius dashboard generates the same
// guard before init when "activate the multisite network integration" is on.
if ( !defined( 'WP_FS__PRODUCT_32563_MULTISITE' ) ) {
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Freemius SDK contract: the multisite opt-in constant name is fixed as WP_FS__PRODUCT_<id>_MULTISITE.
    define( 'WP_FS__PRODUCT_32563_MULTISITE', true );
}
/**
 * Whether real Freemius app credentials are configured.
 *
 * Defensive guard retained from the placeholder era: it is a no-op now that
 * real credentials are in place (the real id is not '00000'), but it keeps
 * fs_dynamic_init() from ever registering a zero/placeholder product id
 * against the live Freemius API if the constants are blanked in a fork.
 *
 * @return bool
 */
function carticy_conversions_fs_credentials_ready() : bool {
    // Read the constants through constant() so static analysis does not
    // collapse this check against the literal values; the check stays
    // meaningful regardless of what the constants are defined to.
    $id = (string) constant( 'CARTICY_CONVERSIONS_FS_ID' );
    $key = (string) constant( 'CARTICY_CONVERSIONS_FS_PUBLIC_KEY' );
    return '00000' !== $id && 0 !== (int) $id && 0 === strpos( $key, 'pk_' );
}

if ( !function_exists( 'carticy_conversions_fs' ) ) {
    /**
     * Get the Freemius SDK instance.
     *
     * Initialization is HARD-GATED on real credentials: with the placeholder
     * app id, fs_dynamic_init() must never run - another active plugin (e.g.
     * Checkout Shield) can load the Freemius SDK and make fs_dynamic_init()
     * exist, and registering product id 00000 against the live Freemius API
     * would surface bogus opt-in screens and pings.
     *
     * @return \Freemius|null Freemius instance when the SDK is loaded.
     */
    function carticy_conversions_fs() {
        global $carticy_conversions_fs;
        if ( isset( $carticy_conversions_fs ) ) {
            return $carticy_conversions_fs;
        }
        if ( !carticy_conversions_fs_credentials_ready() ) {
            return null;
        }
        // Prefer the bundled SDK; fall back to one another plugin loaded.
        if ( !function_exists( 'fs_dynamic_init' ) && defined( 'CARTICY_CONVERSIONS_PATH' ) && file_exists( CARTICY_CONVERSIONS_PATH . 'vendor/freemius/wordpress-sdk/start.php' ) ) {
            require_once CARTICY_CONVERSIONS_PATH . 'vendor/freemius/wordpress-sdk/start.php';
        }
        if ( !function_exists( 'fs_dynamic_init' ) ) {
            return null;
        }
        $carticy_conversions_fs = fs_dynamic_init( array(
            'id'               => CARTICY_CONVERSIONS_FS_ID,
            'slug'             => 'carticy-conversions-for-woocommerce',
            'premium_slug'     => 'carticy-conversions-for-woocommerce-pro',
            'type'             => 'plugin',
            'public_key'       => CARTICY_CONVERSIONS_FS_PUBLIC_KEY,
            'is_premium'       => false,
            'premium_suffix'   => 'Pro',
            'has_addons'       => false,
            'has_paid_plans'   => true,
            'is_org_compliant' => true,
            'menu'             => array(
                'slug'       => 'carticy-conversions',
                'first-path' => 'admin.php?page=carticy-conversions&welcome-message=true',
                'support'    => false,
            ),
            'is_live'          => true,
        ) );
        return $carticy_conversions_fs;
    }

    $carticy_conversions_freemius = carticy_conversions_fs();
    if ( is_object( $carticy_conversions_freemius ) ) {
        do_action( 'carticy_conversions_fs_loaded' );
    }
}
/**
 * Check if running premium-only code.
 *
 * Runtime helper only. For Freemius preprocessing, use direct SDK calls:
 * if ( carticy_conversions_fs()->is__premium_only() ) { ... }
 *
 * @return bool
 */
function carticy_conversions_is_premium_only() : bool {
    $freemius = carticy_conversions_fs();
    return is_object( $freemius ) && method_exists( $freemius, 'is__premium_only' ) && $freemius->is__premium_only();
}

/**
 * Check whether licensed premium code can run.
 *
 * Runtime helper only. Strip boundaries must use direct
 * carticy_conversions_fs()->is__premium_only() calls.
 *
 * @return bool
 */
function carticy_conversions_can_use_premium() : bool {
    $freemius = carticy_conversions_fs();
    return is_object( $freemius ) && method_exists( $freemius, 'can_use_premium_code' ) && $freemius->can_use_premium_code();
}

/**
 * Cleanup plugin data when uninstall is confirmed through Freemius.
 *
 * Freemius owns the uninstall hook: WordPress only supports ONE uninstall
 * handler, so a plugin-level uninstall.php would take that slot and prevent
 * the SDK from reporting the uninstall (Freemius docs + the accepted Checkout
 * Shield pattern). The data-removal contract is unchanged — Uninstaller::cleanup()
 * still honors the "delete data on uninstall" option. The uninstaller is loaded
 * explicitly because the SPL autoloader may not be registered in the uninstall
 * context.
 */
function carticy_conversions_uninstall_cleanup() : void {
    if ( !class_exists( \Carticy\Conversions\Core\Uninstaller::class ) && defined( 'CARTICY_CONVERSIONS_PATH' ) && file_exists( CARTICY_CONVERSIONS_PATH . 'src/Core/Uninstaller.php' ) ) {
        require_once CARTICY_CONVERSIONS_PATH . 'src/Core/Uninstaller.php';
    }
    if ( class_exists( \Carticy\Conversions\Core\Uninstaller::class ) ) {
        \Carticy\Conversions\Core\Uninstaller::cleanup();
    }
}

$carticy_conversions_freemius_for_uninstall = carticy_conversions_fs();
if ( is_object( $carticy_conversions_freemius_for_uninstall ) && method_exists( $carticy_conversions_freemius_for_uninstall, 'add_action' ) ) {
    $carticy_conversions_freemius_for_uninstall->add_action( 'after_uninstall', 'carticy_conversions_uninstall_cleanup' );
}