<?php
/**
 * Plugin Name: LucidPen – AI Writing Assistant for Journaling, Prayer, and Reflection
 * Plugin URI: https://lucidpen.enigtech.com
 * Description: AI writing, blog assistant, content generation, journaling plugin, OpenAI, GPT, prayer journal, gratitude journal, reflective writing, spiritual journaling, writing prompts
 * Version: 1.1.2
 * Author: EnigTech
 * Author URI: https://www.enigtech.com
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: lucidpen
 * Domain Path: /language
 */

namespace lucidpen;

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

use core\PluginEventListener;
use core\SingletonTrait;
use core\Session;

// Define plugin directory
define('LCPN_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('LCPN_PLUGIN_URL', plugin_dir_url(__FILE__));

// Include required files
require_once LCPN_PLUGIN_DIR . 'include/constants.php';
require_once LCPN_PLUGIN_DIR . 'vendor/core/wordpress/cls-util.php';
require_once LCPN_PLUGIN_DIR . 'vendor/core/wordpress/cls-session.php';
require_once LCPN_PLUGIN_DIR . 'vendor/core/wordpress/opt-abstract.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-common.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-license.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-azure-openai.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-factory.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-post-manager.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-composers.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-assistant.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-bsf.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-prayer.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-reporter.php';
require_once LCPN_PLUGIN_DIR . 'include/cls-addons-loader.php';
require_once LCPN_PLUGIN_DIR . 'include/opt-openai.php';
require_once LCPN_PLUGIN_DIR . 'include/fep-base.php';
require_once LCPN_PLUGIN_DIR . 'include/fep-generic.php';



// ******************************************************************************
// *
// *  OpenAiChatSession
// *
// ******************************************************************************
class OpenAiChatSession extends Session
{

    public const ABBR = LCPN_ABBR;
    public const APP  = LCPN_APP;

    use SingletonTrait;

    private AssistantFactory $assistantFactory;
    private ComposerManager $composerManager;
    private LicenseManager $licenseManager;
    private AIServiceManager $aiServiceManager;

    private function __construct()
    {
        new AddonLoader();
        $this->aiServiceManager = AIServiceManager::get_instance();
        $this->assistantFactory = AssistantFactory::get_instance();
        $this->composerManager = new ComposerManager($this);
        $this->licenseManager = LicenseManager::get_instance();
    }

    public function getAssistantFactory(): AssistantFactory
    {
        return $this->assistantFactory;
    }

    public function getComposerManager(): ComposerManager
    {
        return $this->composerManager;
    }
    public function getLicenseManager(): \LicenseManager
    {
        return $this->licenseManager;
    }

}


// ******************************************************************************
// *
// *  class OpenAiChatSystem implements PluginEventListener {
// *
// ******************************************************************************
class OpenAiChatSystem implements PluginEventListener
{

    use SingletonTrait;

    private function __construct()
    {
    }

    public function on_plugin_load(): void
    {
        // Session
        $session = OpenAiChatSession::get_instance();

        // Initialize the Ticket Inventory Manager
        // new TopLevelAdmin();

        // Options
        new OpenAIOption();

        // Setup front-end pages for each assistant
        $session->getComposerManager()->setupFrontEndPages();
    }

    public function on_plugin_activate(): void
    {
        // Session
        $session = OpenAiChatSession::get_instance();

        // ComposerManager
        $composerManager = $session->getComposerManager();
        $composerManager->setupFrontEndPages();
        $composerManager->ensureSlugRegistered(true);
    }

    public function on_plugin_deactivate(): void
    {
    }
}


// ******************************************************************************
// *
// *  Event Listener for the Spirit Fire System
// *
// ******************************************************************************

// Initialize the plugin
add_action('plugins_loaded', function () {
    $fire_system = OpenAiChatSystem::get_instance();
    $fire_system->on_plugin_load();
});

// Register the activation hook
register_activation_hook(__FILE__, function () {
    $fire_system = OpenAiChatSystem::get_instance();
    $fire_system->on_plugin_activate();
    flush_rewrite_rules();

    PostManager::migrate_all_existing_meta();
});


register_deactivation_hook(__FILE__, function () {
    $fire_system = OpenAiChatSystem::get_instance();
    $fire_system->on_plugin_deactivate();
    flush_rewrite_rules();
});

?>