<?php
/**
 * Freemius SDK bootstrap.
 *
 * This file is loaded from brainwerk-seo-suite.php (BEFORE other includes) so that the
 * Freemius global is available everywhere. The product/public_key values below
 * are the real, live Freemius credentials for Brainwerk SEO Suite (see
 * FREEMIUS_SETUP.md) — not placeholders. Do not paste them into a public
 * issue/PR, and treat this file as sensitive if the repo is ever made public.
 * The SDK itself still needs to be dropped into /freemius/; the plugin works
 * fine even before that — the License adapter falls back to "free tier" until
 * the SDK is present.
 *
 * @package Brainwerk SEO Suite
 */

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

if ( ! function_exists( 'seoforge_fs' ) ) {
    /**
     * @return \Freemius|null
     */
    function seoforge_fs() {
        global $seoforge_fs;
        if ( ! isset( $seoforge_fs ) ) {
            $sdk_path = SEOFORGE_PATH . 'freemius/start.php';
            if ( ! file_exists( $sdk_path ) ) {
                $seoforge_fs = null;
                return null;
            }
            require_once $sdk_path;
            try {
                $seoforge_fs = fs_dynamic_init( array(
                    // ── Real, live Freemius product credentials (not placeholders) ──
                    'id'                  => '29959',
                    'slug'                => 'brainwerk-seo-suite',
                    'premium_slug'        => 'seoforge-premium',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_3bf2a32f48ec3420eca1f2f63044e',
                    'is_premium'          => false,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'menu'                => array(
                        'slug'    => 'brainwerk-seo-suite',
                        'account' => true,
                        'contact' => true,
                        'support' => true,
                    ),
                    'is_live'             => true,
                ) );
            } catch ( \Throwable $e ) {
                $seoforge_fs = null;
            }
        }
        return $seoforge_fs;
    }
}

// Trigger Freemius hooks early.
seoforge_fs();
do_action( 'seoforge_fs_loaded' );

/**
 * Uninstall hook — fires when a customer removes the plugin through the
 * regular WP-Admin Plugins page. Replaces the legacy uninstall.php so the
 * cleanup runs reliably through Freemius's update/uninstall flow too
 * (per Freemius Deploy requirement).
 *
 * Only deletes data when the user has explicitly opted in via the
 * "delete on uninstall" setting — otherwise this is a no-op.
 */
if ( function_exists( 'seoforge_fs' ) && seoforge_fs() ) {
    seoforge_fs()->add_action( 'after_uninstall', 'seoforge_after_uninstall' );
}

if ( ! function_exists( 'seoforge_after_uninstall' ) ) {
    function seoforge_after_uninstall() {
        global $wpdb;

        $options_key = 'seoforge_settings';
        $network     = is_multisite() ? get_site_option( $options_key, array() ) : array();
        $site        = get_option( $options_key, array() );
        $settings    = array_replace(
            is_array( $network ) ? $network : array(),
            is_array( $site ) ? $site : array()
        );

        // Kept semantically identical to uninstall.php: an undecided choice
        // (onboarding never finished → key never stored) purges by default for
        // data minimisation; a stored choice (including false) is honoured.
        // NB: uninstall.php is authoritative in a standard WP deletion (WP runs
        // it and returns before reaching the uninstall_plugins callbacks Freemius
        // registers), so this hook only matters in the premium/Freemius build —
        // it stays aligned so the outcome is the same whichever path fires.
        // This is deliberately NOT feature parity with uninstall.php (no settings
        // backup, no transient/geo-file cleanup, no per-site multisite loop) —
        // see "2. Zwei parallele Uninstall-Implementierungen" in
        // docs/1.5-open-decisions.md for the empirical-verification plan before
        // any consolidation/refactor is attempted.
        if ( array_key_exists( 'delete_on_uninstall', $settings )
                && empty( $settings['delete_on_uninstall'] ) ) {
            return;
        }

        $tables = array(
            $wpdb->base_prefix . 'seoforge_visits',
            $wpdb->base_prefix . 'seoforge_sessions',
            $wpdb->base_prefix . 'seoforge_redirects',
            $wpdb->base_prefix . 'seoforge_404',
            $wpdb->base_prefix . 'seoforge_meta',
            $wpdb->base_prefix . 'seoforge_daily',
            $wpdb->base_prefix . 'seoforge_audit',
        );
        foreach ( $tables as $t ) {
            $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $t ) );
        }

        if ( is_multisite() ) {
            delete_site_option( $options_key );
            delete_site_option( 'seoforge_db_version' );
            delete_site_option( 'seoforge_onboarding_pending' );
        } else {
            delete_option( $options_key );
            delete_option( 'seoforge_db_version' );
            delete_option( 'seoforge_onboarding_pending' );
        }

        // Canonical list — see Seoforge_Cron_Hooks for what fires each hook
        // (Free + Pro, whether or not the Pro modules are present here).
        foreach ( Seoforge_Cron_Hooks::all() as $hook ) {
            wp_clear_scheduled_hook( $hook );
        }
    }
}
