document.addEventListener('DOMContentLoaded', function () {
    const shareSubmitButton = document.getElementById('share-submit');
    if (!shareSubmitButton) {
        console.error("⚠️ Botón de compartir no encontrado");
        return;
    }

    shareSubmitButton.addEventListener('click', () => {
        console.log("✅ Share button clicked");

        const username = document.getElementById('share-username').value.trim();
        const canEdit = document.getElementById('share-can-edit').checked;

        if (!username) {
            showToast('Por favor, ingresa un nombre de usuario.', 'bg-warning');
            return;
        }

        fetch(`/paste/${document.body.dataset.pasteId}/share`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'include',
            body: JSON.stringify({ username, can_edit: canEdit })
        })
        .then(response => response.json())
        .then(data => {
            showToast(data.message || 'Shared successfully!', 'bg-success');
            bootstrap.Modal.getInstance(document.getElementById('shareModal'))?.hide();
        })
        .catch(error => {
            console.error('Error sharing paste:', error);
            showToast('Error sharing paste.', 'bg-danger');
        });
    });
});

