/**
 * mbs-esp-signup.js -- Reader Signup Widget -- F-026-5
 *
 * Handles AJAX form submission for .mbs-esp-signup-form widgets.
 * Validates email client-side, sends nonce-protected POST, and
 * shows inline success/error messages.
 *
 * Localized object: MBS_ESPSignup
 *   .ajaxurl  string  WordPress admin-ajax.php URL
 *   .i18n     object  Translatable strings
 */
( function ( $ ) {
    'use strict';
    var __ = wp.i18n.__;

    $( document ).on( 'submit', '.mbs-esp-signup-form', function ( e ) {
        e.preventDefault();

        var $form    = $( this );
        var $submit  = $form.find( '.mbs-esp-submit' );
        var $msg     = $form.find( '.mbs-esp-msg' );
        var $email   = $form.find( '[name="mbs_esp_email"]' );
        var $consent = $form.find( '[name="mbs_esp_consent"]' );
        var bookId   = $form.data( 'book-id' ) || 0;

        // Client-side email validation.
        var emailVal = $.trim( $email.val() );
        var emailRe  = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if ( ! emailRe.test( emailVal ) ) {
            $msg.removeClass( 'mbs-esp-msg--success' )
                .addClass( 'mbs-esp-msg--error' )
                .text( __( 'Please enter a valid email address.', 'my-book-showroom' ) );
            $email.focus();
            return;
        }

        // Client-side consent validation.
        if ( ! $consent.prop( 'checked' ) ) {
            $msg.removeClass( 'mbs-esp-msg--success' )
                .addClass( 'mbs-esp-msg--error' )
                .text( __( 'Please tick the consent checkbox to subscribe.', 'my-book-showroom' ) );
            $consent.closest( '.mbs-esp-consent' ).find( 'input' ).focus();
            return;
        }

        // Disable submit button during in-flight request.
        $submit.prop( 'disabled', true );
        $msg.removeClass( 'mbs-esp-msg--success mbs-esp-msg--error' ).text( '' );

        $.ajax( {
            url:  MBS_ESPSignup.ajaxurl,
            type: 'POST',
            data: {
                action:          'mbs_esp_subscribe',
                _ajax_nonce:     $form.find( '#_wpnonce' ).val(),
                mbs_esp_email:   emailVal,
                mbs_esp_name:    $.trim( $form.find( '[name="mbs_esp_name"]' ).val() ),
                mbs_esp_consent: $consent.prop( 'checked' ) ? '1' : '0',
                mbs_esp_book_id: bookId,
            },
            success: function ( response ) {
                if ( response.success ) {
                    $form.find( '.mbs-esp-email-input, .mbs-esp-name-input, .mbs-esp-consent-row, .mbs-esp-submit-row' ).hide();
                    $msg.removeClass( 'mbs-esp-msg--error' )
                        .addClass( 'mbs-esp-msg--success' )
                        .text( response.data.message );
                } else {
                    var errMsg = ( response.data && response.data.message )
                        ? response.data.message
                        : __( 'Something went wrong. Please try again.', 'my-book-showroom' );
                    $msg.removeClass( 'mbs-esp-msg--success' )
                        .addClass( 'mbs-esp-msg--error' )
                        .text( errMsg );
                    $submit.prop( 'disabled', false );
                }
            },
            error: function () {
                $msg.removeClass( 'mbs-esp-msg--success' )
                    .addClass( 'mbs-esp-msg--error' )
                    .text( __( 'Something went wrong. Please try again.', 'my-book-showroom' ) );
                $submit.prop( 'disabled', false );
            },
        } );
    } );

} )( jQuery );
