<?php
/**
 * Gutenberg blocks for SkyNet Smart Favorites.
 *
 * @package SKYNSMFA_Smart_Favorites
 */

declare( strict_types=1 );

namespace SKYNSMFA\Wishlist;

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

final class Blocks {
	/**
	 * Singleton instance.
	 *
	 * @var Blocks|null
	 */
	private static $instance = null;

	/**
	 * Editor script handle.
	 *
	 * @var string
	 */
	private const EDITOR_SCRIPT_HANDLE = 'skynsmfa-wishlist-menu-icon-editor';

	/**
	 * Editor style handle.
	 *
	 * @var string
	 */
	private const EDITOR_STYLE_HANDLE = 'skynsmfa-wishlist-menu-icon-editor';

	/**
	 * Get singleton instance.
	 *
	 * @return Blocks
	 */
	public static function instance(): Blocks {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {
		add_action( 'init', array( $this, 'register_blocks' ), 20 );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
	}

	/**
	 * Register dynamic blocks (server render + attribute schema).
	 *
	 * @return void
	 */
	public function register_blocks(): void {
		if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
			return;
		}

		$block_dir   = SKYNSMFA_PATH . 'blocks/wishlist-menu-icon';
		$script_path = $block_dir . '/index.js';
		$style_path  = $block_dir . '/editor.css';

		if ( ! file_exists( $block_dir . '/block.json' ) || ! file_exists( $script_path ) ) {
			return;
		}

		wp_register_script(
			self::EDITOR_SCRIPT_HANDLE,
			SKYNSMFA_URL . 'blocks/wishlist-menu-icon/index.js',
			array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-dom', 'wp-media-utils' ),
			$this->get_asset_version( $script_path ),
			true
		);

		wp_set_script_translations(
			self::EDITOR_SCRIPT_HANDLE,
			'skynet-smart-favorites',
			SKYNSMFA_PATH . 'languages'
		);

		$frontend_style_path = SKYNSMFA_PATH . 'assets/css/skynsmfa-wishlist.css';
		if ( file_exists( $frontend_style_path ) ) {
			wp_register_style(
				'skynsmfa-wishlist',
				SKYNSMFA_URL . 'assets/css/skynsmfa-wishlist.css',
				array(),
				$this->get_asset_version( $frontend_style_path )
			);
		}

		if ( file_exists( $style_path ) ) {
			wp_register_style(
				self::EDITOR_STYLE_HANDLE,
				SKYNSMFA_URL . 'blocks/wishlist-menu-icon/editor.css',
				array( 'skynsmfa-wishlist' ),
				$this->get_asset_version( $style_path )
			);
		}

		register_block_type_from_metadata(
			$block_dir,
			array(
				'render_callback' => array( $this, 'render_wishlist_menu_icon' ),
				'editor_script'   => self::EDITOR_SCRIPT_HANDLE,
				'editor_style'    => self::EDITOR_STYLE_HANDLE,
			)
		);
	}

	/**
	 * Enqueue block editor assets.
	 *
	 * @return void
	 */
	public function enqueue_block_editor_assets(): void {
		wp_enqueue_media();

		if ( wp_style_is( 'skynsmfa-wishlist', 'registered' ) ) {
			wp_enqueue_style( 'skynsmfa-wishlist' );
		}
	}

	/**
	 * Render the favorites menu icon block.
	 *
	 * @param array<string, mixed> $attributes Block attributes.
	 * @return string
	 */
	public function render_wishlist_menu_icon( array $attributes = array() ): string {
		return Frontend::instance()->render_menu_icon( $attributes );
	}

	/**
	 * Cache-busting version for block editor assets.
	 *
	 * @param string $file_path Absolute file path.
	 * @return string
	 */
	private function get_asset_version( string $file_path ): string {
		$mtime = filemtime( $file_path );
		if ( false === $mtime ) {
			return SKYNSMFA_VERSION;
		}

		return (string) $mtime;
	}
}
