<?php
/**
 * Menu Builder page template
 *
 * Variables provided by Agency_Pulse_Admin_Menu_Builder::render_builder_page():
 *
 *   $all_configs        array  All named configurations keyed by ID.
 *   $active_config_id   string Currently active configuration ID.
 *   $active_config_name string Display name of the active configuration.
 *   $merged_items       array  Merged snapshot+config list ready for rendering.
 *                              Each item: slug, title, original_title, icon, hidden,
 *                              separator, custom_container, children[].
 *                              Each child: slug, title, hidden.
 *   $all_roles          array  All registered WP roles: slug => display name.
 *   $assignments        array  Current map of role_slug => config_id.
 *
 * @package Agency_Pulse_Admin_Menu
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap aamo-wrap">

	<h1 class="aamo-page-title">
		<?php esc_html_e( 'Admin Menu Organizer', 'agency-pulse-admin-menu' ); ?>
	</h1>

	<div class="aamo-tabs-card">
	<nav class="nav-tab-wrapper">
		<a href="#tab-builder" class="nav-tab nav-tab-active" id="aamo-tab-builder">
			<?php esc_html_e( 'Menu Builder', 'agency-pulse-admin-menu' ); ?>
		</a>
		<a href="#tab-roles" class="nav-tab" id="aamo-tab-roles">
			<?php esc_html_e( 'Role Assignments', 'agency-pulse-admin-menu' ); ?>
		</a>
		<a href="#tab-role-manager" class="nav-tab" id="aamo-tab-role-manager">
			<?php esc_html_e( 'Role Manager', 'agency-pulse-admin-menu' ); ?>
		</a>
	</nav>

	<div class="aamo-tab-content">

	<?php // ── Tab 1: Menu Builder ──────────────────────────────────────────── ?>
	<div id="aamo-panel-builder" class="aamo-tab-panel is-active">

	<?php if ( empty( $all_configs ) ) : ?>

		<p><?php esc_html_e( 'No configurations found. Save a new configuration to get started.', 'agency-pulse-admin-menu' ); ?></p>

	<?php else : ?>

	<?php
	// ── Config selector toolbar ──────────────────────────────────────────────
	?>
	<div class="aamo-config-toolbar">

		<label class="aamo-config-label" for="aamo-config-select">
			<?php esc_html_e( 'Config:', 'agency-pulse-admin-menu' ); ?>
		</label>

		<select id="aamo-config-select" class="aamo-config-select">
			<?php foreach ( $all_configs as $cfg_id => $cfg ) : ?>
			<option value="<?php echo esc_attr( $cfg_id ); ?>"
				<?php selected( $cfg_id, $active_config_id ); ?>>
				<?php echo esc_html( $cfg['name'] ); ?>
			</option>
			<?php endforeach; ?>
		</select>

		<button type="button" id="aamo-new-config" class="button">
			<?php esc_html_e( '+ New', 'agency-pulse-admin-menu' ); ?>
		</button>
		<button type="button" id="aamo-rename-config" class="button"
			data-current-name="<?php echo esc_attr( $active_config_name ); ?>">
			<?php esc_html_e( 'Rename', 'agency-pulse-admin-menu' ); ?>
		</button>
		<button type="button" id="aamo-duplicate-config" class="button"
			data-current-name="<?php echo esc_attr( $active_config_name ); ?>">
			<?php esc_html_e( 'Duplicate', 'agency-pulse-admin-menu' ); ?>
		</button>
		<button type="button" id="aamo-delete-config" class="button aamo-btn-danger"
			<?php echo count( $all_configs ) <= 1 ? 'disabled' : ''; ?>>
			<?php esc_html_e( 'Delete', 'agency-pulse-admin-menu' ); ?>
		</button>

	</div><!-- /.aamo-config-toolbar -->

	<?php
	// ── Save/action toolbar ──────────────────────────────────────────────────
	?>
	<div class="aamo-page-toolbar">

		<button type="button" id="aamo-save" class="button button-primary">
			<?php esc_html_e( 'Save Menu', 'agency-pulse-admin-menu' ); ?>
		</button>
		<button type="button" id="aamo-reset" class="button">
			<?php esc_html_e( 'Reset to Default', 'agency-pulse-admin-menu' ); ?>
		</button>
		<button type="button" id="aamo-add-container" class="button">
			<?php esc_html_e( '+ Add Container', 'agency-pulse-admin-menu' ); ?>
		</button>

		<span id="aamo-status" class="aamo-status" aria-live="polite"></span>

	</div><!-- /.aamo-page-toolbar -->

	<?php
	// ── Menu item list ───────────────────────────────────────────────────────
	?>
	<ul id="aamo-sortable" class="aamo-sortable">

		<?php foreach ( $merged_items as $item ) :

			$item_classes = array( 'aamo-menu-item' );
			if ( $item['hidden'] )           { $item_classes[] = 'aamo-item-hidden'; }
			if ( $item['separator'] )        { $item_classes[] = 'aamo-separator'; }
			if ( $item['custom_container'] ) { $item_classes[] = 'aamo-custom-container'; }
			$is_locked = ( $item['slug'] === Agency_Pulse_Admin_Menu_Constants::PAGE_MENU_BUILDER );
			if ( $is_locked )                { $item_classes[] = 'aamo-item-locked'; }

		?>
		<li class="<?php echo esc_attr( implode( ' ', $item_classes ) ); ?>"
			data-slug="<?php echo esc_attr( $item['slug'] ); ?>"
			data-hidden="<?php echo $item['hidden'] ? 'true' : 'false'; ?>"
			data-custom-container="<?php echo ! empty( $item['custom_container'] ) ? 'true' : 'false'; ?>"
			data-icon="<?php echo esc_attr( $item['icon'] ); ?>"
			data-original-icon="<?php echo esc_attr( $item['original_icon'] ); ?>"
			<?php if ( ! empty( $item['original_title'] ) ) : ?>
			data-original-title="<?php echo esc_attr( $item['original_title'] ); ?>"
			<?php endif; ?>
			<?php if ( $item['custom_container'] || ( ! empty( $item['original_title'] ) && $item['title'] !== $item['original_title'] ) ) : ?>
			data-custom-label="<?php echo esc_attr( $item['title'] ); ?>"
			<?php endif; ?>>

			<?php // ── Drag handle ──────────────────────────────────── ?>
			<span class="aamo-handle dashicons dashicons-menu" title="<?php esc_attr_e( 'Drag to reorder', 'agency-pulse-admin-menu' ); ?>"></span>

			<?php if ( ! $item['separator'] ) : ?>

				<?php // ── Expand arrow (custom containers always; others only when sub-items exist) ─── ?>
				<?php if ( ! empty( $item['children'] ) || $item['custom_container'] ) : ?>
				<button type="button" class="aamo-expand-btn" aria-expanded="false"
					aria-label="<?php esc_attr_e( 'Toggle sub-menu', 'agency-pulse-admin-menu' ); ?>">
					<span class="dashicons dashicons-arrow-right-alt2"></span>
				</button>
				<?php else : ?>
				<span class="aamo-expand-placeholder"></span>
				<?php endif; ?>

				<?php // ── Icon + original-icon restore indicator ───────── ?>
				<?php
				$icon          = $item['icon'];
				$original_icon = $item['original_icon'];
				if ( 0 === strpos( $icon, 'dashicons-' ) ) :
				?>
				<span class="aamo-item-icon dashicons <?php echo esc_attr( $icon ); ?>"
					title="<?php esc_attr_e( 'dbl click to change icon', 'agency-pulse-admin-menu' ); ?>"></span>
				<?php elseif ( ! empty( $icon ) && 'none' !== $icon ) : ?>
				<span class="aamo-item-icon" style="background-image:url(<?php echo esc_attr( $icon ); ?>)"
					title="<?php esc_attr_e( 'dbl click to change icon', 'agency-pulse-admin-menu' ); ?>"></span>
				<?php else : ?>
				<span class="aamo-item-icon"
					title="<?php esc_attr_e( 'dbl click to change icon', 'agency-pulse-admin-menu' ); ?>"></span>
				<?php endif; ?>
				<?php if ( ! $item['custom_container'] && ! empty( $original_icon ) && $icon !== $original_icon ) :
					if ( 0 === strpos( $original_icon, 'dashicons-' ) ) : ?>
				<span class="aamo-original-icon dashicons <?php echo esc_attr( $original_icon ); ?>"
					title="<?php esc_attr_e( 'click to restore', 'agency-pulse-admin-menu' ); ?>"></span>
				<?php  else : ?>
				<span class="aamo-original-icon"
					style="background-image:url(<?php echo esc_attr( $original_icon ); ?>)"
					title="<?php esc_attr_e( 'click to restore', 'agency-pulse-admin-menu' ); ?>"></span>
				<?php  endif; ?>
				<?php endif; ?>

				<?php // ── Custom container badge ──────────────────────── ?>
				<?php if ( $item['custom_container'] ) : ?>
				<span class="aamo-container-badge"><?php esc_html_e( 'custom', 'agency-pulse-admin-menu' ); ?></span>
				<?php endif; ?>

				<?php // ── Edit pencil (left of label) + Title ──────── ?>
				<button type="button" class="aamo-label-edit-btn"
					title="<?php esc_attr_e( 'Rename label', 'agency-pulse-admin-menu' ); ?>"
					aria-label="<?php esc_attr_e( 'Rename label', 'agency-pulse-admin-menu' ); ?>">
					<span class="dashicons dashicons-edit"></span>
				</button>
				<span class="aamo-title-group">
					<span class="aamo-item-title"><?php echo esc_html( $item['title'] ); ?></span>
					<?php if ( ! empty( $item['original_title'] ) && $item['title'] !== $item['original_title'] ) : ?>
					<span class="aamo-original-title"
						title="<?php esc_attr_e( 'click to restore', 'agency-pulse-admin-menu' ); ?>"><?php echo esc_html( $item['original_title'] ); ?></span>
					<?php endif; ?>
				</span>

				<?php // ── Slug badge ─────────────────────────────────── ?>
				<span class="aamo-item-slug"><?php echo esc_html( $item['slug'] ); ?></span>

				<?php // ── Visibility toggle ──────────────────────────── ?>
				<button type="button" class="aamo-visibility-btn"
					title="<?php esc_attr_e( 'Toggle visibility', 'agency-pulse-admin-menu' ); ?>"
					aria-label="<?php $item['hidden'] ? esc_attr_e( 'Show item', 'agency-pulse-admin-menu' ) : esc_attr_e( 'Hide item', 'agency-pulse-admin-menu' ); ?>">
					<span class="dashicons <?php echo $item['hidden'] ? 'dashicons-hidden' : 'dashicons-visibility'; ?>"></span>
				</button>

				<?php // ── Remove container button ────────────────────── ?>
				<?php if ( $item['custom_container'] ) : ?>
				<button type="button" class="aamo-remove-container-btn"
					title="<?php esc_attr_e( 'Remove container', 'agency-pulse-admin-menu' ); ?>"
					aria-label="<?php esc_attr_e( 'Remove container', 'agency-pulse-admin-menu' ); ?>">
					<span class="dashicons dashicons-dismiss"></span>
				</button>
				<?php endif; ?>

			<?php else : ?>

				<?php // ── Separator row ─────────────────────────────── ?>
				<span class="aamo-separator-line"></span>
				<span class="aamo-item-slug"><?php esc_html_e( '— separator —', 'agency-pulse-admin-menu' ); ?></span>

			<?php endif; ?>

			<?php // ── Sub-item list ──────────────────────────────────── ?>
			<?php if ( ! empty( $item['children'] ) || $item['custom_container'] ) : ?>
			<ul class="aamo-submenu-list">
				<?php foreach ( $item['children'] as $child ) :
					$child_classes = array( 'aamo-submenu-item' );
					if ( $child['hidden'] ) { $child_classes[] = 'aamo-item-hidden'; }
					if ( $is_locked )       { $child_classes[] = 'aamo-subitem-locked'; }
				?>
				<li class="<?php echo esc_attr( implode( ' ', $child_classes ) ); ?>"
					data-slug="<?php echo esc_attr( $child['slug'] ); ?>"
					data-hidden="<?php echo $child['hidden'] ? 'true' : 'false'; ?>"
					data-original-title="<?php echo esc_attr( $child['original_title'] ); ?>"
					<?php if ( $child['title'] !== $child['original_title'] ) : ?>
					data-custom-label="<?php echo esc_attr( $child['title'] ); ?>"
					<?php endif; ?>
					<?php if ( ! empty( $child['original_parent'] ) ) : ?>
					data-original-parent="<?php echo esc_attr( $child['original_parent'] ); ?>"
					<?php endif; ?>>

					<span class="aamo-handle dashicons dashicons-menu" title="<?php esc_attr_e( 'Drag to reorder', 'agency-pulse-admin-menu' ); ?>"></span>

					<button type="button" class="aamo-label-edit-btn"
						title="<?php esc_attr_e( 'Rename label', 'agency-pulse-admin-menu' ); ?>"
						aria-label="<?php esc_attr_e( 'Rename label', 'agency-pulse-admin-menu' ); ?>">
						<span class="dashicons dashicons-edit"></span>
					</button>
					<span class="aamo-title-group">
						<span class="aamo-item-title"><?php echo esc_html( $child['title'] ); ?></span>
						<?php if ( $child['title'] !== $child['original_title'] ) : ?>
						<span class="aamo-original-title"
							title="<?php esc_attr_e( 'click to restore', 'agency-pulse-admin-menu' ); ?>"><?php echo esc_html( $child['original_title'] ); ?></span>
						<?php endif; ?>
					</span>

					<span class="aamo-item-slug"><?php echo esc_html( $child['slug'] ); ?></span>

					<button type="button" class="aamo-visibility-btn"
						title="<?php esc_attr_e( 'Toggle visibility', 'agency-pulse-admin-menu' ); ?>"
						aria-label="<?php $child['hidden'] ? esc_attr_e( 'Show item', 'agency-pulse-admin-menu' ) : esc_attr_e( 'Hide item', 'agency-pulse-admin-menu' ); ?>">
						<span class="dashicons <?php echo $child['hidden'] ? 'dashicons-hidden' : 'dashicons-visibility'; ?>"></span>
					</button>

				</li>
				<?php endforeach; ?>
			</ul>
			<?php endif; ?>

		</li>
		<?php endforeach; ?>

	</ul><!-- /#aamo-sortable -->

	<?php endif; ?>

	</div><!-- /#aamo-panel-builder -->

	<?php // ── Tab 2: Role Assignments ──────────────────────────────────────── ?>
	<div id="aamo-panel-roles" class="aamo-tab-panel">

		<p class="description" style="margin-top:16px">
			<?php esc_html_e(
				'Assign each WordPress role to a named menu configuration. Users whose role is not assigned here see the unmodified default WordPress admin menu.',
				'agency-pulse-admin-menu'
			); ?>
		</p>

		<?php if ( empty( $all_configs ) ) : ?>

			<p><?php esc_html_e( 'No configurations found. Create a configuration in the Menu Builder first.', 'agency-pulse-admin-menu' ); ?></p>

		<?php else : ?>

		<div id="aamo-role-assignments">

			<?php foreach ( $all_configs as $cfg_id => $cfg ) : ?>

			<div class="aamo-config-card" data-config-id="<?php echo esc_attr( $cfg_id ); ?>">

				<h2><?php echo esc_html( $cfg['name'] ); ?></h2>

				<?php if ( empty( $all_roles ) ) : ?>
					<p><?php esc_html_e( 'No roles found.', 'agency-pulse-admin-menu' ); ?></p>
				<?php else : ?>

				<div class="aamo-role-checkboxes">
					<?php foreach ( $all_roles as $role_slug => $role_name ) :
						$is_checked = isset( $assignments[ $role_slug ] ) && $assignments[ $role_slug ] === $cfg_id;
					?>
					<label class="aamo-role-label">
						<input
							type="checkbox"
							class="aamo-role-checkbox"
							data-role="<?php echo esc_attr( $role_slug ); ?>"
							data-config="<?php echo esc_attr( $cfg_id ); ?>"
							<?php checked( $is_checked ); ?>
						>
						<?php echo esc_html( translate_user_role( $role_name ) ); ?>
					</label>
					<?php endforeach; ?>
				</div>

				<?php endif; ?>

			</div><!-- /.aamo-config-card -->

			<?php endforeach; ?>

		</div><!-- /#aamo-role-assignments -->

		<p class="aamo-assignments-actions">
			<button type="button" id="aamo-save-assignments" class="button button-primary">
				<?php esc_html_e( 'Save Assignments', 'agency-pulse-admin-menu' ); ?>
			</button>
			<span id="aamo-assignments-status" class="aamo-status" aria-live="polite"></span>
		</p>

		<?php endif; ?>

	</div><!-- /#aamo-panel-roles -->

	<?php // ── Tab 3: Role Manager ──────────────────────────────────────── ?>
	<div id="aamo-panel-role-manager" class="aamo-tab-panel">

		<?php
		$_rm_licensed = Agency_Pulse_Admin_Menu_License_Manager::is_active();
		?>

		<?php if ( ! $_rm_licensed ) : ?>
		<div class="notice notice-warning inline" style="margin-top:16px">
			<p><?php printf(
				/* translators: %s: link to license settings */
				esc_html__( 'Role Manager requires an active license. %s', 'agency-pulse-admin-menu' ),
				'<a href="' . esc_url( admin_url( 'admin.php?page=aamo_settings#aamo-license-wrap' ) ) . '">' . esc_html__( 'Activate your license &rarr;', 'agency-pulse-admin-menu' ) . '</a>'
			); ?></p>
		</div>
		<?php endif; ?>

		<?php
		$role_manager_access = Agency_Pulse_Admin_Menu_Role_Manager::get_instance()->get_plugin_access_list();
		?>
		<div<?php echo $_rm_licensed ? '' : ' class="aamo-panel-locked"'; ?>>
		<?php include AGENCY_PULSE_ADMIN_MENU_DIR . 'templates/admin-role-manager.php'; ?>
		</div>

	</div><!-- /#aamo-panel-role-manager -->

	</div><!-- /.aamo-tab-content -->

	</div><!-- /.aamo-tabs-card -->

