<?php
/**
 * Block registration + conditional asset enqueue for BW Agenda Table.
 */

defined( 'ABSPATH' ) || exit;

class BW_Agenda_Table_Block {

	const BLOCK_NAME = 'bw/agenda-table';

	public static function register() {
		add_action( 'init', array( __CLASS__, 'register_block' ) );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_material_symbols' ) );
		add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_material_symbols_editor' ) );
	}

	public static function register_block() {
		$manifest = BW_AGENDA_TABLE_DIR . 'blocks/bw-agenda-table';
		if ( file_exists( $manifest . '/block.json' ) ) {
			register_block_type( $manifest );
		}
	}

	/**
	 * Material Symbols (Google) — used by the block for inline icons
	 * (calendar, clock, apartment, mic, slides). Loaded only when the
	 * block is on the current page so we don't add a request to every
	 * frontend view.
	 */
	public static function enqueue_material_symbols() {
		if ( is_admin() || ! has_block( self::BLOCK_NAME ) ) {
			return;
		}
		wp_enqueue_style(
			'bw-agenda-table-material-symbols',
			'https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..24,400,0,0&display=swap',
			array(),
			null
		);
	}

	/**
	 * Same font in the editor preview. We don't gate on has_block here:
	 * the editor lazily inserts the block, and the icons need to render
	 * the moment the user adds it.
	 */
	public static function enqueue_material_symbols_editor() {
		wp_enqueue_style(
			'bw-agenda-table-material-symbols',
			'https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..24,400,0,0&display=swap',
			array(),
			null
		);
	}
}
