<?php
/**
 * Admin request guard and redirect helpers for admin autonomy screens.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Admin_Autonomy_Admin_Request_Helper {
	public function assert_admin_post_nonce( $action, $nonce_key ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Forbidden', 'niwa-ai' ), 403 );
		}

		check_admin_referer( $action, $nonce_key );
	}

	public function redirect_to_settings_page( $notice, $message, $tab = '' ) {
		$args = array(
			'page'                         => 'niwa-ai-admin-autonomy',
			'niwa_admin_autonomy_notice'  => sanitize_key( $notice ),
			'niwa_admin_autonomy_message' => (string) $message,
		);

		$tab = sanitize_key( (string) $tab );
		if ( '' !== $tab ) {
			$args['tab'] = $tab;
		}

		wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) );
		exit;
	}
}
