/**
 * Notification settings handlers (save + test email) for the relocated
 * Email Notification Settings card on the My AI Agent → Voice & Behavior tab.
 *
 * Self-contained (no jQuery, no settings-page.js dependency) so it can run on the
 * My AI Agent page without colliding with that page's own voice/save handlers.
 * No-ops when the elements aren't present.
 *
 * Expects window.sitestaffrNotifSettings: { ajaxurl, nonce, i18n{} }.
 *
 * @since 1.28.0
 */
( function () {
	var cfg = window.sitestaffrNotifSettings;
	if ( ! cfg ) {
		return;
	}
	var i18n = cfg.i18n || {};
	function t( key, fallback ) { return i18n[ key ] || fallback; }

	function post( params ) {
		var body = new URLSearchParams();
		body.append( 'nonce', cfg.nonce );
		Object.keys( params ).forEach( function ( k ) { body.append( k, params[ k ] ); } );
		return fetch( cfg.ajaxurl, {
			method: 'POST',
			credentials: 'same-origin',
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			body: body.toString()
		} ).then( function ( r ) { return r.json(); } );
	}

	// Shared save-UX component (dependency of this script).
	var SaveUX = window.SiteStaffrSaveUX;

	var saveBtn = document.getElementById( 'sitestaffr-save-notification-settings' );
	if ( saveBtn ) {
		var notifDirty = SaveUX.trackDirty( saveBtn.closest( '.sitestaffr-email-layout' ) );
		saveBtn.addEventListener( 'click', function () {
			var status = SaveUX.status( '#sitestaffr-notif-save-status' );
			var enabled = document.getElementById( 'sitestaffr_email_notifications_enabled' );
			var email = document.getElementById( 'sitestaffr_notification_email' );
			var cc = document.getElementById( 'sitestaffr_notification_cc_emails' );
			var scope = document.getElementById( 'sitestaffr_notification_scope' );

			saveBtn.disabled = true;
			status.saving();

			post( {
				action: 'sitestaffr_save_notification_settings',
				enabled: enabled && enabled.checked ? 1 : 0,
				email: email ? email.value : '',
				cc_emails: cc ? cc.value : '',
				// Empty when the control is missing; the server treats an empty value
				// as "not supplied" and keeps whatever is stored, rather than
				// resetting the owner's choice back to the default.
				notification_scope: scope ? scope.value : ''
			} ).then( function ( res ) {
				saveBtn.disabled = false;
				if ( res && res.success ) {
					status.saved( res.data && res.data.message );
					notifDirty.markClean();
				} else {
					// Falls back to the component's localized "Save failed."
					status.error( res && res.data && res.data.message );
				}
			} ).catch( function () {
				saveBtn.disabled = false;
				status.error();
			} );
		} );
	}

	var testBtn = document.getElementById( 'sitestaffr-send-test-email' );
	if ( testBtn ) {
		testBtn.addEventListener( 'click', function () {
			var status = SaveUX.status( '#sitestaffr-test-email-status' );
			testBtn.disabled = true;
			status.saving( t( 'sending', 'Sending…' ) );

			post( { action: 'sitestaffr_send_test_email', source: 'voice' } ).then( function ( res ) {
				testBtn.disabled = false;
				if ( res && res.success ) {
					status.saved( ( res.data && res.data.message ) || t( 'sent', 'Test email sent.' ) );
				} else {
					status.error( ( res && res.data && res.data.message ) || t( 'error', 'Something went wrong.' ) );
				}
			} ).catch( function () {
				testBtn.disabled = false;
				status.error( t( 'error', 'Something went wrong.' ) );
			} );
		} );
	}

	// Blog agent's own recipients card (separate list, separate save action).
	var blogSaveBtn = document.getElementById( 'sitestaffr-blog-notif-save' );
	if ( blogSaveBtn ) {
		var blogNotifDirty = SaveUX.trackDirty( blogSaveBtn.closest( '.sitestaffr-email-layout' ) );
		blogSaveBtn.addEventListener( 'click', function () {
			var status = SaveUX.status( '#sitestaffr-blog-notif-save-status' );
			var enabled = document.getElementById( 'sitestaffr-blog-notif-enabled' );
			var email = document.getElementById( 'sitestaffr-blog-notif-email' );
			var cc = document.getElementById( 'sitestaffr-blog-notif-cc' );

			blogSaveBtn.disabled = true;
			status.saving();

			post( {
				action: 'sitestaffr_save_blog_notification_settings',
				enabled: enabled && enabled.checked ? 1 : 0,
				email: email ? email.value : '',
				cc_emails: cc ? cc.value : ''
			} ).then( function ( res ) {
				blogSaveBtn.disabled = false;
				if ( res && res.success ) {
					status.saved( res.data && res.data.message );
					blogNotifDirty.markClean();
				} else {
					// Falls back to the component's localized "Save failed."
					status.error( res && res.data && res.data.message );
				}
			} ).catch( function () {
				blogSaveBtn.disabled = false;
				status.error();
			} );
		} );
	}

	var blogTestBtn = document.getElementById( 'sitestaffr-blog-notif-test' );
	if ( blogTestBtn ) {
		blogTestBtn.addEventListener( 'click', function () {
			var status = SaveUX.status( '#sitestaffr-blog-notif-test-status' );
			var email = document.getElementById( 'sitestaffr-blog-notif-email' );
			var addr = email && email.value ? email.value.trim() : '';
			if ( ! addr ) {
				status.error( t( 'blogEmailRequired', 'Enter a blog email address first.' ) );
				return;
			}
			blogTestBtn.disabled = true;
			status.saving( t( 'sending', 'Sending…' ) );

			// Always to the typed blog address — no fallback to the conversation email.
			post( { action: 'sitestaffr_send_test_email', email: addr, source: 'blog' } ).then( function ( res ) {
				blogTestBtn.disabled = false;
				if ( res && res.success ) {
					status.saved( ( res.data && res.data.message ) || t( 'sent', 'Test email sent.' ) );
				} else {
					status.error( ( res && res.data && res.data.message ) || t( 'error', 'Something went wrong.' ) );
				}
			} ).catch( function () {
				blogTestBtn.disabled = false;
				status.error( t( 'error', 'Something went wrong.' ) );
			} );
		} );
	}
} )();
