<?php
/**
 * Role Assignments page template
 *
 * Variables provided by Agency_Pulse_Admin_Menu_Role_Assignments::render_page():
 *
 *   $all_configs  array  All named configurations keyed by config ID.
 *                        Each entry: [ id, name, created, items[] ]
 *   $all_roles    array  All registered WP roles: role_slug => display_name.
 *   $assignments  array  Current map of role_slug => config_id.
 *
 * Layout: one card per configuration. Each card shows a checkbox for every
 * registered WordPress role. A checkbox is pre-checked when that role is
 * currently assigned to this configuration. Mutual-exclusion enforcement
 * (a role can only be checked under one config) is handled by Phase 7 JS.
 *
 * @package Agency_Pulse_Admin_Menu
 * @since   1.0.0
 */

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

	<h1><?php esc_html_e( 'Role Assignments', 'agency-pulse-admin-menu' ); ?></h1>

	<p class="description">
		<?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><!-- /.wrap -->

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