/**
 * Jobus - Frontend AJAX Controller
 *
 * Handles all public-facing AJAX actions used across the Jobus plugin.
 * This script manages the following user interactions via AJAX:
 *
 * It ensures secure data transmission using nonce verification and utilizes
 * WordPress's built-in admin-ajax.php endpoint.
 *
 * @summary   Modular JS controller for handling public AJAX actions
 * @author    spider-themes
 * @since     1.0.0
 * @package   Jobus
 * @license   GPL-2.0+ https://www.gnu.org/licenses/gpl-2.0.html
 * @link      https://developer.wordpress.org/plugins/javascript/ajax/
 */

; (function ($) {

    'use strict';

    const JobusAjaxActions = {
        init: function () {
            this.savePost(); // save for job and candidate posts
            this.jobApplicationForm(); // Initialize job application form submission by candidates
            this.applicationFormUI(); // Guest gate panel switch + field validation messages
            this.emailFormToCandidate(); // Initialize email form to candidate
            this.registerForm(); // Initialize registration form
        },

        /**
         * Unified handler for saving/unsaving jobs or candidates.
         */
        savePost: function () {
            $(document).on('click', '.save_post_btn', function (e) {
                e.preventDefault();

                // Skip if user is not logged in (handled by public.js)
                const isLoggedIn = jobus_public_obj.is_user_logged_in === true || jobus_public_obj.is_user_logged_in === '1';
                if (!isLoggedIn) {
                    return;
                }

                const btn = $(this);
                const postId = btn.data('post_id');
                const postType = btn.data('post_type');
                const metaKey = btn.data('meta_key');
                const nonce = jobus_public_obj.save_post_nonce;
                const icon = btn.find('i');
                if (!postId || !postType || !metaKey || !nonce || btn.hasClass('disabled')) return;
                const originalIcon = icon.attr('class');
                icon.attr('class', 'jbs-spinner-border jbs-spinner-border-sm jbs-align-middle');
                btn.addClass('loading disabled');
                $.ajax({
                    url: jobus_public_obj.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'jobus_saved_post',
                        post_id: postId,
                        post_type: postType,
                        meta_key: metaKey,
                        nonce: nonce
                    },
                    success: function (res) {
                        btn.removeClass('loading disabled');
                        if (res.success && res.data && typeof res.data.status !== 'undefined') {
                            // Debug: log action and button
                            console.log('AJAX success:', res.data.status, 'for post', postId, 'type', postType);
                            // Always update button state
                            if (res.data.status === 'added') {
                                btn.addClass('saved');
                                icon.attr('class', 'bi bi-bookmark-check-fill jbs-text-primary');
                                btn.attr('title', postType === 'jobus_job' ? 'Saved' : postType === 'jobus_candidate' ? 'Saved Candidate' : 'Saved');
                            } else if (res.data.status === 'removed') {
                                btn.removeClass('saved');
                                icon.attr('class', 'bi bi-bookmark-dash');
                                btn.attr('title', postType === 'jobus_job' ? 'Save Job' : postType === 'jobus_candidate' ? 'Save Candidate' : 'Save');
                            }
                        } else {
                            icon.attr('class', originalIcon);
                        }
                    },
                    error: function () {
                        btn.removeClass('loading disabled');
                        icon.attr('class', originalIcon);
                    }
                });
            });
        },


        /**
         * Handles job application form submission.
         */
        jobApplicationForm: function () {

            const jobApplication = $('#jobApplicationForm');

            jobApplication.on('submit', function (event) {
                event.preventDefault();

                // Collect FormData before reset
                const formData = new FormData(this);
                formData.append('action', 'jobus_job_application');

                // Show success instantly and clear the form
                jobApplication[0].reset();
                $('#jbs-application-inline-success').fadeIn();

                // Save to server in background
                $.ajax({
                    url: jobus_public_obj.ajax_url,
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (response) {
                        if (!response.success) {
                            $('#jbs-application-inline-success').hide();
                            alert(response.data && response.data.message ? response.data.message : 'Submission failed.');
                        }
                    },
                    error: function (xhr, status, error) {
                        $('#jbs-application-inline-success').hide();
                        console.error(error);
                        alert('Error submitting application. Please try again.');
                    }
                });
            });
        },


        /**
         * Guest gate panel switch and custom HTML5 validation messages for the application form.
         */
        applicationFormUI: function () {

            $(document).on('click', '#jbs-continue-as-guest', function () {
                $('#jbs-apply-gate-panel').hide();
                $('#jbs-apply-form-panel').show();
            });

            var i18n = (typeof jobus_public_obj !== 'undefined' && jobus_public_obj.i18n) ? jobus_public_obj.i18n : {};

            // 'invalid' does not bubble — use capture phase to handle all fields in the form
            document.addEventListener('invalid', function (e) {
                var el = e.target;
                if (!el.form || el.form.id !== 'jobApplicationForm') return;

                if (el.id === 'email') {
                    if (el.validity.valueMissing) {
                        el.setCustomValidity(i18n.email_required || '');
                    } else if (el.validity.typeMismatch) {
                        el.setCustomValidity(i18n.email_invalid || '');
                    }
                } else {
                    var msgMap = {
                        firstName: i18n.first_name_required,
                        lastName:  i18n.last_name_required,
                        phone:     i18n.phone_required,
                        message:   i18n.message_required,
                        upload_cv: i18n.cv_required,
                    };
                    if (msgMap[el.id]) el.setCustomValidity(msgMap[el.id]);
                }
            }, true);

            // Reset custom validity on input/change so re-validation runs cleanly
            $(document).on('input change', '#jobApplicationForm input, #jobApplicationForm textarea', function () {
                this.setCustomValidity('');
            });
        },


        /**
         * Handles email form submission to candidate.
         */
        emailFormToCandidate: function () {

            $('#candidate-email-from').on('submit', function (event) {
                event.preventDefault(); // Prevent default form submission

                let formData = $(this).serialize(); // Serialize form data
                let candidateId = $('#candidate-id').val(); // Get candidate ID

                // Add class to the message container on submit button click
                let messageContainer = $('#email-form-message');

                $.ajax({
                    url: jobus_public_obj.ajax_url, // WordPress AJAX URL
                    type: 'POST',
                    data: formData + '&action=jobus_candidate_send_mail_form&security=' + jobus_public_obj.candidate_email_nonce + '&candidate_id=' + candidateId,
                    success: function (response) {
                        messageContainer.removeClass('success error'); // Clear any previous messages

                        if (response.success) {
                            // For success responses, data is a string
                            let successMessage = typeof response.data === 'string' ? response.data : 'Message sent successfully!';
                            messageContainer.addClass('success').text(successMessage);
                            $('#candidate-email-from')[0].reset(); // Clear the form fields

                            // Remove the message after 10 seconds
                            setTimeout(function () {
                                messageContainer.removeClass('success').text('');
                            }, 10000); // 10000 milliseconds = 10 seconds
                        } else {
                            // For error responses, data might be an object with 'message' property
                            let errorMessage = '';
                            if (typeof response.data === 'object' && response.data.message) {
                                errorMessage = response.data.message;
                            } else if (typeof response.data === 'string') {
                                errorMessage = response.data;
                            } else {
                                errorMessage = 'An error occurred. Please try again.';
                            }
                            messageContainer.addClass('error').text(errorMessage);
                        }
                    },
                    error: function () {
                        messageContainer.addClass('error').text('There was an error with the AJAX request.');
                    }
                });
            });
        },


        /**
         * Handles registration form submission.
         */
        registerForm: function () {
            const self = this;
            const messageContainer = $('#registration-message');

            const handleRegistration = function (formSelector, actionName, nonceName) {
                $(formSelector).on('submit', function (e) {
                    e.preventDefault();

                    const form = $(this);
                    const btn = form.find('button[type="submit"]');
                    const originalBtnText = btn.text();

                    btn.prop('disabled', true).text('Processing...');
                    messageContainer.removeClass('jbs-alert jbs-alert-success jbs-alert-danger').hide();

                    const formData = new FormData(this);
                    formData.append('action', actionName);
                    formData.append('nonce', form.find('input[name="' + nonceName + '"]').val());

                    $.ajax({
                        url: jobus_public_obj.ajax_url,
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function (res) {
                            if (res.success) {
                                messageContainer.addClass('jbs-alert jbs-alert-success').html(res.data.message).fadeIn();
                                form.find('input:not([type="hidden"])').val(''); // Clear form

                                // Redirect after 2 seconds
                                setTimeout(function () {
                                    window.location.href = res.data.redirect_url;
                                }, 2000);
                            } else {
                                messageContainer.addClass('jbs-alert jbs-alert-danger').html(res.data.message).fadeIn();
                                btn.prop('disabled', false).text(originalBtnText);
                            }
                        },
                        error: function () {
                            messageContainer.addClass('jbs-alert jbs-alert-danger').text('An error occurred. Please try again.').fadeIn();
                            btn.prop('disabled', false).text(originalBtnText);
                        }
                    });
                });
            };

            handleRegistration('#jobus-candidate-registration-form', 'jobus_register_candidate', 'register_candidate_nonce');
            handleRegistration('#jobus-employer-registration-form', 'jobus_register_employer', 'register_employer_nonce');
        }

    };

    // Initialize when DOM is ready
    $(document).ready(function () {
        JobusAjaxActions.init();
    });

})(jQuery);