<?php



/**



 * Fired during plugin activation



 *



 * @link       https://sensfrx.ai



 * @since      1.0.0



 *a.sensfrx.ai



 * @package    SensFRX



 * @subpackage SensFRX/includes



 */



/**



 * Fired during plugin activation.



 * a.sensfrx.ai



 * This class defines all code necessary to run during the plugin's activation.



 *



 * @since      1.0.0



 * @package    SensFRX



 * @subpackage SensFRX/includes



 */



class SensFRX_Activator {



	/**



	 * Short Description. (use period)



	 *



	 * Long Description.



	 *



	 * @since    1.0.0



	 */



	public static function activate() {

		// Debug log file path

		$debug_log_file = dirname(dirname(__FILE__)) . '/activation-debug.log';

		file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: Plugin activation started\n", FILE_APPEND);

		

		$options = get_option('sensfrx_options', SensFRX::default_options());

		require_once(SENSFRX_DIR . 'sensfrx-php-sdk/SensFRX/autoload.php');

		

		$p_id = $options['sensfrx_property_id'];

		$s_key = $options['sensfrx_property_secret'];

		

		file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: p_id=" . ($p_id ? $p_id : 'NOT SET') . ", s_key=" . ($s_key ? substr($s_key, 0, 10) . '...' : 'NOT SET') . "\n", FILE_APPEND);

		

		// If credentials already exist, DON'T call getPropertySecrets (it regenerates the secret!)

		// Only register webhooks and update plugin info

		if (!empty($p_id) && !empty($s_key)) {

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: Credentials exist, skipping getPropertySecrets (would regenerate secret!)\n", FILE_APPEND);

			

		$obj = new SensFRX\SensFRX([

				'property_id' => $p_id,

				'property_secret' => $s_key

		]);

			

		global $wp_version;

		$version = $wp_version;

		$site_url = site_url();

		$site_url_without_https = str_replace('https://', '', $site_url);

		$postDataArray = [

			'app_type' => 'WordPress',

			'app_version' => $version,

			'plugin_version' => SENSFRX_VERSION,

			'domain' => $site_url_without_https,

			];

			

			// Update plugin info

				$Activation = $obj->integrateplugininfo($postDataArray);

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: integrateplugininfo response: " . print_r($Activation, true) . "\n", FILE_APPEND);

			

			// Register webhooks

				$webHookUrl = home_url() . '/wp-json/sensfrx-fraud-prevention-for-woocommerce/sensfrx_webhook';

				$webHookUrl_trans = home_url() . '/wp-json/sensfrx-fraud-prevention-for-woocommerce/sensfrx__transaction_webhook';

			

			$webhook_update = $obj->addWebHook($webHookUrl);

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR webhook_update response: " . print_r($webhook_update, true) . "\n", FILE_APPEND);

			

			$webhook_updateTrans = $obj->addWebHook($webHookUrl_trans);

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR webhook_updateTrans response: " . print_r($webhook_updateTrans, true) . "\n", FILE_APPEND);



			// Store signing_secret from webhook registration response

			if ( is_array($webhook_update) && isset($webhook_update['signing_secret']) ) {

				update_option('sensfrx_webhook_signing_secret', $webhook_update['signing_secret']);

				file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR STORED registration secret: " . $webhook_update['signing_secret'] . "\n", FILE_APPEND);

			} else {

				file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR registration secret NOT FOUND in response\n", FILE_APPEND);

			}

			if ( is_array($webhook_updateTrans) && isset($webhook_updateTrans['signing_secret']) ) {

				update_option('sensfrx_transaction_webhook_signing_secret', $webhook_updateTrans['signing_secret']);

				file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR STORED transaction secret: " . $webhook_updateTrans['signing_secret'] . "\n", FILE_APPEND);

			} else {

				file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR transaction secret NOT FOUND in response\n", FILE_APPEND);

			}

			

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: Completed successfully (credentials preserved)\n", FILE_APPEND);

		} else {

			// No credentials set - this is a fresh install, user needs to configure in settings

			file_put_contents($debug_log_file, date('Y-m-d H:i:s') . " - ACTIVATOR: No credentials found, user needs to configure via Settings or OAuth\n", FILE_APPEND);

		}

	}

}



