<?php

namespace WeDevs\AppBridge\Admin;

use WeDevs\AppBridge\Core\Contracts\Hookable;

class Menu implements Hookable {

	public function register_hooks(): void {
		add_action( 'admin_menu', [ $this, 'register_menus' ] );
	}

	/**
	 * Register parent "App Bridge" menu and integration sub-menus.
	 */
	public function register_menus(): void {
		$capability = 'manage_options';
		$icon = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( plugin_dir_path( APP_BRIDGE_FILE ) . 'assets/images/appbride_icon.svg' ) ); // phpcs:ignore

		// Parent menu: App Bridge
		add_menu_page(
			__( 'App Bridge', 'app-bridge' ),
			__( 'App Bridge', 'app-bridge' ),
			$capability,
			'app-bridge',
			[ $this, 'render_pocket_store_page' ],
			$icon,
			56,
		);

		// Sub-menus declared by each active integration
		foreach ( app_bridge()->integrations() as $integration ) {
			foreach ( $integration->menus() as $menu ) {
				$mount_id = $menu['mount_id'];

				add_submenu_page(
					'app-bridge',
					$menu['label'],
					$menu['label'],
					$capability,
					$menu['slug'],
					static function () use ( $mount_id ) {
						printf( '<div class="app-bridge" id="%s"></div>', esc_attr( $mount_id ) );
					}
				);
			}
		}
	}

	public function render_pocket_store_page(): void {
		echo '<div class="app-bridge" id="app-bridge-app-bridge"></div>';
	}
}
