/* AI Story Prompt Generator — Mailing List Signup Script */
( function () {
    'use strict';

    document.addEventListener( 'DOMContentLoaded', function () {
        const widgets = document.querySelectorAll( '.aspg-signup-widget' );
        if ( ! widgets.length ) return;
        widgets.forEach( initSignupWidget );
    } );

    function initSignupWidget( widget ) {
        const nameInput   = widget.querySelector( '#aspg-signup-name' );
        const emailInput  = widget.querySelector( '#aspg-signup-email' );
        const submitBtn   = widget.querySelector( '#aspg-signup-btn' );
        const notice      = widget.querySelector( '#aspg-signup-notice' );
        const formEl      = widget.querySelector( '#aspg-signup-form' );
        const successEl   = widget.querySelector( '#aspg-signup-success' );

        submitBtn.addEventListener( 'click', function () {
            const name  = nameInput.value.trim();
            const email = emailInput.value.trim();

            // Basic client-side validation
            if ( ! name ) {
                showNotice( 'Please enter your name.', 'error' );
                nameInput.focus();
                return;
            }
            if ( ! email || ! /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test( email ) ) {
                showNotice( 'Please enter a valid email address.', 'error' );
                emailInput.focus();
                return;
            }

            submitBtn.disabled = true;
            submitBtn.querySelector( '.aspg-btn-text' ).textContent = 'Signing up…';
            hideNotice();

            const formData = new FormData();
            formData.append( 'action', 'aspg_signup' );
            formData.append( 'nonce',  ASPG_SIGNUP.nonce );
            formData.append( 'name',   name );
            formData.append( 'email',  email );

            fetch( ASPG_SIGNUP.ajax_url, {
                method: 'POST',
                body:   formData,
            } )
            .then( function ( res ) {
                if ( ! res.ok ) throw new Error( 'Server error: ' + res.status );
                return res.json();
            } )
            .then( function ( data ) {
                if ( data.success ) {
                    formEl.hidden    = true;
                    successEl.hidden = false;
                } else {
                    const msg = ( data.data && data.data.message ) ? data.data.message : 'Something went wrong. Please try again.';
                    showNotice( msg, 'error' );
                    submitBtn.disabled = false;
                    submitBtn.querySelector( '.aspg-btn-text' ).textContent = 'Join the Mailing List';
                }
            } )
            .catch( function ( err ) {
                showNotice( err.message || 'Network error. Please check your connection.', 'error' );
                submitBtn.disabled = false;
                submitBtn.querySelector( '.aspg-btn-text' ).textContent = 'Join the Mailing List';
            } );
        } );

        // Allow Enter key to submit
        [ nameInput, emailInput ].forEach( function ( input ) {
            input.addEventListener( 'keydown', function ( e ) {
                if ( e.key === 'Enter' ) submitBtn.click();
            } );
        } );

        function showNotice( msg, type ) {
            notice.textContent = msg;
            notice.hidden      = false;
            notice.className   = 'aspg-signup-notice aspg-notice-' + type;
        }

        function hideNotice() {
            notice.hidden    = true;
            notice.textContent = '';
        }
    }

} )();
