<?php
/**
 * Admin Menu Manager
 *
 * Manages admin menu pages and React app mounting.
 *
 * @package DonorCore\Admin
 */

namespace DonorCore\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Menu Manager Class
 */
class Menu_Manager {

	/**
	 * DI Container.
	 *
	 * @var \DonorCore\Utils\Container
	 */
	private $container;

	/**
	 * Constructor.
	 *
	 * @param \DonorCore\Utils\Container $container DI container.
	 */
	public function __construct( $container ) {
		$this->container = $container;
	}

	/**
	 * Initialize admin functionality.
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
	}

	/**
	 * Register admin menu pages.
	 */
	public function register_menu() {
		// Main menu page
		add_menu_page(
			__( 'DonorCore', 'donorcore-donations' ),
			__( 'DonorCore', 'donorcore-donations' ),
			'manage_options',
			'donorcore-donations',
			array( $this, 'render_admin_app' ),
			'dashicons-heart',
			30
		);

		// Dashboard submenu (rename first submenu)
		add_submenu_page(
			'donorcore-donations',
			__( 'Dashboard', 'donorcore-donations' ),
			__( 'Dashboard', 'donorcore-donations' ),
			'manage_options',
			'donorcore-donations', // Same as parent for dashboard
			array( $this, 'render_admin_app' )
		);

		// Donors submenu
		add_submenu_page(
			'donorcore-donations',
			__( 'Donors', 'donorcore-donations' ),
			__( 'Donors', 'donorcore-donations' ),
			'manage_options',
			'donorcore-donations#/donors',
			array( $this, 'render_admin_app' )
		);

		// Getting Started submenu
		add_submenu_page(
			'donorcore-donations',
			__( 'Getting Started', 'donorcore-donations' ),
			__( 'Getting Started', 'donorcore-donations' ),
			'manage_options',
			'donorcore-donations#/getting-started',
			array( $this, 'render_admin_app' )
		);

		// Settings submenu
		add_submenu_page(
			'donorcore-donations',
			__( 'Settings', 'donorcore-donations' ),
			__( 'Settings', 'donorcore-donations' ),
			'manage_options',
			'donorcore-donations#/settings',
			array( $this, 'render_admin_app' )
		);
	}

	/**
	 * Render the React admin app container.
	 */
	public function render_admin_app() {
		?>

		<div id="donor-core-admin-root" class="wrap"></div>
		<?php
	}

	/**
	 * Enqueue admin scripts and styles.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_scripts( $hook ) {
		// Only load on DonorCore pages.
		if ( strpos( $hook, 'donorcore-donations' ) === false ) {
			return;
		}

		// Get asset file for dependency management.
		$asset_file = DONOR_CORE_PLUGIN_DIR . 'assets/php/admin-app.asset.php';
		
		if ( file_exists( $asset_file ) ) {
			$asset = include $asset_file;

			wp_enqueue_script(
				'donor-core-admin',
				DONOR_CORE_PLUGIN_URL . 'assets/js/admin-app.js',
				$asset['dependencies'],
				$asset['version'],
				true
			);

			// Enable JavaScript translations for React admin dashboard
			wp_set_script_translations(
				'donor-core-admin',
				'donorcore-donations',
				DONOR_CORE_PLUGIN_DIR . 'languages'
			);

			// Enqueue admin styles
			wp_enqueue_style(
				'donor-core-admin-style',
				DONOR_CORE_PLUGIN_URL . 'assets/css/admin-app.css',
				array(),
				$asset['version']
			);

			// Localize WooCommerce currency settings and default suggested amounts for admin app
			$global_suggested = get_option( 'donorcore_suggested_amounts', '50,100,250,500' );
			$default_suggested = array_map( 'floatval', array_filter( array_map( 'trim', explode( ',', $global_suggested ) ) ) );
			if ( empty( $default_suggested ) ) {
				$default_suggested = array( 50, 100, 250, 500 );
			}

			wp_localize_script(
				'donor-core-admin',
				'donorCoreSettings',
				array(
					'currency'                => sanitize_text_field( get_woocommerce_currency() ),
					'currencySymbol'          => sanitize_text_field( get_woocommerce_currency_symbol() ),
					'locale'                  => sanitize_text_field( get_locale() ),
					'defaultSuggestedAmounts' => array_values( $default_suggested ),
				)
			);

			// Localize script with API settings.
			wp_localize_script(
				'donor-core-admin',
				'donorCoreAdmin',
				array(
					'apiUrl'     => esc_url_raw( rest_url() ),
					'nonce'      => wp_create_nonce( 'wp_rest' ),
					'pluginUrl'  => esc_url_raw( DONOR_CORE_PLUGIN_URL ),
					'monacoPath' => esc_url_raw( DONOR_CORE_PLUGIN_URL . 'assets/vendor/monaco-editor/min/vs' ),
				)
			);
			
			// Set up REST API preloading for better performance
			wp_add_inline_script(
				'wp-api',
				sprintf(
					'wp.api.loadPromise.done( function() { wp.api.init( { versionString: "wp/v2/", nonce: "%s" } ); } );',
					esc_js( wp_create_nonce( 'wp_rest' ) )
				),
				'after'
			);
		}
	}
}
