<?php
/**
 * Page Builder Integrations — Elementor, Bricks, and Gutenberg.
 *
 * Registers widgets/elements for popular page builders that render
 * Mersal OTP shortcodes: [mersal_otp_login], [mersal_signup], [mersal_phone_bar].
 *
 * @package Mersal_Notif
 */

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

class Mersal_Notif_Integrations {

	/**
	 * Initialize hooks for all integrations.
	 */
	public function init() {
		add_action( 'init', array( $this, 'register_block_patterns' ) );
		add_action( 'elementor/widgets/register', array( $this, 'register_elementor_widgets' ) );
		add_action( 'bricks/register_elements', array( $this, 'register_bricks_elements' ) );
	}

	/**
	 * Register Gutenberg block patterns.
	 */
	public function register_block_patterns() {
		if ( ! function_exists( 'register_block_pattern_category' ) ) {
			return;
		}
		register_block_pattern_category( 'mersal', array(
			'label' => __( 'Mersal', 'mersal-notifications' ),
		) );

		if ( ! function_exists( 'register_block_pattern' ) ) {
			return;
		}
		register_block_pattern( 'mersal/login-form', array(
			'title'       => __( 'Mersal OTP Login', 'mersal-notifications' ),
			'description' => __( 'Passwordless login form with OTP verification.', 'mersal-notifications' ),
			'categories'  => array( 'mersal' ),
			'content'     => '<!-- wp:shortcode -->[mersal_otp_login]<!-- /wp:shortcode -->',
		) );
		register_block_pattern( 'mersal/signup-form', array(
			'title'       => __( 'Mersal Signup Form', 'mersal-notifications' ),
			'description' => __( 'Registration form with phone verification.', 'mersal-notifications' ),
			'categories'  => array( 'mersal' ),
			'content'     => '<!-- wp:shortcode -->[mersal_signup]<!-- /wp:shortcode -->',
		) );
		register_block_pattern( 'mersal/phone-bar', array(
			'title'       => __( 'Mersal Phone Verification Bar', 'mersal-notifications' ),
			'description' => __( 'Phone verification prompt bar.', 'mersal-notifications' ),
			'categories'  => array( 'mersal' ),
			'content'     => '<!-- wp:shortcode -->[mersal_phone_bar]<!-- /wp:shortcode -->',
		) );
	}

	/**
	 * Register Elementor widgets.
	 *
	 * @param \Elementor\Widgets_Manager $widgets_manager Widgets manager.
	 */
	public function register_elementor_widgets( $widgets_manager ) {
		if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
			return;
		}

		$shortcodes = array(
			'mersal_login'    => array(
				'shortcode' => '[mersal_otp_login]',
				'title'     => __( 'Mersal OTP Login', 'mersal-notifications' ),
			),
			'mersal_signup'   => array(
				'shortcode' => '[mersal_signup]',
				'title'     => __( 'Mersal Signup', 'mersal-notifications' ),
			),
			'mersal_phone_bar' => array(
				'shortcode' => '[mersal_phone_bar]',
				'title'     => __( 'Mersal Phone Bar', 'mersal-notifications' ),
			),
		);

		foreach ( $shortcodes as $name => $config ) {
			$widgets_manager->register( new class( $name, $config ) extends \Elementor\Widget_Base {
				private $shortcode_config;
				private $widget_name;

				public function __construct( $name, $config, $args = array() ) {
					$this->shortcode_config = $config;
					$this->widget_name = $name;
					parent::__construct( $args );
				}

				public function get_name() { return $this->widget_name; }
				public function get_title() { return $this->shortcode_config['title']; }
				public function get_icon() { return 'eicon-form'; }
				public function get_categories() { return array( 'general' ); }
				protected function render() { echo do_shortcode( $this->shortcode_config['shortcode'] ); }
			} );
		}
	}

	/**
	 * Register Bricks elements.
	 *
	 * @param array $elements Existing elements.
	 */
	public function register_bricks_elements( $elements ) {
		if ( ! class_exists( '\Bricks\Element' ) ) {
			return;
		}

		$shortcodes = array(
			'mersal_login'    => '[mersal_otp_login]',
			'mersal_signup'   => '[mersal_signup]',
			'mersal_phone_bar' => '[mersal_phone_bar]',
		);

		foreach ( $shortcodes as $name => $shortcode ) {
			$element = new class( $name, $shortcode ) extends \Bricks\Element {
				private $sc;

				public function __construct( $name, $shortcode ) {
					$this->sc = $shortcode;
					$this->name = $name;
					parent::__construct();
				}

				public function get_name() { return $this->name; }
				public function get_label() { return $this->name; }
				public function get_category() { return 'basic'; }
				public function get_icon() { return 'icon-unfold'; }
				public function render( $render = null ) { echo do_shortcode( $this->sc ); }
			};
			$elements[ $name ] = $element;
		}
	}
}