</div><!-- /.wrap -->

<?php // ── Icon picker modal (one instance, populated by JS) ─────────────── ?>
<div id="aamo-icon-picker" class="aamo-icon-picker" role="dialog"
	aria-label="<?php esc_attr_e( 'Choose Icon', 'agency-pulse-admin-menu' ); ?>"
	aria-modal="true">
	<div class="aamo-icon-picker-header">
		<span class="aamo-icon-picker-title">
			<?php esc_html_e( 'Choose Icon', 'agency-pulse-admin-menu' ); ?>
		</span>
		<button type="button" class="aamo-icon-picker-close"
			aria-label="<?php esc_attr_e( 'Close', 'agency-pulse-admin-menu' ); ?>">&times;</button>
	</div>
	<div class="aamo-icon-picker-search-wrap">
		<span class="dashicons dashicons-search aamo-icon-picker-search-icon"></span>
		<input type="text" id="aamo-icon-search"
			placeholder="<?php esc_attr_e( 'Filter icons…', 'agency-pulse-admin-menu' ); ?>"
			autocomplete="off">
	</div>
	<div class="aamo-icon-picker-grid" id="aamo-icon-grid">
		<?php // Populated by admin.js initIconPicker() ?>
	</div>
</div>

<?php // ── Clone / New Role modal (Phase I / J) ────────────────────────────── ?>
<div id="aamo-clone-modal" class="aamo-modal-overlay" hidden>
	<div class="aamo-modal-dialog" role="dialog" aria-modal="true" aria-labelledby="aamo-clone-modal-title">
		<div class="aamo-modal-header">
			<h2 id="aamo-clone-modal-title">
				<?php esc_html_e( 'Clone Role', 'agency-pulse-admin-menu' ); ?>
			</h2>
			<button type="button" class="aamo-modal-close"
				aria-label="<?php esc_attr_e( 'Close', 'agency-pulse-admin-menu' ); ?>">&times;</button>
		</div>
		<div class="aamo-modal-body">
			<p id="aamo-clone-source-desc" class="description"></p>
			<table class="form-table" role="presentation">
				<tr>
					<th scope="row">
						<label for="aamo-clone-name">
							<?php esc_html_e( 'New Role Name', 'agency-pulse-admin-menu' ); ?>
						</label>
					</th>
					<td>
						<input type="text" id="aamo-clone-name" class="regular-text" autocomplete="off">
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="aamo-clone-slug">
							<?php esc_html_e( 'Role Slug', 'agency-pulse-admin-menu' ); ?>
						</label>
					</th>
					<td>
						<input type="text" id="aamo-clone-slug" class="regular-text" autocomplete="off">
						<p class="description">
							<?php esc_html_e( 'Lowercase letters, numbers, and underscores only. Cannot be changed after creation.', 'agency-pulse-admin-menu' ); ?>
						</p>
						<p id="aamo-clone-slug-error" class="aamo-field-error"></p>
					</td>
				</tr>
			</table>
			<p id="aamo-clone-error" class="aamo-field-error"></p>
		</div><!-- /.aamo-modal-body -->
		<div class="aamo-modal-footer">
			<button type="button" class="aamo-modal-cancel button">
				<?php esc_html_e( 'Cancel', 'agency-pulse-admin-menu' ); ?>
			</button>
			<button type="button" id="aamo-clone-submit" class="button button-primary">
				<?php esc_html_e( 'Clone Role', 'agency-pulse-admin-menu' ); ?>
			</button>
			<span class="spinner"></span>
		</div><!-- /.aamo-modal-footer -->
	</div><!-- /.aamo-modal-dialog -->
