<?php

namespace FluentC;

use FluentC\Models\Hooks;

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

/**
 * Plugin Bootstrap
 *
 * @since 1.2
 */
class Bootstrap_FluentC {

    /**
     * List actions WordPress
     *
     * @since 1.2
     * @var array
     */
    protected $actions = array();

    /**
     * List class services
     *
     * @since 1.2
     * @var array
     */
    protected $services = array();

    /**
     * Set actions
     *
     * @since 1.2
     * @param array $actions actions.
     * @return Bootstrap_FluentC
     */
    public function set_actions($actions) {
        $this->actions = $actions;
        return $this;
    }

    /**
     * Set services
     *
     * @since 1.2
     * @param array $services services.
     * @return Bootstrap_FluentC
     */
    public function set_services($services) {
        foreach ($services as $service) {
            $this->set_service($service);
        }
        return $this;
    }

    /**
     * Set a service
     *
     * @since 1.2
     * @param string $service services.
     * @return Bootstrap_FluentC
     */
    public function set_service($service) {
        $name = explode('\\', $service);
        end($name);
        $key = key($name);
        $this->services[$name[$key]] = $service;
        return $this;
    }

    /**
     * Set essential services that are always loaded
     *
     * @param array $services List of service class names
     * @return Bootstrap_FluentC
     */
    public function set_essential_services($services) {
        foreach ($services as $service) {
            $this->set_service($service);
        }
        return $this;
    }

    /**
     * Set additional services that are loaded conditionally
     *
     * @param array $services List of service class names
     * @return Bootstrap_FluentC
     */
    public function set_additional_services($services) {
        foreach ($services as $service) {
            $this->set_service($service);
        }
        return $this;
    }

    /**
     * Check if API key is set
     *
     * @return bool
     */
    public function has_api_key() {
        $api_key = get_option('fluentc_api_key');
        return !empty($api_key);
    }

    /**
     * Get one service by classname
     *
     * @param string $name name.
     * @return object
     * @throws \Exception if service doesn't exist
     * @since 1.2
     */
    public function get_service($name) {
        if (!array_key_exists($name, $this->services)) {
            throw new \Exception('Service : ' . esc_html($name) . ' not exist');
        }

        if (is_string($this->services[$name])) {
            try {
                $this->services[$name] = new $this->services[$name]();
            } catch (\Exception $e) {
                error_log('FluentC: Error initializing service ' . $name . ': ' . $e->getMessage());
                throw $e;
            }
        }

        return $this->services[$name];
    }

    
    /**
     * Initialize plugin
     *
     * @since 1.2
     * @return void
     */
    public function init_plugin() {
        // Initialize all actions
        // First check if API Key is valid
        $has_valid_key = $this->has_api_key();

        // Initialize all actions - essential ones first based on key validation
    foreach ($this->actions as $action) {
        try {
            // Skip some actions if API key is not valid
            if (!$has_valid_key) {
                // Determine if this is an essential action we should always load
                $always_load = in_array($action, [
                    'FluentC\Actions\Admin',       // Admin class for settings
                    'FluentC\Actions\Links',       // Basic link handling
                    'FluentC\Actions\Heartbeat'    // Heartbeat for plugin activation tracking
                ]);
                
                if (!$always_load) {
                    continue; // Skip non-essential actions when key is invalid
                }
            }
            
            $action_instance = new $action();
            if ($action_instance instanceof Hooks) {
                $action_instance->hooks();
            }
        } catch (\Exception $e) {
            error_log('FluentC: Error initializing action ' . $action . ': ' . $e->getMessage());
            continue;
        }
    }

    // Initialize the appropriate manager, but only if key is valid
    if ($has_valid_key) {
        if (!function_exists('pll_get_post')) {
            $this->init_fluentc_polylang_manager();
        } else {
            $this->init_fluentc_manager();
        }
    }
    }

    /**
     * Initialize FluentC_Manager
     *
     * @since 1.2
     * @return void
     */
    protected function init_fluentc_manager() {
        global $fluentc;
        try {
            $fluentc = new FluentC_Manager();
        } catch (\Exception $e) {
            error_log('FluentC: Error initializing FluentC Manager: ' . $e->getMessage());
        }
    }

    /**
     * Initialize FluentC_Polylang_Manager
     *
     * @since 1.2
     * @return void
     */
    protected function init_fluentc_polylang_manager() {
        global $polylang;
        
        try {
            $polylang = new Polylang();
            
            if (!function_exists('pll_languages_list')) {
                // Loads the API.
                include_once FLUENTC_DIR . '/src/fluentc_pll_api.php';
            }
        } catch (\Exception $e) {
            error_log('FluentC: Error initializing Polylang Manager: ' . $e->getMessage());
        }
    }

    /**
     * Activate plugin
     *
     * @since 1.2
     * @return void
     */
    public function activate_plugin() {
        foreach ($this->actions as $action) {
            try {
                $action_instance = new $action();
                if (method_exists($action_instance, 'fluentc_activate')) {
                    $action_instance->fluentc_activate();
                }
            } catch (\Exception $e) {
                error_log('FluentC: Error during activation for ' . $action . ': ' . $e->getMessage());
            }
        }
    }

    /**
     * Deactivate plugin
     *
     * @since 1.2
     * @return void
     */
    public function deactivate_plugin() {
        foreach ($this->actions as $action) {
            try {
                $action_instance = new $action();
                if (method_exists($action_instance, 'fluentc_deactivate')) {
                    $action_instance->fluentc_deactivate();
                }
            } catch (\Exception $e) {
                error_log('FluentC: Error during deactivation for ' . $action . ': ' . $e->getMessage());
            }
        }
    }
}