<?php
/**
 * Cloudflare Hooks - Connect to BotFend Events
 *
 * @package BotFend\Cloudflare
 * @since 4.9.0
 */

namespace BotFend\Cloudflare;

use BotFend\Core\Config;
use BotFend\Security\Encryption;
use BotFend\License\LicenseKey;

defined( 'ABSPATH' ) || exit;

/**
 * Cloudflare Hooks class.
 *
 * @since 4.9.0
 */
class CloudflareHooks {

	/**
	 * Config instance.
	 *
	 * @since 4.9.0
	 * @var Config
	 */
	private $config;

	/**
	 * Sync instance.
	 *
	 * @since 4.9.0
	 * @var CloudflareSync|null
	 */
	private $sync;

	/**
	 * Whether integration is enabled.
	 *
	 * @since 4.9.0
	 * @var bool
	 */
	private $is_enabled = false;

	/**
	 * Whether Pro license is valid and has cloudflare feature.
	 *
	 * @since 4.9.0
	 * @var bool
	 */
	private $is_pro = false;

	/**
	 * Constructor.
	 *
	 * @since 4.9.0
	 */
	public function __construct() {
		// Check if Pro license is valid and has cloudflare feature
		if ( class_exists( 'BotFend\\License\\LicenseKey' ) ) {
			try {
				$license = LicenseKey::get_instance();
				$this->is_pro = $license->has_feature( 'cloudflare' );
			} catch ( \Throwable $e ) {
				if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
					error_log( 'BotFend Cloudflare license check error: ' . $e->getMessage() );
				}
				$this->is_pro = false;
			}
		} else {
			// License system not installed - Cloudflare is not available
			$this->is_pro = false;
		}

		// Only proceed if Pro license has cloudflare feature
		if ( ! $this->is_pro ) {
			return;
		}

		$this->config = Config::get_instance();
		$this->is_enabled = $this->config->get_bool( 'cf_enabled', false );

		if ( $this->is_enabled ) {
			$this->init();
		}
	}

	/**
	 * Initialize hooks.
	 *
	 * @since 4.9.0
	 *
	 * @return void
	 */
	private function init(): void {
		$sync_blocks = $this->config->get_bool( 'cf_sync_blocks', true );

		if ( ! $sync_blocks ) {
			return;
		}

		// Double-check license before proceeding
		if ( ! $this->is_pro ) {
			return;
		}

		try {
			if ( ! class_exists( 'BotFend\\Cloudflare\\CloudflareAPI' ) ) {
				$api_file = BOTFEND_PATH . 'includes/cloudflare/CloudflareAPI.php';
				if ( file_exists( $api_file ) ) {
					require_once $api_file;
				} else {
					if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
						error_log( 'BotFend: CloudflareAPI.php not found for hooks' );
					}
					return;
				}
			}

			if ( ! class_exists( 'BotFend\\Cloudflare\\CloudflareSync' ) ) {
				$sync_file = BOTFEND_PATH . 'includes/cloudflare/CloudflareSync.php';
				if ( file_exists( $sync_file ) ) {
					require_once $sync_file;
				} else {
					if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
						error_log( 'BotFend: CloudflareSync.php not found for hooks' );
					}
					return;
				}
			}

			$encryption = Encryption::get_instance();
			$api = new CloudflareAPI( $this->config, $encryption );

			if ( ! $api->is_configured() ) {
				return;
			}

			$this->sync = new CloudflareSync( $api, $this->config );

			// Hook into BotFend's block events.
			add_action( 'botfend_ip_blocked', array( $this, 'on_ip_blocked' ), 10, 4 );
			add_action( 'botfend_ip_unblocked', array( $this, 'on_ip_unblocked' ), 10, 1 );
			
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'BotFend Cloudflare hooks initialized successfully' );
			}
		} catch ( \Throwable $e ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'BotFend Cloudflare Hooks init error: ' . $e->getMessage() );
			}
		}
	}

	/**
	 * Handle IP blocked event.
	 *
	 * @since 4.9.0
	 *
	 * @param string $ip     Blocked IP address.
	 * @param string $type   Block type.
	 * @param int    $score  Threat score.
	 * @param string $reason Block reason.
	 * @return void
	 */
	public function on_ip_blocked( string $ip, string $type, int $score, string $reason ): void {
		// Verify license again at execution time
		if ( ! $this->is_pro ) {
			return;
		}
		
		if ( ! $this->sync ) {
			return;
		}

		$min_score = $this->config->get_int( 'cf_min_block_score', 5 );

		if ( $score < $min_score ) {
			return;
		}

		$this->sync->add_ip_to_blocklist( $ip, $type, $score, $reason );
	}

	/**
	 * Handle IP unblocked event.
	 *
	 * @since 4.9.0
	 *
	 * @param string $ip Unblocked IP address.
	 * @return void
	 */
	public function on_ip_unblocked( string $ip ): void {
		// Verify license again at execution time
		if ( ! $this->is_pro ) {
			return;
		}
		
		if ( ! $this->sync ) {
			return;
		}

		$this->sync->remove_ip_from_blocklist( $ip );
	}
	
	/**
	 * Check if Pro license is active (public method).
	 *
	 * @since 4.9.0
	 *
	 * @return bool
	 */
	public function is_pro(): bool {
		return $this->is_pro;
	}
	
	/**
	 * Check if Cloudflare integration is enabled.
	 *
	 * @since 4.9.0
	 *
	 * @return bool
	 */
	public function is_enabled(): bool {
		return $this->is_enabled && $this->is_pro;
	}
}