<?php
/**
 * Plugin Name: NimblyMail SMTP
 * Plugin URI:  https://plugins.svn.wordpress.org/nimblymail-smtp/
 * Description: A modern SMTP and email log plugin for WordPress. Improve deliverability via SMTP, Gmail, Resend, Amazon SES, or SendLayer, with full delivery logging.
 * Version:     1.0.6
 * Author:      NimblyMail Team
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: nimblymail-smtp
 * Domain Path: /languages
 * Requires at least: 6.0
 * Tested up to: 7.1
 * Requires PHP: 8.0
 *
 * @package NimblyMail\SMTP
 */

defined( 'ABSPATH' ) || exit;

// Plugin constants.
define( 'NIMBLYMAIL_VERSION', '1.0.6' );
define( 'NIMBLYMAIL_FILE', __FILE__ );
define( 'NIMBLYMAIL_PATH', plugin_dir_path( __FILE__ ) );
define( 'NIMBLYMAIL_URL', plugin_dir_url( __FILE__ ) );
define( 'NIMBLYMAIL_BASENAME', plugin_basename( __FILE__ ) );

// PSR-4 autoloader via Composer.
if ( file_exists( NIMBLYMAIL_PATH . 'vendor/autoload.php' ) ) {
    require_once NIMBLYMAIL_PATH . 'vendor/autoload.php';
}

// Action Scheduler bootstrap. Loaded directly so its functions
// (as_schedule_recurring_action, as_next_scheduled_action, etc.) are
// available as soon as plugins_loaded fires. Action Scheduler internally
// negotiates which copy wins when multiple plugins ship it.
if ( file_exists( NIMBLYMAIL_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php' ) ) {
    require_once NIMBLYMAIL_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php';
}

use NimblyMail\SMTP\Plugin;
use NimblyMail\SMTP\Log\Rotation;
use NimblyMail\SMTP\Audit\Scheduler as HealthScheduler;
use NimblyMail\SMTP\Network\NetworkActivationInstaller;
use NimblyMail\SMTP\Network\StatsAggregator;
use NimblyMail\SMTP\Review\ReviewRequest;

/**
 * Returns the main plugin instance.
 *
 * @since 1.0.0
 *
 * @return Plugin
 */
function nimblymail(): Plugin {
    return Plugin::instance();
}

/**
 * Boot the plugin singleton — `plugins_loaded` callback.
 *
 * Wraps `nimblymail()` with a void return so the signature matches the
 * action-callback contract while keeping the accessor usable as a public API.
 *
 * @since 1.0.0
 *
 * @return void
 */
function nimblymail_boot(): void {
    nimblymail();
}

// Boot the plugin.
add_action( 'plugins_loaded', 'nimblymail_boot' );

/**
 * Unschedule all NimblyMail recurring Action Scheduler jobs on deactivation.
 *
 * @since 1.0.0
 * @since 1.0.4 Added StatsAggregator::unschedule() so a chunked
 *              network-dashboard run in progress does not leave an orphan
 *              action queued after deactivation.
 * @since 1.0.6 Added NetworkActivationInstaller::unschedule() so a chunked
 *              network-activation run in progress does not leave an orphan
 *              action queued after deactivation.
 *
 * @return void
 */
function nimblymail_deactivate(): void {
    Rotation::unschedule();
    HealthScheduler::unschedule();
    ReviewRequest::unschedule();
    StatsAggregator::unschedule();
    NetworkActivationInstaller::unschedule();
}

// Activation / Deactivation hooks.
register_activation_hook( __FILE__, array( Plugin::class, 'activate' ) );
register_deactivation_hook( __FILE__, 'nimblymail_deactivate' );
