<?php
/**
 * FluentC Bootstrap file
 * 
 * Initializes the plugin properly
 */

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

use FluentC\Bootstrap_FluentC;

/**
 * Only use for get one context
 *
 * @since 1.2
 */
abstract class FluentC_Context {

    /**
     * Get Context Variable
     *
     * @static
     * @since 1.2
     * @var Bootstrap_FluentC|null
     */
    protected static $context;

    /**
     * Create context if not exist
     *
     * @static
     * @return object
     * @since 1.2
     */
    public static function fluentc_get_context() {
        if (null !== self::$context) {
            return self::$context;
        }

        self::$context = new Bootstrap_FluentC();

        // Set up essential services that are always loaded
        $essential_services = array(
            '\FluentC\Services\Cache',
            '\FluentC\Services\Connect',
            '\FluentC\Utils\Language',
            '\FluentC\Models\Htmltags',
            '\FluentC\Services\Html_Processor',
            '\FluentC\Services\FluentC_Free',
            
        );
        
        self::$context->set_essential_services($essential_services);
        
        // Check if API key exists
        $has_api_key = !empty(get_option('fluentc_api_key'));
        
        // Only load additional services if API key exists
        if ($has_api_key) {
            $additional_services = array(
                '\FluentC\Services\Url',
                '\FluentC\Services\Widget',
                '\FluentC\Services\Scan',
                '\FluentC\Services\Translation_Processor',
                '\FluentC\Services\Translation_Manager',
                '\FluentC\Services\FluentC_Translations',
                '\FluentC\Compat\WP_Rocket_Compatibility::class'
            );
            
            self::$context->set_additional_services($additional_services);
        }

        // Set up actions - these will be loaded but only hooked if API key exists
        $actions = array(
            'FluentC\Actions\Insert',
            'FluentC\Actions\Links',
            'FluentC\Actions\Aioseo',
            'FluentC\Actions\Rankmath',
            'FluentC\Actions\Yoast',
            'FluentC\Actions\Wordpress',
            'FluentC\Actions\Admin',
            'FluentC\Actions\Translationstatus',
            'FluentC\Actions\Heartbeat',
            'FluentC\Actions\Permalinks',
        );

        self::$context->set_actions($actions);
        return self::$context;
    }
}

/**
 * Init plugin
 *
 * @return void
 * @since 1.2
 */
function fluentc_init() {
    try {
        $context = FluentC_Context::fluentc_get_context();
        $context->init_plugin();
    } catch (Exception $e) {
        error_log('FluentC: Error initializing plugin: ' . $e->getMessage());
    }
}