<?php
/**
 * Footer branding - "Powered by Goose"
 *
 * Renders a branded footer bar on all frontend pages.
 * Free/limited users: always shown, with DOM re-injection protection.
 * Unlimited plan users: can toggle off via Settings > Widget Controls.
 *
 * @package GGM_Commerce
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class GGM_Commerce_Footer_Branding {

	/**
	 * Initialize footer branding hooks.
	 */
	public static function init() {
		add_action( 'wp_footer', array( __CLASS__, 'render' ), 100 );
	}

	/**
	 * Check if the current user has an unlimited license.
	 *
	 * @return bool
	 */
	private static function is_unlimited() {
		if ( ! class_exists( 'GGM_Commerce_License' ) ) {
			return false;
		}

		$license = GGM_Commerce_License::instance();

		return $license->is_license_valid() && $license->get_widget_limit() === 0;
	}

	/**
	 * Render the "Powered by Goose" footer bar.
	 */
	public static function render() {
		// Don't render in Elementor editor.
		if ( class_exists( '\Elementor\Plugin' ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		// Don't render in Elementor preview mode.
		if ( class_exists( '\Elementor\Plugin' ) && \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
			return;
		}

		$is_unlimited = self::is_unlimited();

		// If unlimited user has disabled branding, skip rendering.
		if ( $is_unlimited ) {
			$widget_controls = get_option( 'ggm_commerce_widget_controls', array() );
			$disabled        = isset( $widget_controls['powered_by_disabled'] ) && $widget_controls['powered_by_disabled'] === '1';

			if ( $disabled ) {
				return;
			}
		}

		// Unique identifier for re-injection.
		$uid = 'ggmc' . substr( md5( 'powered-by-goose' . ABSPATH ), 0, 8 );

		?>
		<div id="<?php echo esc_attr( $uid ); ?>" class="ggmc-pbg" style="background:#1e293b;padding:12px 0;text-align:center;width:100%;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;margin:0;clear:both;position:relative;z-index:10;">
			<a href="https://wpgoose.com" target="_blank" rel="noopener noreferrer" style="color:#f97316;text-decoration:none;font-weight:600;font-size:14px;display:inline-flex;align-items:center;gap:6px;line-height:1;">
				<img src="<?php echo esc_url( plugins_url( 'assets/css/icon.svg', dirname( __FILE__ ) ) ); ?>" alt="" style="width:20px;height:20px;vertical-align:middle;">
				<span>Powered by Goose</span>
			</a>
		</div>
		<?php

		// For non-unlimited users, add DOM protection.
		if ( ! $is_unlimited ) :
			?>
			<script>
			(function(){
				var _i='<?php echo esc_js( $uid ); ?>',_h=document.getElementById(_i);
				if(!_h)return;
				var _s=_h.outerHTML,_p=_h.parentNode;
				try{
					var _o=new MutationObserver(function(m){
						m.forEach(function(r){
							r.removedNodes.forEach(function(n){
								if(n.id===_i||n.querySelector&&n.querySelector('#'+_i)){
									var _t=document.createElement('div');
									_t.innerHTML=_s;
									var _r=_t.firstChild;
									document.body.appendChild(_r);
									_h=_r;_p=_h.parentNode;
								}
							});
						});
					});
					_o.observe(_p,{childList:true,subtree:false});
					var _b=new MutationObserver(function(){
						if(!document.getElementById(_i)){
							var _t=document.createElement('div');
							_t.innerHTML=_s;
							document.body.appendChild(_t.firstChild);
						}
					});
					_b.observe(document.body,{childList:true,subtree:true});
				}catch(e){}
				// Re-check on style tampering.
				setInterval(function(){
					var _e=document.getElementById(_i);
					if(!_e){
						var _t=document.createElement('div');
						_t.innerHTML=_s;
						document.body.appendChild(_t.firstChild);
					}else if(_e.style.display==='none'||_e.style.visibility==='hidden'||_e.style.opacity==='0'||_e.offsetHeight<5){
						_e.setAttribute('style','background:#1e293b;padding:12px 0;text-align:center;width:100%;font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,sans-serif;margin:0;clear:both;position:relative;z-index:10;display:block;visibility:visible;opacity:1;height:auto;overflow:visible;');
					}
				},3000);
			})();
			</script>
			<?php
		endif;
	}
}
