<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

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

/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGTermsController extends HappyFenceNGService {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_terms_endpoint' ) );
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $method_name the method name?.
	 */
	public static function getStatus( string $method_name ) {
		switch ( $method_name ) {
			case 'register_terms_endpoint':
				return 'true';
			default:
				return HappyFenceNGPlaceholder::UNKNOWN;
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function register_terms_endpoint() {
		register_rest_route(
			'happyfenceng-privacy/v1',
			'/terms',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'render_terms_html' ),
				'permission_callback' => '__return_true',
			)
		);
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function render_terms_html() {
		if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
			return new WP_REST_Response( HappyFenceNGPlaceholder::EMPTY_STRING, 204 );
		}

		if ( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_privacy_management ) {
			if ( HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PRIVACY->enable_terms_banner, HappyFenceNGPlaceholder::BOOL_PROP_DEFAULT ) ) {
				ob_start();
				include plugin_dir_path( __FILE__ ) . '../../templates/terms-template.php';
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo ob_get_clean();
				exit();

			} else {
				if ( ! empty( HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url ) ) {
					$custom_url = esc_url( HappyFenceNGWhoAmI::get_instance()->PRIVACY->terms_url ?? '' );
					echo '<div class="happyfence-privacy-terms-wrapper">';
                    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo '<iframe src="' . $custom_url . '" style="width:100%; height:500px; border:0;"></iframe>';
					echo '</div>';
				}
				exit();

			}
		}
		exit();
	}
}
