<?php

/**
 * Fired during plugin activation
 *
 * @link       https://github.com/ParogDev
 * @since      1.0.0
 *
 * @package    WeeConnectPay
 * @subpackage WeeConnectPay/includes
 */

namespace WeeConnectPay\WordPress\Plugin\includes;

use WeeConnectPay\Exceptions\EnvironmentWarningException;
use WeeConnectPay\Exceptions\InsufficientDependencyVersionException;
use WeeConnectPay\Exceptions\MissingDependencyException;
use WeeConnectPay\Integrations\DependencyChecker;
use WeeConnectPay\Integrations\IntegrationSettings;
use WeeConnectPay\Integrations\LogService;

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    WeeConnectPay
 * @subpackage WeeConnectPay/includes
 * @author     ParogDev <integration@cspaiement.com>
 */
class WeeConnectPayActivator {


	/**
	 * Activates the plugin.
	 *
	 * Used when the admin dashboard calls activation, whether it is from the installation UI or the plugin dashboard.
	 *
	 * @since    1.0.0
	 * @updated  3.7.0
	 */
	public static function activate() {
		// Check environment before any integration requests
		self::checkEnvironmentOnActivation();
		
		// Validate Dependencies when initializing plugin
		$dependencyChecker = new DependencyChecker;
		try {
			$dependencyChecker->validateOnRun();
		} catch (InsufficientDependencyVersionException | MissingDependencyException $exception) {
			LogService::error(sprintf("WeeConnectPay initialization error: %s", json_encode($exception->getMessage())));
			$dependencyChecker->notify( $exception);
		}

		// Initializes plugin
		IntegrationSettings::maybeFirstTimeInit();
	}

	/**
	 * Check environment on plugin activation and log warnings if not production
	 * This runs regardless of debug mode setting and happens before integration requests
	 * 
	 * @return void
	 */
	private static function checkEnvironmentOnActivation(): void {
		try {
			$environment = WeeConnectPayUtilities::get_wp_env();
			
			if ($environment !== 'production') {
				// Create environment warning exception with extra instructions
				$warningMessage = sprintf(
					"CRITICAL WARNING: WeeConnectPay is running in '%s' environment instead of 'production'. " .
					"Non-production use of this plugin will NOT WORK because Clover does not expose our app in non-production environments. " .
					"Authentication will fail.",
					$environment
				);
				
				$extraInstructions = sprintf(
					"<strong>REQUIRED ACTION:</strong><br>" .
					"1. Uninstall the WeeConnectPay plugin completely<br>" .
					"2. Set your WordPress environment to 'production'<br>" .
					"3. Reinstall the plugin<br><br>" .
					"<strong>Technical Details:</strong><br>" .
					"This happens because your WP_ENVIRONMENT constant is set to '%s'. " .
					"Non-production authentication is only available to internal WeeConnectPay developers.<br><br>" .
					"If you don't understand why WeeConnectPay is not resolving to production environment, " .
					"please contact <a href='mailto:support@weeconnectpay.com'>support@weeconnectpay.com</a> for assistance.",
					$environment
				);

				$exception = new EnvironmentWarningException($warningMessage, 0, null, $extraInstructions);
				
				// Log the warning (for server logs)
				LogService::error(sprintf("WeeConnectPay environment warning: %s", $exception->getMessage()));
				
				// Use the DependencyChecker notify pattern to show admin notice
				$dependencyChecker = new DependencyChecker();
				$dependencyChecker->notify($exception);
			}
		} catch (\Throwable $e) {
			// Force log the error regardless of debug mode
			LogService::error('Error checking WeeConnectPay environment on activation: ' . $e->getMessage());
		}
	}

}
