<?php
/**
 * The core plugin class.
 *
 * @package    WPFMJ
 * @subpackage WPFMJ/includes
 */

class WPFMJ_Core {

    /**
     * The loader that's responsible for maintaining and registering all hooks.
     */
    protected $loader;

    /**
     * The unique identifier of this plugin.
     */
    protected $plugin_name;

    /**
     * The current version of the plugin.
     */
    protected $version;

    /**
     * Initialize the plugin.
     */
    public function __construct() {
        $this->version = WPFMJ_VERSION;
        $this->plugin_name = 'wpforms-mailjet-automation';

        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
    }

    /**
     * Load the required dependencies for this plugin.
     */
    private function load_dependencies() {
        // Define required files
        $required_files = array(
            // Core classes
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-loader.php',
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-cpt.php',
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-encryption.php',
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-mailjet-api.php',
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-form-handler.php',
            WPFMJ_PLUGIN_DIR . 'includes/class-wpfmj-error-logger.php',
        );

        // Add admin files if in admin context
        if (is_admin()) {
            $required_files[] = WPFMJ_PLUGIN_DIR . 'admin/class-wpfmj-admin.php';
            $required_files[] = WPFMJ_PLUGIN_DIR . 'admin/class-wpfmj-dashboard.php';
        }

        // Check for missing files
        $missing_files = array();
        foreach ($required_files as $file) {
            if (!file_exists($file)) {
                $missing_files[] = basename($file);
            }
        }

        // If any files are missing, show error and stop loading
        if (!empty($missing_files)) {
            add_action('admin_notices', function() use ($missing_files) {
                ?>
                <div class="notice notice-error">
                    <p>
                        <strong><?php esc_html_e('WPForms to Mailjet Automation Error:', 'wpforms-mailjet-automation'); ?></strong>
                        <?php 
                        echo esc_html(
                            sprintf(
                                __('Missing required files: %s. Please reinstall the plugin.', 'wpforms-mailjet-automation'),
                                implode(', ', $missing_files)
                            )
                        ); 
                        ?>
                    </p>
                </div>
                <?php
            });
            return;
        }

        // All files exist, load them
        foreach ($required_files as $file) {
            require_once $file;
        }

        $this->loader = new WPFMJ_Loader();
    }

    /**
     * Register all of the hooks related to the admin area functionality.
     */
    private function define_admin_hooks() {
        if (!is_admin()) {
            return;
        }

        $admin = new WPFMJ_Admin($this->get_plugin_name(), $this->get_version());
        $dashboard = new WPFMJ_Dashboard();
        $cpt = new WPFMJ_CPT();

        // Admin assets
        $this->loader->add_action('admin_enqueue_scripts', $admin, 'enqueue_styles');
        $this->loader->add_action('admin_enqueue_scripts', $admin, 'enqueue_scripts');

        // Dashboard
        $this->loader->add_action('admin_menu', $dashboard, 'add_menu_page');

        // Custom post type
        $this->loader->add_action('init', $cpt, 'register_post_type');
        
        // AJAX endpoints
        $this->loader->add_action('wp_ajax_wpfmj_get_forms', $admin, 'ajax_get_forms');
        $this->loader->add_action('wp_ajax_wpfmj_get_form_fields', $admin, 'ajax_get_form_fields');
        $this->loader->add_action('wp_ajax_wpfmj_test_mailjet', $admin, 'ajax_test_mailjet');
        $this->loader->add_action('wp_ajax_wpfmj_get_mailjet_lists', $admin, 'ajax_get_mailjet_lists');
        $this->loader->add_action('wp_ajax_wpfmj_save_automation', $admin, 'ajax_save_automation');
        $this->loader->add_action('wp_ajax_wpfmj_get_automation', $admin, 'ajax_get_automation');
        $this->loader->add_action('wp_ajax_wpfmj_toggle_automation', $admin, 'ajax_toggle_automation');
        $this->loader->add_action('wp_ajax_wpfmj_delete_automation', $admin, 'ajax_delete_automation');
        $this->loader->add_action('wp_ajax_wpfmj_get_dashboard_data', $admin, 'ajax_get_dashboard_data');
    }

    /**
     * Register all of the hooks related to the public-facing functionality.
     */
    private function define_public_hooks() {
        $form_handler = new WPFMJ_Form_Handler();
        
        // WPForms submission hook
        $this->loader->add_action('wpforms_process_complete', $form_handler, 'handle_submission', 10, 4);
    }

    /**
     * Run the loader to execute all of the hooks with WordPress.
     */
    public function run() {
        $this->loader->run();
    }

    /**
     * The name of the plugin.
     */
    public function get_plugin_name() {
        return $this->plugin_name;
    }

    /**
     * The reference to the class that orchestrates the hooks with the plugin.
     */
    public function get_loader() {
        return $this->loader;
    }

    /**
     * Retrieve the version number of the plugin.
     */
    public function get_version() {
        return $this->version;
    }
}
