document.addEventListener('DOMContentLoaded', function () {
    const cancelButton = document.getElementById('login');
    const continueButton = document.getElementById('continue');

    cancelButton.addEventListener('click', function () {
        console.log(`'Login' button clicked!`);
        window.electronAPI.changeContent('login.html')
            .then(() => console.log('Content changed successfully'))
            .catch(error => console.error('Error changing content:', error));
    });

    continueButton.addEventListener('click', async function (e) {
        try {
            e.preventDefault();
            console.log('Continue button clicked');

            const form = document.getElementById('signupForm');
            const formData = new FormData(form);

            const email = formData.get('email');
            const name = formData.get('name');
            const password = formData.get('password');

            if (!email || !name || !password) {
                throw new Error('All fields are required.');
            }

            if(password.length < 8){
                throw new Error('Password must have minimum length 8!');
            }

            let statusFetch = 200;

            await fetch('http://localhost:5000/users/validate_email', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'x-api-key': 'uc_api'
                },
                body: JSON.stringify({
                    email: email
                })
            }).then(async response => {
                const responseData = await response.json();
                statusFetch = response.status;
                console.error(responseData.message);
            })
                .catch(error => {
                console.log(error);
            })

            switch(statusFetch){
                case 500:
                    throw new Error('Internal server error! Try again later');
                case 400:
                    throw new Error('Not a valid email!');
                case 409:
                    throw new Error('Email already exists in system!');
            }

            const formDataJSON = {};
            formData.forEach((value, key) => {
                formDataJSON[key] = value;
            });

            const result = await window.electronAPI.writeFile('signupData.json', JSON.stringify(formDataJSON));
            if (!result.success) {
                throw new Error('Error writing to file. Please try again later.');
            }

            window.electronAPI.changeContent('sign_up_departments.html')
                .then(() => console.log('Content changed successfully'))
                .catch(error => console.error('Error changing content:', error));
        }
        catch (error) {
            await window.electronAPI.showAlert(error.message)
                .then(() => console.log('Alert window opened'))
                .catch(error => console.error('Error changing content:', error));;
        }
    });
});