<?php

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

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.6.5
 */
class NewCodeByte_Chatbot_Main {

    /**
     * The main function to run the plugin.
     *
     * @since    1.6.5
     */
    public function run() {
        $this->load_dependencies();
        $this->define_hooks();
    }

    /**
     * Load the required dependencies for this plugin.
     *
     * @since    1.6.5
     */
    private function load_dependencies() {
        require_once NCB_CHATBOT_PLUGIN_DIR . 'includes/class-chatbot-install.php';
        require_once NCB_CHATBOT_PLUGIN_DIR . 'includes/class-chatbot-admin.php';
        require_once NCB_CHATBOT_PLUGIN_DIR . 'includes/class-chatbot-frontend.php';
        require_once NCB_CHATBOT_PLUGIN_DIR . 'includes/class-chatbot-ajax.php';
        require_once NCB_CHATBOT_PLUGIN_DIR . 'includes/class-chatbot-api.php';
    }

    /**
     * Register all of the hooks related to the plugin functionality.
     *
     * @since    1.6.5
     */
    private function define_hooks() {
        add_action('plugins_loaded', [$this, 'load_textdomain']);
        add_action('init', [$this, 'start_session'], 1);

        $installer = new NewCodeByte_Chatbot_Install();
        $installer->add_hooks();

        $admin = new NewCodeByte_Chatbot_Admin();
        $admin->add_hooks();

        $frontend = new NewCodeByte_Chatbot_Frontend();
        $frontend->add_hooks();

        $ajax = new NewCodeByte_Chatbot_Ajax();
        $ajax->add_hooks();
    }

    /**
     * Load the plugin text domain for translation.
     *
     * @since    1.6.5
     */
    public function load_textdomain() {
        load_plugin_textdomain(
            NCB_CHATBOT_TEXT_DOMAIN,
            false,
            dirname(plugin_basename(NCB_CHATBOT_PLUGIN_FILE)) . '/languages/'
        );
    }

    /**
     * Start PHP session if not already started.
     *
     * @since    1.6.5
     */
    public function start_session() {
        if (is_admin() || wp_doing_ajax() || wp_doing_cron() || (defined('REST_REQUEST') && REST_REQUEST) || (defined('WP_CLI') && WP_CLI)) {
            return;
        }
        if (!session_id()) {
            session_start();
        }
    }
}