<?php
/**
 * Block Registrar — auto-discovers and registers all Gutenberg blocks.
 *
 * @package GGM Commerce
 */

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

class GGMC_Block_Registrar {

	/**
	 * Boot the block system.
	 */
	public static function init() {
		// If init has already fired (called from within init hook), register immediately
		if ( did_action( 'init' ) ) {
			self::register_blocks();
		} else {
			add_action( 'init', array( __CLASS__, 'register_blocks' ) );
		}
		add_action( 'block_categories_all', array( __CLASS__, 'register_block_category' ), 10, 2 );
		add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_editor_assets' ) );
	}

	/**
	 * Auto-discover block.json files and register each block.
	 */
	public static function register_blocks() {
		$plugin_dir = dirname( __FILE__, 2 );

		// Load shared data handlers that widget render files depend on
		$data_handlers = array(
			'widgets/data/class-product-data-handler.php',
			'widgets/data/class-product-gallery-data-handler.php',
			'widgets/data/class-cart-display-data-handler.php',
			'widgets/data/class-customer-account-data-handler.php',
		);
		foreach ( $data_handlers as $handler ) {
			$path = $plugin_dir . '/' . $handler;
			if ( file_exists( $path ) ) {
				require_once $path;
			}
		}

		$blocks_dir = dirname( __FILE__ );

		// Discover blocks in tags/ and premium/ sub-directories.
		$block_json_files = array_merge(
			glob( $blocks_dir . '/tags/*/block.json' ) ?: [],
			glob( $blocks_dir . '/premium/*/block.json' ) ?: []
		);

		foreach ( $block_json_files as $block_json ) {
			$block_dir = dirname( $block_json );
			$render_file = $block_dir . '/render.php';

			// Include render.php to define the callback function
			if ( file_exists( $render_file ) ) {
				require_once $render_file;
			}

			// Derive callback function name from directory: product-grid → render_ggmc_product_grid_block
			$slug = basename( $block_dir );
			$callback_name = 'render_ggmc_' . str_replace( '-', '_', $slug ) . '_block';

			$args = array();
			if ( function_exists( $callback_name ) ) {
				$args['render_callback'] = $callback_name;
			}

			register_block_type( $block_dir, $args );
		}
	}

	/**
	 * Register a "Goose Commerce" block category so blocks are grouped.
	 *
	 * @param array                    $categories Existing categories.
	 * @param \WP_Block_Editor_Context $context    Editor context.
	 * @return array
	 */
	public static function register_block_category( $categories, $context ) {
		return array_merge(
			array(
				array(
					'slug'  => 'goose-commerce',
					'title' => __( 'Goose Commerce', 'ggm-commerce' ),
					'icon'  => 'cart',
				),
			),
			$categories
		);
	}

	/**
	 * Enqueue shared editor assets (global scripts/styles for all blocks).
	 */
	public static function enqueue_editor_assets() {
		$build_dir  = dirname( __FILE__, 2 ) . '/build/';
		$build_url  = plugin_dir_url( dirname( __FILE__ ) ) . 'build/';

		// Shared editor stylesheet (optional — create if needed).
		$editor_css = $build_dir . 'editor.css';
		if ( file_exists( $editor_css ) ) {
			wp_enqueue_style(
				'ggmc-blocks-editor',
				$build_url . 'editor.css',
				array(),
				filemtime( $editor_css )
			);
		}

		// Enqueue the plugin's frontend CSS so blocks preview correctly in the editor.
		$widgets_css = dirname( __FILE__, 2 ) . '/assets/css/widgets.css';
		if ( file_exists( $widgets_css ) ) {
			wp_enqueue_style(
				'ggmc-widgets',
				plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/widgets.css',
				array(),
				filemtime( $widgets_css )
			);
		}

		$defaults_css = dirname( __FILE__, 2 ) . '/assets/css/professional-defaults.css';
		if ( file_exists( $defaults_css ) && ! get_option( 'ggm_commerce_defaults_cleared', false ) ) {
			wp_enqueue_style(
				'ggmc-professional-defaults',
				plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/professional-defaults.css',
				array(),
				filemtime( $defaults_css )
			);
		}
	}
}
