/* =========================================
   GP Crackers - Form Validation (jQuery)
   ========================================= */

$(document).ready(function() {
    
    // Email Regex
    const isValidEmail = (email) => {
        return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
    };

    // Mobile Regex (10 digits)
    const isValidMobile = (mobile) => {
        return /^\d{10}$/.test(mobile);
    };

    // Password Strength Logic
    const checkPasswordStrength = (password) => {
        let strength = 0;
        if (password.length > 5) strength += 20;
        if (password.length > 8) strength += 20;
        if (/[A-Z]/.test(password)) strength += 20;
        if (/[0-9]/.test(password)) strength += 20;
        if (/[^A-Za-z0-9]/.test(password)) strength += 20;
        return strength;
    };

    const updateStrengthMeter = (strength) => {
        const meter = $('#password-strength-meter');
        const bar = $('#strength-bar');
        const text = $('#strength-text');

        if (strength === 0) {
            meter.hide();
            return;
        }

        meter.show();
        bar.css('width', strength + '%');
        
        if (strength <= 40) {
            bar.removeClass().addClass('progress-bar bg-danger');
            text.text('Weak').removeClass().addClass('text-danger');
        } else if (strength <= 80) {
            bar.removeClass().addClass('progress-bar bg-warning');
            text.text('Medium').removeClass().addClass('text-warning');
        } else {
            bar.removeClass().addClass('progress-bar bg-success');
            text.text('Strong').removeClass().addClass('text-success');
        }
    };

    // Real-time validation
    $('.form-control-modern').on('input blur', function() {
        const id = $(this).attr('id');
        const val = $(this).val().trim();

        if (id === 'reg-password') {
            updateStrengthMeter(checkPasswordStrength(val));
        }

        validateField($(this));
    });

    function validateField(element) {
        const id = element.attr('id');
        const val = element.val().trim();
        let isValid = true;
        let errorMsg = '';

        // Registration Fields
        if (id === 'reg-name') {
            if (!val) { errorMsg = 'Full Name required'; isValid = false; }
            else if (val.length < 3) { errorMsg = 'Min 3 characters'; isValid = false; }
        } else if (id === 'reg-phone') {
            if (!val) { errorMsg = 'Mobile required'; isValid = false; }
            else if (!isValidMobile(val)) { errorMsg = '10-digit number'; isValid = false; }
        } else if (id === 'reg-email') {
            if (!val) { errorMsg = 'Email required'; isValid = false; }
            else if (!isValidEmail(val)) { errorMsg = 'Invalid email'; isValid = false; }
        } else if (id === 'reg-password') {
            if (!val) { errorMsg = 'Password required'; isValid = false; }
            else if (val.length < 6) { errorMsg = 'Min 6 characters'; isValid = false; }
        } else if (id === 'reg-confirm-password') {
            const pass = $('#reg-password').val();
            if (!val) { errorMsg = 'Confirm password'; isValid = false; }
            else if (val !== pass) { errorMsg = 'Passwords mismatch'; isValid = false; }
        }
        
        // Login Fields
        if (id === 'login-username') {
            if (!val) { errorMsg = 'Required'; isValid = false; }
        } else if (id === 'login-password') {
            if (!val) { errorMsg = 'Required'; isValid = false; }
        }

        if (!isValid && val !== '') {
            showError(element, errorMsg);
        } else if (isValid && val !== '') {
            showSuccess(element);
        } else {
            clearStates(element);
        }

        return isValid;
    }

    function showSuccess(element) {
        element.removeClass('is-invalid').addClass('is-valid');
        $(`#${element.attr('id')}-error`).text('').hide();
    }

    function showError(element, message) {
        element.removeClass('is-valid').addClass('is-invalid');
        $(`#${element.attr('id')}-error`).text(message).show();
    }

    function clearStates(element) {
        element.removeClass('is-invalid is-valid');
        $(`#${element.attr('id')}-error`).text('').hide();
    }

    // Validation Logic for Login (Exported for auth.js)
    window.validateLoginForm = function() {
        let valid = true;
        valid = validateField($('#login-username')) && valid;
        valid = validateField($('#login-password')) && valid;
        return valid;
    };

    // Validation Logic for Register (Exported for auth.js)
    window.validateRegisterForm = function() {
        let valid = true;
        valid = validateField($('#reg-name')) && valid;
        valid = validateField($('#reg-phone')) && valid;
        valid = validateField($('#reg-email')) && valid;
        valid = validateField($('#reg-password')) && valid;
        valid = validateField($('#reg-confirm-password')) && valid;
        return valid;
    };
});
