<?php
/**
 * Plugin Name: InboxMend – Submission Inbox & Email Log
 * Plugin URI: https://inboxmend.com/
 * Description: Save WordPress form submissions before notification emails fail, review leads, Email Health, and Email Log.
 * Version: 1.3.0
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: phpner
 * Author URI: https://inboxmend.com/
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: submission-inbox
 * Domain Path: /languages
 *
 * @package SubmissionInbox
 */

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

define( 'SUBIN_PLUGIN_FILE', __FILE__ );
define( 'SUBIN_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'SUBIN_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'SUBIN_PLUGIN_VERSION', '1.3.0' );
define( 'SUBIN_PLUGIN_TEXT_DOMAIN', 'submission-inbox' );

require_once SUBIN_PLUGIN_DIR . 'includes/Settings/SettingsRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Integrations/ProviderRegistry.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Cloud/SiteIdentity.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Cloud/CloudApiClient.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Cloud/CloudSyncService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormSubmissionContext.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormAnalyticsRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormAnalyticsService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormOrganizationRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormLocationService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormsOverviewRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormFieldRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/ConditionalLogicEvaluator.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/SubmissionPreflightService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormTemplateRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/DesignSettings.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/ScopedCssGenerator.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormRenderer.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/RecaptchaVerifier.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/SubmissionRateLimiter.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Notifications/NotificationEnvelope.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/SubmissionReceiptRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormSubmissionHandler.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormShortcode.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyTemplateLibrary.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyTemplateRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyConditionEngine.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyMergeTagRenderer.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyPreheader.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyDeliverySnapshot.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyRuleReadiness.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyRuleRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FormPermanentDeletionService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyFormProviderRegistry.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyPolicyService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplyRuleSender.php';
require_once SUBIN_PLUGIN_DIR . 'includes/AutoReplies/AutoReplySender.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Leads/LeadRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Leads/LeadFieldRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Leads/LeadCaptureService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Leads/LeadActivityRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Leads/LeadWorkflowRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailEventRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/EmailLogPreferences.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailEventTypeContext.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailEventEvidenceService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailEventPresenter.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/EmailLogExportService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailErrorSanitizer.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/EmailErrorClassifier.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/EmailHealthIncidentProjector.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/LeadEmailStatusService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/EmailHealthService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/EmailVerificationService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mail/EmailHealthDiagnosticService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Export/CsvExporter.php';
require_once SUBIN_PLUGIN_DIR . 'includes/MailLog/MailLogger.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Notifications/NotificationJobRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/SubmissionAcceptanceService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Sync/SyncEventRepository.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Sync/SyncEventRecorder.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/ContactForm7Integration.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/WPFormsIntegration.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/FluentFormsIntegration.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/ForminatorIntegration.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Forms/NinjaFormsIntegration.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Mailer/SmtpMailer.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Retention/RetentionService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Database.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Security/Capabilities.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Security/TeamAccessService.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Activator.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Deactivator.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Utils/View.php';
require_once SUBIN_PLUGIN_DIR . 'includes/Utils/Request.php';

if ( is_admin() ) {
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/AdminNotices.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/AutoReplyAjaxController.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/InboxPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/EmailStatusPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/EmailLogPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/SmtpSettingsPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/FormsCommandCenter.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/FormsPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/AutoRepliesPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/Pages/SettingsPage.php';
	require_once SUBIN_PLUGIN_DIR . 'includes/Admin/AdminMenu.php';
}

require_once SUBIN_PLUGIN_DIR . 'includes/Plugin.php';

register_activation_hook( __FILE__, array( 'SUBIN_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'SUBIN_Deactivator', 'deactivate' ) );

/**
 * Boot the plugin.
 *
 * @return void
 */
function subin_run_plugin() {
	$ready = SUBIN_Database::ensure_ready();
	if ( is_wp_error( $ready ) ) {
		do_action( 'subin_database_not_ready', $ready );
		if ( is_admin() ) {
			add_action( 'admin_notices', 'subin_database_not_ready_notice' );
		}
		return;
	}

	$capabilities_ready = \SUBIN\Security\Capabilities::maybe_upgrade();
	if ( is_wp_error( $capabilities_ready ) ) {
		do_action( 'subin_capabilities_not_ready', $capabilities_ready );
		if ( is_admin() ) {
			add_action( 'admin_notices', 'subin_capabilities_not_ready_notice' );
		}
	}

	$plugin = new SUBIN_Plugin();
	$plugin->run();
}
add_action( 'plugins_loaded', 'subin_run_plugin' );

/**
 * Show a safe schema failure notice to administrators.
 *
 * @return void
 */
function subin_database_not_ready_notice() {
	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}
	?>
	<div class="notice notice-error">
		<p><?php esc_html_e( 'InboxMend could not finish its database upgrade. The plugin is temporarily paused to protect submission data. Reload the page to retry, then check the WordPress debug log if the problem continues.', 'submission-inbox' ); ?></p>
	</div>
	<?php
}

/**
 * Show a safe capability migration failure without pausing lead capture.
 *
 * @return void
 */
function subin_capabilities_not_ready_notice() {
	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}
	?>
	<div class="notice notice-warning">
		<p><?php esc_html_e( 'InboxMend could not finish updating its role permissions. Existing administrator access remains available; reload the page to retry, then check the WordPress debug log if the problem continues.', 'submission-inbox' ); ?></p>
	</div>
	<?php
}
