<?php
/**
 * Freemius SDK bootstrap.
 *
 * Deliberately in the GLOBAL namespace (no `namespace` declaration) so the
 * helper is available as `\bata_fs()` from every namespaced class in the plugin.
 *
 * @package BulkAltText
 */

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

if ( ! function_exists( 'bata_fs' ) ) {
    /**
     * Create a helper function for easy SDK access.
     *
     * @return \Freemius
     */
    function bata_fs() {
        global $bata_fs;

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

            $bata_fs = fs_dynamic_init( array(
                'id'                  => '34620',
                'slug'                => 'bulk-alt-text-ai',
                'type'                => 'plugin',
                'public_key'          => 'pk_fea89fcf7ac380b61b155844f752f',
                'is_premium'          => true,
                'premium_suffix'      => 'Monthly',
                // 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,
                // Automatically removed in the free version. If you're not using the
                // auto-generated free version, delete this line before uploading to wp.org.
                'wp_org_gatekeeper'   => 'OA7#BoRiBNqdf52FvzEf!!074aRLPs8fspif$7K1#4u4Csys1fQlCecVcUTOs2mcpeVHi#C2j9d09fOTvbC0HloPT7fFee5WdS3G',
                'trial'               => array(
                    'days'               => 3,
                    'is_require_payment' => true,
                ),
                'menu'                => array(
                    // Our own admin screen lives under the Media menu; point
                    // Freemius at that same slug so its Account/Upgrade pages
                    // attach to it instead of creating a second top-level menu.
                    'slug'       => 'bulk-alt-text-ai',
                    'first-path' => 'upload.php?page=bulk-alt-text-ai',
                    'parent'     => array(
                        'slug' => 'upload.php',
                    ),
                    'support'    => false,
                ),
            ) );
        }

        return $bata_fs;
    }

    // Init Freemius.
    bata_fs();
    // Signal that SDK was initiated.
    do_action( 'bata_fs_loaded' );

    /**
     * Clean up plugin data on uninstall.
     *
     * Freemius requires this to be registered via its `after_uninstall` hook
     * rather than a standalone uninstall.php file. Alt text itself is left in
     * place — it is real content the site now depends on.
     */
    function bata_fs_uninstall_cleanup() {
        delete_option( 'batai_settings' );
        delete_option( 'batai_usage' );

        global $wpdb;
        $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
            "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ('_batai_generated', '_batai_prev_alt')"
        );
    }

    bata_fs()->add_action( 'after_uninstall', 'bata_fs_uninstall_cleanup' );
}
