<?php
namespace Vidplex;

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

/**
 * Class Blocks
 *
 * Manages the registration and rendering of custom Gutenberg blocks
 * for the plugin.
 *
 * Handles block assets, server-side rendering, and integration
 * with the WordPress block editor.
 */
class Blocks {
	/**
	 * The single instance of the Blocks class.
	 *
	 * @var Blocks|null
	 */
	private static $instance = null;

	/**
	 * Directory path for the blocks folder.
	 *
	 * @var string
	 */
	private static $dir = VIDPLEX_PLUGIN_DIR . 'blocks/';

	/**
	 * URL for the blocks folder.
	 *
	 * @var string
	 */
	private static $url = VIDPLEX_PLUGIN_URL . 'blocks/';

	/**
	 * Get the single instance of the class.
	 *
	 * @return Blocks
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 *
	 * Hooks into init and block category filters.
	 */
	private function __construct() {
		add_action( 'init', array( $this, 'enqueue_vidplex_block' ) );
		add_filter( 'block_categories_all', array( $this, 'add_block_category' ), 10, 2 );
	}

	/**
	 * Enqueue scripts/styles and register the base block.
	 */
	public function enqueue_vidplex_block() {
		wp_enqueue_script(
			'vidplex-script',
			VIDPLEX_PLUGIN_URL . 'blocks/index.js',
			array( 'wp-blocks', 'wp-element', 'wp-editor' ),
			VIDPLEX_VERSION,
			false
		);

		wp_localize_script(
			'vidplex-script',
			'vidplexData',
			array(
				'imageDir'    => VIDPLEX_PLUGIN_URL . 'assets/img/',
				'isPremium'   => vidplex_premium_access(),
				'googleFonts' => vidplex_google_fonts(),
				'channels'    => \Vidplex\Store::get_instance()->channels->get_all(),
				'upsellURL'   => vidplex_upsell_url(),
			)
		);
		$manifest_file = self::$dir . 'blocks-manifest.php';

		if ( ! file_exists( $manifest_file ) ) {
			return;
		}

		$blocks = require_once $manifest_file;
		if ( ! is_array( $blocks ) || empty( $blocks ) ) {
			return;
		}

		foreach ( $blocks as $key => $block ) {
			$block_path = self::$dir . $key;

			// Frontend script registration/enqueue.
			$frontend_script_path = VIDPLEX_PLUGIN_DIR . 'frontend/' . $key . '.js';
			$frontend_script_url  = VIDPLEX_PLUGIN_URL . 'frontend/' . $key . '.js';
			$script_handle        = 'vidplex--' . $key . '--frontend-script';

			if ( file_exists( $frontend_script_path ) ) {
				$deps = array( 'jquery', 'vidplex-swiper-js', 'vidplex-youtube-iframe-js', 'vidplex-frontend-script' );

				wp_register_script( $script_handle, $frontend_script_url, $deps, VIDPLEX_VERSION, true );
			}

			register_block_type( $block_path );
			wp_register_script( "vidplex--{$key}", VIDPLEX_PLUGIN_URL . "frontend/{$key}.js", array( 'jquery' ), VIDPLEX_VERSION, false );
		}
	}

	/**
	 * Add the "Vidplex Blocks" category in the block editor inserter.
	 *
	 * @param array $categories Existing categories.
	 * @return array Updated categories.
	 */
	public function add_block_category( $categories ) {
		return array_merge(
			array(
				array(
					'slug'  => 'vidplex',
					'title' => __( 'Vidplex Blocks', 'vidplex' ),
				),
			),
			$categories
		);
	}
}
