<?php
/**
 * Plugin Name: Pewit GEO Analysis Tool
 * Plugin URI: https://pewitlabs.com/product-ai-visibility-toolkit.html
 * Description: Generative Engine Optimization (GEO/AEO) for WordPress — GEO Score, entity & EEAT analysis, FAQ/Q&A generation, schema, AI citability, competitor analysis and AI answer-engine prompt testing.
 * Version: 1.0.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-geo-analysis-tool
 * 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 that
 * features are gated by the real Freemius plan. Do not edit by hand.
 */
if (!defined('AIVT_PRO')) {
    define('AIVT_PRO', false); /* AIVT_PRO_BUILD_FLAG */
}

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

if ( ! function_exists( 'aivt_freemius' ) ) {
    function aivt_freemius() {
        global $aivt_freemius;
        if ( ! isset( $aivt_freemius ) ) {
            $sdk = dirname( __FILE__ ) . '/vendor/freemius/start.php';
            if ( ! file_exists( $sdk ) ) {
                return null;
            }
            /*
             * Freemius go-live: paste your product id and public_key from the Freemius
             * dashboard (Developer → your product → SDK Integration) into the two values
             * below. Until a real (numeric) id is set, the SDK is intentionally NOT
             * initialized so the free build runs fully without any license layer and
             * cannot fatal on activation with placeholder credentials.
             */
            $aivt_fs_id  = 'REPLACE_WITH_PLUGIN_ID';
            $aivt_fs_key = 'REPLACE_WITH_PUBLIC_KEY';
            if ( ! ctype_digit( (string) $aivt_fs_id ) ) {
                return null;
            }
            require_once $sdk;
            $aivt_freemius = fs_dynamic_init( array(
                'id'                  => $aivt_fs_id,
                'slug'                => 'pewit-geo-analysis-tool',
                'premium_slug'        => 'pewit-geo-analysis-tool-premium',
                'type'                => 'plugin',
                'public_key'          => $aivt_fs_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,
                'menu'                => array(
                    'slug'       => 'pewit-geo-analysis-tool',
                    'first-path' => 'admin.php?page=pewit-geo-analysis-tool',
                    'account'    => false,
                    'contact'    => false,
                    'support'    => false,
                    'parent'     => array(
                        'slug' => 'pewit-geo-analysis-tool',
                    ),
                ),
            ) );
        }
        return $aivt_freemius;
    }
    if ( file_exists( dirname( __FILE__ ) . '/vendor/freemius/start.php' ) ) {
        aivt_freemius();
        do_action( 'aivt_freemius_loaded' );
    }
}

define('AIVT_VERSION', '1.0.5');
define('AIVT_FILE', __FILE__);
define('AIVT_DIR', plugin_dir_path(__FILE__));
define('AIVT_URL', plugin_dir_url(__FILE__));
define('AIVT_OPTION', 'aivt_settings');
define('AIVT_JOB_OPTION', 'aivt_job');

require_once AIVT_DIR . 'includes/helpers.php';
require_once AIVT_DIR . 'includes/class-pewit-labs-hub.php';
require_once AIVT_DIR . 'includes/class-license.php';
require_once AIVT_DIR . 'includes/class-i18n.php';
require_once AIVT_DIR . 'includes/class-aivt-llm-api.php';
require_once AIVT_DIR . 'includes/class-seo-bridge.php';
require_once AIVT_DIR . 'includes/class-api.php';
require_once AIVT_DIR . 'includes/class-store.php';
require_once AIVT_DIR . 'includes/class-geo-score.php';
require_once AIVT_DIR . 'includes/class-entities.php';
require_once AIVT_DIR . 'includes/class-eeat.php';
require_once AIVT_DIR . 'includes/class-faq.php';
require_once AIVT_DIR . 'includes/class-qa.php';
require_once AIVT_DIR . 'includes/class-schema.php';
require_once AIVT_DIR . 'includes/class-citability.php';
require_once AIVT_DIR . 'includes/class-prompt-test.php';
require_once AIVT_DIR . 'includes/class-competitors.php';
require_once AIVT_DIR . 'includes/class-queue.php';
require_once AIVT_DIR . 'includes/class-lifecycle.php';
require_once AIVT_DIR . 'includes/class-render.php';
require_once AIVT_DIR . 'includes/class-audit.php';
require_once AIVT_DIR . 'includes/class-autopilot.php';
require_once AIVT_DIR . 'includes/class-cli.php';
require_once AIVT_DIR . 'admin/class-admin.php';
require_once AIVT_DIR . 'admin/class-post-editor.php';

function aivt_freemius_uninstall_cleanup() {
    AIVT_Lifecycle::on_uninstall();
}
if ( function_exists( 'aivt_freemius' ) && aivt_freemius() ) {
    aivt_freemius()->add_action( 'after_uninstall', 'aivt_freemius_uninstall_cleanup' );
}

register_activation_hook(__FILE__, function ($network_wide = false) {
    $defaults = aivt_default_settings();
    $current = get_option(AIVT_OPTION, []);
    update_option(AIVT_OPTION, wp_parse_args($current, $defaults));
    if (!wp_next_scheduled('aivt_process_queue')) {
        wp_schedule_event(time() + 60, 'aivt_every_minute', 'aivt_process_queue');
    }
    // Trigger the welcome wizard on first single-site activation only.
    if (!$network_wide && empty($current['onboarding_done'])) {
        add_option('aivt_do_redirect', 1);
    }
});

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

add_filter('cron_schedules', function ($schedules) {
    if (!isset($schedules['aivt_every_minute'])) {
        $schedules['aivt_every_minute'] = ['interval' => 60, 'display' => 'Every minute (AIVT)'];
    }
    if (!isset($schedules['aivt_monthly'])) {
        $schedules['aivt_monthly'] = ['interval' => 30 * DAY_IN_SECONDS, 'display' => 'Monthly (AIVT)'];
    }
    return $schedules;
});

add_action('plugins_loaded', function () {
    Pewit_Labs_Hub::register('pewit-geo-analysis-tool', [
        'label'         => 'Pewit GEO Analysis Tool',
        'text_domain'   => 'pewit-geo-analysis-tool',
        'priority'      => 60,
        'llm_class'     => 'AIVT_LLM_API',
        'seed_callback' => 'aivt_get_settings',
    ]);
    Pewit_Labs_Hub::boot();

    AIVT_I18n::load();
    AIVT_Lifecycle::init();
    AIVT_Schema::init();
    AIVT_Queue::init();
    AIVT_Prompt_Test::init();
    AIVT_Admin::init();
    AIVT_Post_Editor::init();
    AIVT_CLI::register();
});
