<?php
/**
 * Plugin Name:       Rewind Form Bridge for Brevo
 * Description:       Send your site's email through Brevo SMTP, and add every form submission to a Brevo contact list automatically.
 * Version:           1.0.0
 * Requires at least: 6.6
 * Requires PHP:      7.4
 * Author:            jakirulislambd19
 * Author URI:        https://profiles.wordpress.org/jakirulislambd19/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       rewind-form-bridge-for-brevo
 *
 * @package RewindBrevoFormBridge
 */

defined( 'ABSPATH' ) || exit;

define( 'RBFB_VERSION', '1.0.0' );
define( 'RBFB_FILE', __FILE__ );
define( 'RBFB_PATH', plugin_dir_path( __FILE__ ) );
define( 'RBFB_URL', plugin_dir_url( __FILE__ ) );
define( 'RBFB_DB_VERSION', '1.0.0' );

/**
 * Collect unmet environment requirements.
 *
 * Kept procedural and free of PHP 7.4+ syntax so this file parses on any
 * PHP version old enough to need the guard.
 *
 * @since 0.1.0
 * @return string[] Human-readable requirement failures, empty when fine.
 */
function rbfb_environment_problems() {
	global $wp_version;

	$problems = array();

	if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
		$problems[] = sprintf(
			/* translators: 1: required PHP version, 2: current PHP version. */
			__( 'Rewind Form Bridge for Brevo requires PHP %1$s or newer. This site runs PHP %2$s.', 'rewind-form-bridge-for-brevo' ),
			'7.4',
			PHP_VERSION
		);
	}

	if ( version_compare( $wp_version, '6.6', '<' ) ) {
		$problems[] = sprintf(
			/* translators: 1: required WordPress version, 2: current WordPress version. */
			__( 'Rewind Form Bridge for Brevo requires WordPress %1$s or newer. This site runs WordPress %2$s.', 'rewind-form-bridge-for-brevo' ),
			'6.6',
			$wp_version
		);
	}

	return $problems;
}

/**
 * Deactivate self and print an admin notice when the environment is unmet.
 *
 * @since 0.1.0
 * @return void
 */
function rbfb_environment_guard() {
	$problems = rbfb_environment_problems();

	if ( empty( $problems ) ) {
		return;
	}

	if ( ! function_exists( 'deactivate_plugins' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	deactivate_plugins( plugin_basename( RBFB_FILE ) );

	add_action(
		'admin_notices',
		function () use ( $problems ) {
			echo '<div class="notice notice-error"><p>';
			echo esc_html( implode( ' ', $problems ) );
			echo ' ';
			esc_html_e( 'The plugin has been deactivated.', 'rewind-form-bridge-for-brevo' );
			echo '</p></div>';
		}
	);
}

/**
 * Boot the plugin container.
 *
 * @since 0.1.0
 * @return void
 */
function rbfb_boot() {
	if ( count( rbfb_environment_problems() ) > 0 ) {
		add_action( 'admin_init', 'rbfb_environment_guard' );
		return;
	}

	require_once RBFB_PATH . 'includes/class-autoloader.php';
	\RewindBrevoFormBridge\Autoloader::register();

	\RewindBrevoFormBridge\Plugin::instance();
}
add_action( 'plugins_loaded', 'rbfb_boot' );

/**
 * Activation: install the database schema. No API calls happen here — ever.
 *
 * @since 0.1.0
 * @return void
 */
function rbfb_activate() {
	if ( count( rbfb_environment_problems() ) > 0 ) {
		return; // The plugins_loaded guard will deactivate and explain.
	}

	require_once RBFB_PATH . 'includes/class-autoloader.php';
	\RewindBrevoFormBridge\Autoloader::register();

	\RewindBrevoFormBridge\Schema::install();
}
register_activation_hook( __FILE__, 'rbfb_activate' );

/**
 * Deactivation: nothing to tear down in this phase. Scheduled events arrive
 * with the queue in Phase 4 and will be cleared here.
 *
 * @since 0.1.0
 * @return void
 */
function rbfb_deactivate() {
	// Intentionally empty for Phase 1.
}
register_deactivation_hook( __FILE__, 'rbfb_deactivate' );
