<?php
/**
 * Plugin Name: Radle
 * Description: Radle brings the powers of the Reddit API into WordPress.
 * Version: 2.0.4
 * Author: GBTI
 * Author URI:  https://gbti.network/?ref=atwellpub&utm_source=radle-lite&utm_medium=wordpress-plugin&utm_campaign=author-uri
 * Contributors: GBTI,Hudson Atwell
 * Text Domain: radle-lite
 * Domain Path: /languages
 * License: GPL-3.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Requires at least: 5.9.0
 * Requires PHP: 7.4
 *
 */

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

/**
 * Main plugin class that initializes all Radle functionality.
 * 
 * This class is responsible for:
 * - Loading required files and dependencies
 * - Initializing all plugin modules
 * - Setting up WordPress hooks and filters
 * - Managing plugin settings and configuration
 * - Handling internationalization
 * - Setting up REST API endpoints
 */
class Radle_Plugin {

    /**
     * Debug mode flag
     * @var bool
     */
    private $debug_mode;

    /**
     * Logging utility instance
     * @var object
     */
    public $logs;

    /**
     * Initialize the plugin.
     * 
     * Sets up all necessary hooks, loads dependencies,
     * and initializes core functionality.
     */
    public function __construct() {
        $this->set_constants();
        $this->set_variables();
        $this->load_textdomain();
        $this->includes();
        $this->initialize_modules();
        add_action( 'rest_api_init', [ $this, 'register_rest_endpoints' ] );
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
        add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), [ $this, 'add_plugin_action_links' ] );
    }

    /**
     * Define plugin constants.
     * 
     * Sets up constants for:
     * - Plugin file paths
     * - Version information
     * - GitHub repository
     * - Directory paths
     * 
     * @access private
     */
    private function set_constants() {
        define( 'RADLE_PLUGIN_FILE', __FILE__ );
        define( 'RADLE_VERSION', '2.0.4' );
        define( 'RADLE_GITHUB_REPO', 'gbti-network/radle-wordpress-plugin' );
        define( 'RADLE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
        define( 'RADLE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
        define( 'RADLE_LANGUAGES_DIR', RADLE_PLUGIN_DIR . 'languages' );
    }

    /**
     * Set plugin variables based on environment.
     *
     * Configures:
     * - Debug mode
     * - Logging settings
     *
     * @access private
     */
    private function set_variables() {
        $this->debug_mode = false;

        if ( defined( 'WP_ENVIRONMENT_TYPE' ) && WP_ENVIRONMENT_TYPE == 'local' ) {
            $this->debug_mode = true;
        }

        define( 'RADLE_LOGGING_ENABLED', $this->debug_mode );
    }

    /**
     * Load plugin text domain for internationalization.
     * 
     * @access private
     */
    private function load_textdomain() {
        load_plugin_textdomain(
            'radle-lite',
            false,
            dirname( plugin_basename( RADLE_PLUGIN_FILE ) ) . '/languages'
        );
    }

    /**
     * Include required plugin files.
     * 
     * Loads:
     * - Core modules
     * - Settings sections
     * - API endpoints
     * - Utility classes
     * 
     * @access private
     */
    private function includes() {
        require_once RADLE_PLUGIN_DIR . 'modules/utilities/log.php';
        require_once RADLE_PLUGIN_DIR . 'modules/utilities/markdown-handler.php';

        if ( is_admin() ) {
            require_once RADLE_PLUGIN_DIR . 'modules/welcome/welcome-module.php';
        }

        require_once RADLE_PLUGIN_DIR . 'modules/settings/setting-class.php';
        require_once RADLE_PLUGIN_DIR . 'modules/settings/settings-container.php';
        require_once RADLE_PLUGIN_DIR . 'modules/settings/sections/reddit-api-settings.php';
        require_once RADLE_PLUGIN_DIR . 'modules/settings/sections/publishing-settings.php';
        require_once RADLE_PLUGIN_DIR . 'modules/settings/sections/comments-settings.php';
        require_once RADLE_PLUGIN_DIR . 'modules/settings/sections/monitoring-settings.php';

        require_once RADLE_PLUGIN_DIR . 'modules/publish/publish.php';
        require_once RADLE_PLUGIN_DIR . 'modules/publish/destination-override.php';
        require_once RADLE_PLUGIN_DIR . 'modules/publish/seo-token-support.php';
        require_once RADLE_PLUGIN_DIR . 'modules/comments/comments.php';
        require_once RADLE_PLUGIN_DIR . 'modules/comments/comments-search.php';
        require_once RADLE_PLUGIN_DIR . 'modules/reddit/user-agent.php';
        require_once RADLE_PLUGIN_DIR . 'modules/reddit/rate-limit-monitor.php';
        require_once RADLE_PLUGIN_DIR . 'modules/reddit/reddit-api.php';
        require_once RADLE_PLUGIN_DIR . 'modules/reddit/image-upload.php';
        require_once RADLE_PLUGIN_DIR . 'modules/reddit/cache-manager.php';

        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/associate-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/check-auth-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/comments-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/delete-token-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/entries-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/oauth-callback-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/prepare-images-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/publish-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/reddit/refresh-token-endpoint.php';

        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/disassociate-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/hide-comment-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/preview-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/rate-limit-data-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/settings-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/subreddit-endpoint.php';
        require_once RADLE_PLUGIN_DIR . 'api/v1/radle/welcome-endpoints.php';
    }

    /**
     * Initialize plugin modules.
     * 
     * Sets up:
     * - Settings pages
     * - Reddit API integration
     * - Comments system
     * - Publishing tools
     *
     * @access private
     */
    private function initialize_modules() {
        $this->logs = new \Radle\Utilities\log();

        /**
         * Fires before Radle modules are initialized
         *
         * Allows extensions to prepare before module loading
         *
         * @since 1.2.0
         */
        do_action('radle_before_modules_init');

        new Radle\Modules\Settings\Settings_Container();
        new Radle\Modules\Settings\Reddit_Api_Settings();
        new Radle\Modules\Settings\Publishing_Settings();
        new Radle\Modules\Settings\Comment_Settings();
        new Radle\Modules\Settings\Monitoring_Settings();
        new Radle\Modules\Publish\publish();
        new Radle\Modules\Publish\Destination_Override();
        new Radle\Modules\Publish\SEO_Token_Support();
        new Radle\Modules\Comments\comments();
        new Radle\Modules\Comments\Comments_Search();
        new Radle\Modules\Reddit\Cache_Manager();

        if ( is_admin() ) {
            new Radle\Modules\Welcome\Welcome_Module();
        }

        /**
         * Fires after Radle modules are initialized
         *
         * Allows extensions to add their own modules
         *
         * @since 1.2.0
         * @param Radle_Plugin $plugin The main plugin instance
         */
        do_action('radle_after_modules_init', $this);
    }

    /**
     * Register REST API endpoints.
     * 
     * Sets up endpoints for:
     * - Reddit API integration
     * - Comment management
     * - Publishing tools
     * - Settings management
     */
    public function register_rest_endpoints() {
        new \Radle\API\v1\Reddit\Associate_Endpoint();
        new \Radle\API\v1\Reddit\Check_Auth_Endpoint();
        new \Radle\API\v1\Reddit\Comments_Endpoint();
        new \Radle\API\v1\Reddit\Delete_Token_Endpoint();
        new \Radle\API\v1\Reddit\Entries_Endpoint();
        new \Radle\API\v1\Reddit\OAuth_Callback_Endpoint();
        new \Radle\API\v1\Reddit\Prepare_Images_Endpoint();
        new \Radle\API\v1\Reddit\Publish_Endpoint();
        new \Radle\API\v1\Reddit\Refresh_Token_Endpoint();

        new \Radle\API\v1\Radle\Disassociate_Endpoint();
        new \Radle\API\v1\Radle\Hide_Comment_Endpoint();
        new \Radle\API\v1\Radle\Preview_Endpoint();
        new \Radle\API\v1\Radle\Rate_Limit_Data_Endpoint();
        new \Radle\API\v1\Radle\Settings_Endpoint();
        new \Radle\API\v1\Radle\Subreddit_Endpoint();
        new \Radle\API\v1\Radle\Welcome_Endpoints();

        /**
         * Fires after Radle REST endpoints are registered
         *
         * Allows extensions to register additional REST endpoints
         *
         * @since 1.2.0
         */
        do_action('radle_register_rest_endpoints');
    }

    /**
     * Enqueue plugin scripts and styles.
     * 
     * Loads required assets for:
     * - Comment system
     * - Publishing tools
     * - Admin interface
     */
    public function enqueue_scripts($hook) {
        $plugin_version = $this->get_plugin_version();
        $plugin_url = plugin_dir_url(__FILE__);

        // Only enqueue debug utility in local environments
        if ($this->debug_mode) {
            wp_enqueue_script(
                'radle-debug',
                $plugin_url . 'modules/utilities/js/debug.js',
                array('jquery'),
                $plugin_version,
                false
            );
        } else {
            // Register a dummy handle for production so dependencies don't break
            wp_register_script('radle-debug', false);
        }
    }

    /**
     * Check if Pro extension is loaded
     *
     * @since 1.2.0
     * @return bool True if Pro is loaded
     */
    public function has_pro() {
        return defined('RADLE_PRO_VERSION');
    }

    /**
     * Get Pro version
     *
     * @since 1.2.0
     * @return string|false Pro version string or false if not loaded
     */
    public function get_pro_version() {
        return defined('RADLE_PRO_VERSION') ? RADLE_PRO_VERSION : false;
    }

    /**
     * Add plugin action links.
     *
     * Adds quick access links to:
     * - Settings page
     * - Documentation
     *
     * @param array $links Existing plugin action links
     * @return array Modified plugin action links
     */
    public function add_plugin_action_links($links) {
        $settings_link = '<a href="' . admin_url('admin.php?page=radle-settings') . '">' . __('Settings','radle-lite') . '</a>';
        $gbti_link = '<a href="https://gbti.network/?ref=atwellpub&utm_source=radle-lite&utm_medium=wordpress-plugin&utm_campaign=plugin-row" target="_blank">' . __('GBTI Network','radle-lite') . '</a>';
        array_unshift($links, $settings_link, $gbti_link);
        return $links;
    }

    /**
     * Get the plugin version.
     *
     * @return string Plugin version
     */
    private function get_plugin_version() {
        return RADLE_VERSION;
    }
}

$Radle = new Radle_Plugin();

global $radleLogs;
$radleLogs = $Radle->logs;
