<?php
/**
 * Widget injection logic
 *
 * @package BubblaV_Chat
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Widget class
 */
class Bubblav_Widget {

	/**
	 * Inject widget script on frontend
	 *
	 * @return void
	 */
	public function inject_widget() {
		// Only on frontend.
		if ( is_admin() ) {
			return;
		}

		// Check connection status.
		$status = get_option( 'bubblav_connection_status', '' );
		if ( 'connected' !== $status ) {
			return;
		}

		$website_id = get_option( 'bubblav_website_id', '' );
		if ( empty( $website_id ) ) {
			return;
		}

		// Check if WooCommerce is active.
		$is_woocommerce = class_exists( 'WooCommerce' );

		// Get site URL.
		$domain = home_url();

		// Build the widget script tag with all required attributes.
		$widget_url = 'https://www.bubblav.com/widget.js';
		$script_tag = sprintf(
			'<script src="%s" data-site-id="%s" data-domain="%s" data-platform="%s" data-ecommerce="%s" async defer></script>',
			esc_url( $widget_url ),
			esc_attr( $website_id ),
			esc_attr( $domain ),
			esc_attr( 'WordPress' ),
			esc_attr( $is_woocommerce ? 'true' : 'false' )
		);

		// Output the script tag directly since we're in wp_footer.
		// Use wp_kses to satisfy WordPress coding standards.
		$allowed_html = array(
			'script' => array(
				'src'           => true,
				'data-site-id'  => true,
				'data-domain'   => true,
				'data-platform' => true,
				'data-ecommerce' => true,
				'async'         => true,
				'defer'         => true,
			),
		);
		echo wp_kses( $script_tag, $allowed_html );
	}

	/**
	 * Get widget script URL
	 *
	 * @return string Widget script URL
	 */
	public function get_widget_script_url() {
		return 'https://www.bubblav.com/widget.js';
	}

	/**
	 * Get widget configuration
	 *
	 * @return array Widget configuration
	 */
	public function get_widget_config() {
		return array(
			'siteId'   => get_option( 'bubblav_website_id', '' ),
			'domain'   => home_url(),
			'platform' => 'WordPress',
			'ecommerce' => class_exists( 'WooCommerce' ) ? 'true' : 'false',
		);
	}

	/**
	 * Check if widget should be displayed
	 *
	 * @return bool True if widget should be displayed
	 */
	public function should_display_widget() {
		// Don't display in admin.
		if ( is_admin() ) {
			return false;
		}

		// Don't display if not connected.
		if ( get_option( 'bubblav_connection_status' ) !== 'connected' ) {
			return false;
		}

		// Don't display if no website ID.
		if ( empty( get_option( 'bubblav_website_id' ) ) ) {
			return false;
		}

		/**
		 * Filter whether to display the widget on current page
		 *
		 * @param bool $display Whether to display the widget
		 */
		return apply_filters( 'bubblav_display_widget', true );
	}

	/**
	 * Get widget placeholder HTML (for no-JS fallback)
	 *
	 * @return string Widget placeholder HTML
	 */
	public function get_widget_placeholder() {
		$website_id = get_option( 'bubblav_website_id', '' );

		if ( empty( $website_id ) ) {
			return '';
		}

		return sprintf(
			'<div id="bubblav-widget-placeholder" data-site-id="%s" style="display:none;"></div>',
			esc_attr( $website_id )
		);
	}
}
