<?php
/**
 * Plugin Name: Pewit AI Schema Builder
 * Plugin URI: https://pewitlabs.com/product-ai-schema-builder.html
 * Description: Generate and maintain structured data and rich snippets from your posts, products and pages.
 * Version: 1.1.5
 * Author: PEWIT Labs
 * Author URI: https://pewitlabs.com
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: pewit-ai-schema-builder
 * Domain Path: /languages
 * Requires at least: 6.0
 * Requires PHP: 7.4
 */

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

/*
 * Build flag. false = free wordpress.org build (every feature unlocked).
 * The Freemius "pro" build sets this to true (see build.sh pro) so features
 * are gated by the real Freemius plan. Do not edit by hand.
 */
if (!defined('AISB_PRO')) {
    define('AISB_PRO', false); /* AISB_PRO_BUILD_FLAG */
}

if ( function_exists( 'aisb_freemius' ) ) {
    aisb_freemius()->set_basename( true, __FILE__ );
    return;
}

if ( ! function_exists( 'aisb_freemius' ) ) {
    function aisb_freemius() {
        global $aisb_freemius;
        if ( ! isset( $aisb_freemius ) ) {
            $sdk = dirname( __FILE__ ) . '/vendor/freemius/start.php';
            if ( ! file_exists( $sdk ) ) {
                return null;
            }
            require_once $sdk;
            if ( ! ctype_digit( (string) 'REPLACE_WITH_PLUGIN_ID' ) ) { return null; } // Free build safeguard: no SDK init with placeholder credentials
            /*
             * Freemius go-live: paste your product id and public_key from the Freemius
             * dashboard (Developer → your product → SDK Integration) into the placeholders
             * below. Only set is_live => true after both values are filled in.
             */
            $aisb_freemius = fs_dynamic_init( array(
                'id'                  => 'REPLACE_WITH_PLUGIN_ID',
                'slug'                => 'pewit-ai-schema-builder',
                'premium_slug'        => 'pewit-ai-schema-builder-premium',
                'type'                => 'plugin',
                'public_key'          => 'REPLACE_WITH_PUBLIC_KEY',
                'is_premium'          => false,
                'premium_suffix'      => 'Pro',
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'is_live'             => false,
                'trial'               => array(
                    'days'               => 14,
                    'is_require_payment' => false,
                ),
                'menu'                => array(
                    'slug'           => 'pewit-ai-schema-builder',
                    'first-path'     => 'admin.php?page=pewit-ai-schema-builder',
                    'account'        => false,
                    'contact'        => false,
                    'support'        => false,
                    'parent'         => array(
                        'slug' => 'pewit-ai-schema-builder',
                    ),
                ),
            ) );
        }
        return $aisb_freemius;
    }
    if ( file_exists( dirname( __FILE__ ) . '/vendor/freemius/start.php' ) ) {
        aisb_freemius();
        do_action( 'aisb_freemius_loaded' );
    }
}

define('AISB_VERSION', '1.1.5');
define('AISB_FILE', __FILE__);
define('AISB_DIR', plugin_dir_path(__FILE__));
define('AISB_URL', plugin_dir_url(__FILE__));
define('AISB_OPTION', 'aisb_settings');
define('AISB_JOB_OPTION', 'aisb_job');

require_once AISB_DIR . 'includes/helpers.php';
require_once AISB_DIR . 'includes/class-pewit-labs-hub.php';
require_once AISB_DIR . 'includes/class-license.php';
require_once AISB_DIR . 'includes/class-i18n.php';
require_once AISB_DIR . 'includes/class-aisb-llm-api.php';
require_once AISB_DIR . 'includes/class-api.php';
require_once AISB_DIR . 'includes/class-etch.php';
require_once AISB_DIR . 'includes/class-seo-plugins.php';
require_once AISB_DIR . 'includes/class-rich-results.php';
require_once AISB_DIR . 'includes/class-cpt-registry.php';
require_once AISB_DIR . 'includes/class-jetengine-cct.php';
require_once AISB_DIR . 'includes/class-metabox-fields.php';
require_once AISB_DIR . 'includes/class-field-mapper.php';
require_once AISB_DIR . 'includes/class-job-notify.php';
require_once AISB_DIR . 'includes/class-onboarding.php';
require_once AISB_DIR . 'includes/class-seo-sync.php';
require_once AISB_DIR . 'includes/class-validation-cron.php';
require_once AISB_DIR . 'includes/class-integrations.php';
require_once AISB_DIR . 'includes/class-rich-results-score.php';
require_once AISB_DIR . 'includes/class-score-booster.php';
require_once AISB_DIR . 'includes/class-lifecycle.php';
require_once AISB_DIR . 'includes/class-schema.php';
require_once AISB_DIR . 'includes/class-queue.php';
require_once AISB_DIR . 'includes/class-admin-columns.php';
require_once AISB_DIR . 'admin/class-admin.php';
require_once AISB_DIR . 'admin/class-post-schema.php';

function aisb_freemius_uninstall_cleanup() {
    AISB_Lifecycle::on_uninstall();
}
if ( function_exists( 'aisb_freemius' ) && aisb_freemius() ) {
    aisb_freemius()->add_action( 'after_uninstall', 'aisb_freemius_uninstall_cleanup' );
}

register_activation_hook(__FILE__, function() {
    $defaults = aisb_default_settings();
    $current = get_option(AISB_OPTION, []);
    update_option(AISB_OPTION, wp_parse_args($current, $defaults));
    AISB_CPT_Registry::seed_defaults();
});

register_deactivation_hook(__FILE__, ['AISB_Lifecycle', 'on_deactivate']);

add_action('plugins_loaded', function() {
    Pewit_Labs_Hub::register('pewit-ai-schema-builder', [
        'label'         => 'Pewit AI Schema Builder',
        'text_domain'   => 'pewit-ai-schema-builder',
        'priority'      => 20,
        'llm_class'     => 'AISB_LLM_API',
        'seed_callback' => 'aisb_get_settings',
    ]);
    Pewit_Labs_Hub::boot();

    AISB_I18n::load();
    AISB_Etch::init();
    AISB_Lifecycle::init();
    AISB_Onboarding::init();
    AISB_SEO_Sync::init();
    AISB_Validation_Cron::init();
    AISB_CPT_Registry::seed_defaults();
    AISB_Validation_Cron::maybe_schedule();
    AISB_Schema::init();
    AISB_Queue::init();
    AISB_Admin::init();
    AISB_Admin_Columns::init();
    AISB_Post_Schema::init();
});