</div><!-- /#aamo-clone-modal -->

<?php // ── Delete Role confirmation modal (Phase K) ────────────────────────── ?>
<div id="aamo-delete-modal" class="aamo-modal-overlay" hidden>
	<div class="aamo-modal-dialog" role="dialog" aria-modal="true" aria-labelledby="aamo-delete-modal-title">
		<div class="aamo-modal-header">
			<h2 id="aamo-delete-modal-title">
				<?php esc_html_e( 'Delete Role', 'agency-pulse-admin-menu' ); ?>
			</h2>
			<button type="button" class="aamo-modal-close"
				aria-label="<?php esc_attr_e( 'Close', 'agency-pulse-admin-menu' ); ?>">&times;</button>
		</div>
		<div class="aamo-modal-body">
			<div id="aamo-delete-user-warn" class="notice notice-warning inline" style="margin:0 0 12px">
				<p id="aamo-delete-user-count"></p>
			</div>
			<p class="description" style="margin-bottom:12px">
				<?php esc_html_e( 'This action cannot be undone. Type the role name to confirm deletion:', 'agency-pulse-admin-menu' ); ?>
			</p>
			<input type="text" id="aamo-delete-confirm-input" class="regular-text" autocomplete="off">
			<p id="aamo-delete-error" class="aamo-field-error"></p>
		</div><!-- /.aamo-modal-body -->
		<div class="aamo-modal-footer">
			<button type="button" class="aamo-modal-cancel button">
				<?php esc_html_e( 'Cancel', 'agency-pulse-admin-menu' ); ?>
			</button>
			<button type="button" id="aamo-delete-submit" class="button aamo-btn-danger" disabled>
				<?php esc_html_e( 'Delete Role', 'agency-pulse-admin-menu' ); ?>
			</button>
			<span class="spinner"></span>
		</div><!-- /.aamo-modal-footer -->
	</div><!-- /.aamo-modal-dialog -->
</div><!-- /#aamo-delete-modal -->

<?php
/*
 * admin_print_footer_scripts does not reliably fire on this page, so the
 * scripts enqueued in the footer (admin.js) are forced here directly.
 */
do_action( 'admin_print_footer_scripts' );
?>
