<?php
/**
 * Plugin Name: Pewit AI Image Optimizer
 * Plugin URI: https://pewitlabs.com/product-ai-image-optimizer.html
 * Description: Smart JPEG/PNG compression with optional WebP/AVIF sidecars, video AV1 WebM (ffmpeg), and backup-first bulk jobs.
 * Version: 1.1.3
 * 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-image-optimizer
 * 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('AIIO_PRO')) {
    define('AIIO_PRO', false); /* AIIO_PRO_BUILD_FLAG */
}

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

if (!function_exists('aiio_freemius')) {
    /**
     * Freemius SDK — placeholder IDs until go-live.
     * Replace REPLACE_WITH_PLUGIN_ID and REPLACE_WITH_PUBLIC_KEY (see docs/FREEMIUS_SETUP.md).
     */
    function aiio_freemius() {
        global $aiio_freemius;
        if (!isset($aiio_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
            $aiio_freemius = fs_dynamic_init([
                'id'                  => 'REPLACE_WITH_PLUGIN_ID',
                'slug'                => 'pewit-ai-image-optimizer',
                'premium_slug'        => 'pewit-ai-image-optimizer-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,
                'menu'                => [
                    'slug'       => 'pewit-ai-image-optimizer',
                    'first-path' => 'admin.php?page=pewit-ai-image-optimizer',
                    'account'    => false,
                    'contact'    => false,
                    'support'    => false,
                    'parent'     => [
                        'slug' => 'pewit-ai-image-optimizer',
                    ],
                ],
            ]);
        }
        return $aiio_freemius;
    }
    if (file_exists(dirname(__FILE__) . '/vendor/freemius/start.php')) {
        aiio_freemius();
        do_action('aiio_freemius_loaded');
    }
}

define('AIIO_VERSION', '1.1.3');
define('AIIO_FILE', __FILE__);
define('AIIO_DIR', plugin_dir_path(__FILE__));
define('AIIO_URL', plugin_dir_url(__FILE__));
define('AIIO_OPTION', 'aiio_settings');
define('AIIO_JOB_OPTION', 'aiio_active_job');

require_once AIIO_DIR . 'includes/helpers.php';
require_once AIIO_DIR . 'includes/class-license.php';
require_once AIIO_DIR . 'includes/class-i18n.php';
require_once AIIO_DIR . 'includes/class-optimizer.php';
require_once AIIO_DIR . 'includes/class-video-optimizer.php';
require_once AIIO_DIR . 'includes/class-media-apply.php';
require_once AIIO_DIR . 'includes/class-etch.php';
require_once AIIO_DIR . 'includes/class-lifecycle.php';
require_once AIIO_DIR . 'includes/class-backup.php';
require_once AIIO_DIR . 'includes/class-scanner.php';
require_once AIIO_DIR . 'includes/class-job-notify.php';
require_once AIIO_DIR . 'includes/class-onboarding.php';
require_once AIIO_DIR . 'includes/class-usage.php';
require_once AIIO_DIR . 'includes/class-lcp-scanner.php';
require_once AIIO_DIR . 'includes/class-queue.php';
require_once AIIO_DIR . 'admin/class-admin.php';

function aiio_freemius_uninstall_cleanup() {
    AIIO_Lifecycle::on_uninstall();
}

if (function_exists('aiio_freemius') && aiio_freemius()) {
    aiio_freemius()->add_action('after_uninstall', 'aiio_freemius_uninstall_cleanup');
}

register_activation_hook(__FILE__, function () {
    $defaults = aiio_default_settings();
    $current = get_option(AIIO_OPTION, []);
    update_option(AIIO_OPTION, wp_parse_args($current, $defaults));
});

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

add_action('plugins_loaded', function () {
    AIIO_I18n::load();
    AIIO_Etch::init();
    AIIO_Lifecycle::init();
    AIIO_Onboarding::init();
    AIIO_Optimizer::init();
    AIIO_Video_Optimizer::init();
    AIIO_Queue::init();
    AIIO_Admin::init();
});
