<?php
/**
 * Plugin Name: Pexally Email Campaigns
 * Plugin URI: https://pexally.com/pexally-email-campaigns/
 * Description: Professional email marketing automation for WordPress. Send campaigns, track opens and clicks, manage contacts, and ensure high inbox delivery.
 * Version: 2.0.0
 * Author: Pexally
 * Author URI: https://pexally.com
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: pexally-email-campaigns
 */

// Exit if accessed directly (Strict WP.org security requirement)
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Define Plugin Constants with strict prefixes
define( 'PEXEMAIL_VERSION', '2.0.0' );
define( 'PEXEMAIL_FILE', __FILE__ );
define( 'PEXEMAIL_DIR', plugin_dir_path( __FILE__ ) );
define( 'PEXEMAIL_URL', plugin_dir_url( __FILE__ ) );

/**
 * The code that runs during plugin activation.
 */
function pexemail_activate_plugin() {
    $activator_file = PEXEMAIL_DIR . 'includes/class-pexemail-activator.php';
    
    // Failsafe: Check if the activator file exists before requiring it
    if ( file_exists( $activator_file ) ) {
        require_once $activator_file;
        PexEmail_Activator::activate();
    } else {
        // Gracefully stop activation and tell the user what went wrong
        wp_die( esc_html__( 'Fatal Error: Missing file includes/class-pexemail-activator.php. Please ensure you created this file in the correct folder.', 'pexally-email-campaigns' ) );
    }
}
register_activation_hook( __FILE__, 'pexemail_activate_plugin' );

/**
 * Initialize the core plugin classes with Failsafes.
 */
function pexemail_init_plugin() {
    $missing_files = [];

    // 1. Load License Engine
    $license_file = PEXEMAIL_DIR . 'includes/class-pexemail-license.php';
    if ( file_exists( $license_file ) ) {
        require_once $license_file;
        $license = new PexEmail_License();
        $license->init();
    } else {
        $missing_files[] = 'includes/class-pexemail-license.php';
    }

    // 2. Load AJAX Backend Engine
    $ajax_file = PEXEMAIL_DIR . 'includes/class-pexemail-ajax.php';
    if ( file_exists( $ajax_file ) ) {
        require_once $ajax_file;
        $ajax = new PexEmail_Ajax();
        $ajax->init();
    } else {
        $missing_files[] = 'includes/class-pexemail-ajax.php';
    }

    // 3. Load Global SMTP Override Engine
    $smtp_file = PEXEMAIL_DIR . 'includes/class-pexemail-smtp.php';
    if ( file_exists( $smtp_file ) ) {
        require_once $smtp_file;
        $smtp = new PexEmail_SMTP();
        $smtp->init();
    } else {
        $missing_files[] = 'includes/class-pexemail-smtp.php';
    }

    // 4. Load Tracking API Engine
    $tracking_file = PEXEMAIL_DIR . 'includes/class-pexemail-tracking.php';
    if ( file_exists( $tracking_file ) ) {
        require_once $tracking_file;
        $tracking = new PexEmail_Tracking();
        $tracking->init();
    } else {
        $missing_files[] = 'includes/class-pexemail-tracking.php';
    }

    // 5. Load Background Cron Engine
    $cron_file = PEXEMAIL_DIR . 'includes/class-pexemail-cron.php';
    if ( file_exists( $cron_file ) ) {
        require_once $cron_file;
        $cron = new PexEmail_Cron();
        $cron->init();
    } else {
        $missing_files[] = 'includes/class-pexemail-cron.php';
    }

    // 6. Load Admin Core Menu
    if ( is_admin() ) {
        $admin_file = PEXEMAIL_DIR . 'includes/class-pexemail-admin.php';
        if ( file_exists( $admin_file ) ) {
            require_once $admin_file;
            $admin = new PexEmail_Admin();
            $admin->init();
        } else {
            $missing_files[] = 'includes/class-pexemail-admin.php';
        }
    }

    // If any files were missing or named wrong, show a clear warning properly localized
    if ( ! empty( $missing_files ) && is_admin() ) {
        add_action( 'admin_notices', function() use ( $missing_files ) {
            echo '<div class="notice notice-error" style="border-left-color:#dc2626;">';
            echo '<p><strong>' . esc_html__( 'Pexally Email Campaigns Error:', 'pexally-email-campaigns' ) . '</strong> ' . esc_html__( 'The plugin cannot run because the following required files are missing or have a typo in their name. Please check your folders:', 'pexally-email-campaigns' ) . '</p>';
            echo '<ul style="list-style-type:disc; margin-left:20px;">';
            foreach( $missing_files as $file ) {
                echo '<li><code>' . esc_html( $file ) . '</code></li>';
            }
            echo '</ul></div>';
        });
    }
}
add_action( 'plugins_loaded', 'pexemail_init_plugin' );