// Register Request
jQuery( document ).ready( function ( $ ) {
	// Handle WordPress sidebar menu highlighting for hash-based routes
	function updateSidebarMenuHighlight() {
		const hash = window.location.hash;
		if ( ! hash || hash === '#/' ) {
			return;
		}

		// Remove leading #/ and get the base path
		const path = hash.replace( '#/', '' );

		// Define route-to-menu mappings for hash routes
		const menuMappings = {
			'alert/': 'ultimate-security#alert/alert-management',
			'activity-logs': 'ultimate-security#/activity-logs',
			'ai-malware-scanner': 'ultimate-security#/ai-malware-scanner',
			'settings/login/': 'ultimate-security#settings/login/2fa/overview',
			'settings/threat/': 'ultimate-security#settings/threat/anti-bot/re-captcha',
			'settings/hardening/': 'ultimate-security#settings/hardening/data-security/overview',
			'settings/monitoring/': 'ultimate-security#settings/monitoring/site-health-info',
			'settings/maintenance/': 'ultimate-security#settings/maintenance/comments-cleaner',
			'contact-form': 'ultimate-security#/contact-form',
			license: 'ultimate-security#/license',
		};

		// Find matching menu slug
		let targetSlug = null;
		for ( const prefix in menuMappings ) {
			if ( path.indexOf( prefix ) === 0 ) {
				targetSlug = menuMappings[ prefix ];
				break;
			}
		}

		if ( targetSlug ) {
			// Remove current highlighting
			$( '#adminmenu .wp-submenu li' ).removeClass( 'current' );
			$( '#adminmenu .wp-submenu a' ).removeClass( 'current' );

			// Find and highlight the correct menu item
			$( '#adminmenu .wp-submenu a[href*="' + targetSlug + '"]' )
				.addClass( 'current' )
				.parent()
				.addClass( 'current' );
		}
	}

	// Run on page load
	updateSidebarMenuHighlight();

	// Run on hash change (for SPA navigation)
	$( window ).on( 'hashchange', function () {
		updateSidebarMenuHighlight();
	} );

	// Review notice handlers
	$( document ).on( 'click', '.us-review-btn', function ( e ) {
		const action = $( this ).data( 'action' );
		const $notice = $( this ).closest( '.ultimate-security-review-notice' );
		const href = $( this ).attr( 'href' );

		// Prevent default for all buttons
		e.preventDefault();

		$.ajax( {
			url: ultimate_security_ajax.ajax_url,
			method: 'POST',
			data: {
				action: 'ultimate_security_review_action',
				review_action: action,
				security: ultimate_security_ajax_obj.security,
			},
			success( response ) {
				if ( response.success ) {
					// Add dismiss animation class
					$notice.addClass( 'us-dismissing' );
					setTimeout( function () {
						$notice.remove();
					}, 300 );
					// Open review page if clicking the review button
					if ( action === 'review' && href ) {
						window.open( href, '_blank' );
					}
				}
			},
		} );
	} );

	// Star rating click handler
	$( document ).on( 'click', '.us-star', function ( e ) {
		e.preventDefault();
		const $notice = $( this ).closest( '.ultimate-security-review-notice' );
		const href = $( this ).attr( 'href' );

		$.ajax( {
			url: ultimate_security_ajax.ajax_url,
			method: 'POST',
			data: {
				action: 'ultimate_security_review_action',
				review_action: 'review',
				security: ultimate_security_ajax_obj.security,
			},
			success( response ) {
				if ( response.success ) {
					$notice.addClass( 'us-dismissing' );
					setTimeout( function () {
						$notice.remove();
					}, 300 );
					if ( href ) {
						window.open( href, '_blank' );
					}
				}
			},
		} );
	} );
} );
