<?php
/**
 * Mail Doctor bootstrap file.
 *
 * @package MailDoctor
 */

/**
 * Plugin Name: Mail Doctor
 * Description: SMTP deliverability, diagnostics, and retry toolkit for WooCommerce emails.
 * Plugin URI: https://wpdoctorpro.com/products/mail-doctor
 * Version: 1.1.0
 * Author: WP Doctor Pro
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Documentation URI: https://wpdoctorpro.com/docs/mail-doctor/welcome-to-mail-doctor
 * Text Domain: mail-doctor
 * Domain Path: /languages
 * Requires at least: 6.0
 * Tested up to: 7.0
 * Requires PHP: 8.1
 */

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

define( 'MAIL_DOCTOR_VERSION', '1.1.0' );
define( 'MAIL_DOCTOR_PATH', plugin_dir_path( __FILE__ ) );
define( 'MAIL_DOCTOR_URL', plugin_dir_url( __FILE__ ) );

/**
 * Minimal autoloader for plugin classes.
 *
 * @param string $class Class name.
 */
spl_autoload_register(
	static function ( $class ) {
		if ( strpos( $class, 'MailDoctor\\' ) !== 0 ) {
			return;
		}

		$map = array(
			'MailDoctor\Admin'                    => 'includes/admin/class-mail-doctor-admin.php',
			'MailDoctor\Onboarding'               => 'includes/admin/class-mail-doctor-onboarding.php',
			'MailDoctor\Network'                  => 'includes/admin/class-mail-doctor-network-admin.php',
			'MailDoctor\Logs_Table'               => 'includes/admin/class-mail-doctor-logs-table.php',
			'MailDoctor\Mailer'                   => 'includes/mail/mailer/class-mail-doctor-mailer.php',
			'MailDoctor\Logger'                   => 'includes/core/class-mail-doctor-logger.php',
			'MailDoctor\Retry'                    => 'includes/mail/retry/class-mail-doctor-retry.php',
			'MailDoctor\Checks'                   => 'includes/services/class-mail-doctor-checks.php',
			'MailDoctor\Profiles'                 => 'includes/services/class-mail-doctor-profiles.php',
			'MailDoctor\Suppression'              => 'includes/services/class-mail-doctor-suppression.php',

			'MailDoctor\Woo'                      => 'includes/integrations/class-mail-doctor-woocommerce.php',
			'MailDoctor\OAuth'                    => 'includes/auth/class-mail-doctor-oauth.php',
			'MailDoctor\OAuthTokenCredential'     => 'includes/auth/class-mail-doctor-oauth-token.php',
			'MailDoctor\Capabilities'             => 'includes/support/class-mail-doctor-capabilities.php',
			'MailDoctor\Privacy'                  => 'includes/support/class-mail-doctor-privacy.php',
			'MailDoctor\Secrets'                  => 'includes/support/class-mail-doctor-secrets.php',
			'MailDoctor\Transports\Mailgun'       => 'includes/transports/class-mail-doctor-transport-mailgun.php',
			'MailDoctor\Transports\SendGrid'      => 'includes/transports/class-mail-doctor-transport-sendgrid.php',
			'MailDoctor\Transports\Postmark'      => 'includes/transports/class-mail-doctor-transport-postmark.php',
			'MailDoctor\Transports\SparkPost'     => 'includes/transports/class-mail-doctor-transport-sparkpost.php',
			'MailDoctor\Transports\Ses'           => 'includes/transports/class-mail-doctor-transport-ses.php',
			'MailDoctor\Transports\Generic_Http'  => 'includes/transports/class-mail-doctor-transport-generic-http.php',
			'MailDoctor\Transports\Utils'         => 'includes/transports/class-mail-doctor-transport-utils.php',
			'MailDoctor\Mail\Config\SMTP_Config'  => 'includes/mail/config/smtp/class-smtp-config.php',
			'MailDoctor\Mail\Config\API_Config'   => 'includes/mail/config/api/class-api-config.php',
			'MailDoctor\Mail\Config\Providers\Presets' => 'includes/mail/config/providers/class-provider-presets.php',

			'MailDoctor\Migrations\Migrations'    => 'includes/migrations/class-migrations.php',
			'MailDoctor\Features'                 => 'includes/support/class-mail-doctor-features.php',
		);

		if ( isset( $map[ $class ] ) ) {
			require_once MAIL_DOCTOR_PATH . $map[ $class ];
		}
	}
);

add_filter(
	'plugin_action_links_' . plugin_basename( __FILE__ ),
	static function ( $links ) {
		if ( ! \MailDoctor\Features::is_pro() ) {
			$upgrade = sprintf(
				'<a href="%1$s" target="_blank" rel="noopener noreferrer"><strong>%2$s</strong></a>',
				esc_url( 'https://wpdoctorpro.com/' ),
				esc_html__( 'Upgrade to Pro', 'mail-doctor' )
			);

			array_unshift( $links, $upgrade );
		}

		return $links;
	}
);

add_filter(
	'plugin_row_meta',
	static function ( $links, $file ) {
		if ( plugin_basename( __FILE__ ) !== $file ) {
			return $links;
		}

		if ( ! \MailDoctor\Features::is_pro() ) {
			$links[] = sprintf(
				'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
				esc_url( 'https://wpdoctorpro.com/' ),
				esc_html__( 'Buy Mail Doctor Pro', 'mail-doctor' )
			);
		}

		return $links;
	},
	10,
	2
);

add_action(
	'init',
	static function () {
		if ( class_exists( 'MailDoctor\Migrations\Migrations' ) ) {
			\MailDoctor\Migrations\Migrations::run();
		}
	},
	1
);

require_once MAIL_DOCTOR_PATH . 'includes/support/helpers.php';



/**
 * Runs on plugin activation.
 */
function mail_doctor_activate() {
	if ( class_exists( 'MailDoctor\\Features' ) ) {
		MailDoctor\Features::init();
	}

	MailDoctor\Logger::install();
	MailDoctor\Retry::activate();
	MailDoctor\Profiles::install();
	MailDoctor\Suppression::install();
	MailDoctor\Capabilities::install();
	MailDoctor\Helpers\Migrations::maybe_upgrade( MAIL_DOCTOR_VERSION );
}

/**
 * Runs on plugin deactivation.
 */
function mail_doctor_deactivate() {
	MailDoctor\Retry::deactivate();
	MailDoctor\Logger::deactivate();


}

register_activation_hook( __FILE__, 'mail_doctor_activate' );
register_deactivation_hook( __FILE__, 'mail_doctor_deactivate' );

add_action(
	'plugins_loaded',
	static function () {
		MailDoctor\Features::init();


		if ( ! MailDoctor\Helpers\Dependencies::is_wordpress_supported() ) {
			return;
		}

		MailDoctor\Logger::init();
		MailDoctor\Mailer::init();
		MailDoctor\Retry::init();
		MailDoctor\Onboarding::init();

		MailDoctor\OAuth::init();
		MailDoctor\Admin::init();
		MailDoctor\Woo::init();
		MailDoctor\Privacy::init();

		MailDoctor\Capabilities::ensure_role_mappings();
		MailDoctor\Helpers\Migrations::maybe_upgrade( MAIL_DOCTOR_VERSION );

		// WooCommerce check notice removed as it is no longer required for core functionality.
	}
);
