// PROTEÇÕES GLOBAIS
document.addEventListener('DOMContentLoaded', function() {
    'use strict';
    
    // Bloquear todas as teclas de atalho
    document.addEventListener('keydown', bloquearAtalhos);
    
    // Bloquear clique direito
    document.addEventListener('contextmenu', function(e) {
        e.preventDefault();
        return false;
    });
    
    // Bloquear seleção de texto
    document.addEventListener('selectstart', function(e) {
        e.preventDefault();
        return false;
    });
    
    // Bloquear arrastar
    document.addEventListener('dragstart', function(e) {
        e.preventDefault();
        return false;
    });
    
    // Bloquear copiar/colar/recortar
    document.addEventListener('copy', function(e) {
        e.preventDefault();
        return false;
    });
    
    document.addEventListener('cut', function(e) {
        e.preventDefault();
        return false;
    });
    
    document.addEventListener('paste', function(e) {
        e.preventDefault();
        return false;
    });
    
    // Bloquear print screen (parcial)
    document.addEventListener('keyup', function(e) {
        if (e.key === 'PrintScreen') {
            navigator.clipboard.writeText('');
            alert('Print screen bloqueado!');
        }
    });
    
    // Bloquear inspecionar elemento
    document.addEventListener('keydown', function(e) {
        if (e.key === 'F12' || 
            (e.ctrlKey && e.shiftKey && e.key === 'I') ||
            (e.ctrlKey && e.shiftKey && e.key === 'J') ||
            (e.ctrlKey && e.key === 'U')) {
            e.preventDefault();
            return false;
        }
    });
    
    // Configurar máscara do WhatsApp
    const whatsappInput = document.getElementById('whatsapp');
    if (whatsappInput) {
        whatsappInput.addEventListener('input', mascaraWhatsapp);
    }
    
    // Configurar botão de submit
    const btnSubmit = document.querySelector('.btn-submit');
    if (btnSubmit) {
        btnSubmit.addEventListener('click', salvarContato);
    }
    
    // Carregar contatos salvos ao iniciar
    console.log('%c📥 Sistema de Contatos Fernando HF Oficial', 'color: #a3e0ff; font-size: 16px;');
    console.log('%cContatos já salvos:', 'color: #b3e4ff;', 
        JSON.parse(localStorage.getItem('contatosFernandoHF')) || []);
});

// FUNÇÃO PARA BLOQUEAR ATALHOS
function bloquearAtalhos(e) {
    // Bloquear Ctrl+S
    if (e.ctrlKey && e.key === 's') {
        e.preventDefault();
        return false;
    }
    
    // Bloquear Ctrl+P
    if (e.ctrlKey && e.key === 'p') {
        e.preventDefault();
        return false;
    }
    
    // Bloquear Ctrl+Shift+C (inspecionar)
    if (e.ctrlKey && e.shiftKey && e.key === 'C') {
        e.preventDefault();
        return false;
    }
    
    // Bloquear Ctrl+Shift+I (devtools)
    if (e.ctrlKey && e.shiftKey && e.key === 'I') {
        e.preventDefault();
        return false;
    }
}

// FUNÇÃO PARA SALVAR OS DADOS EM JSON
function salvarContato() {
    // Captura os valores dos campos
    const nome = document.getElementById('nome').value.trim();
    const email = document.getElementById('email').value.trim();
    const whatsapp = document.getElementById('whatsapp').value.trim();

    // Validação simples
    if (!nome || !email || !whatsapp) {
        alert('Por favor, preencha todos os campos!');
        return;
    }

    if (!email.includes('@') || !email.includes('.')) {
        alert('Por favor, insira um e-mail válido!');
        return;
    }

    // Cria o objeto JSON com os dados
    const dadosContato = {
        nome: nome,
        email: email,
        whatsapp: whatsapp,
        data_contato: new Date().toISOString(),
        origem: 'Fernando HF Oficial - Página de Manutenção'
    };

    // Converte para string JSON
    const jsonString = JSON.stringify(dadosContato, null, 2);
    
    // Salva no localStorage (simula salvamento)
    let contatos = JSON.parse(localStorage.getItem('contatosFernandoHF')) || [];
    contatos.push(dadosContato);
    localStorage.setItem('contatosFernandoHF', JSON.stringify(contatos));

    // Mostra o JSON no console (protegido)
    console.log('%c📋 Novo contato salvo:', 'color: #00ff88;');
    console.log(jsonString);
    console.log('%c📊 Total de contatos:', 'color: #ffaa00;', contatos.length);

    // Mostra mensagem de sucesso
    const successMessage = document.getElementById('successMessage');
    successMessage.style.display = 'block';
    
    // Limpa os campos
    document.getElementById('nome').value = '';
    document.getElementById('email').value = '';
    document.getElementById('whatsapp').value = '';

    // Esconde a mensagem após 5 segundos
    setTimeout(() => {
        successMessage.style.display = 'none';
    }, 5000);
}

// MÁSCARA PARA WHATSAPP
function mascaraWhatsapp(e) {
    let valor = e.target.value.replace(/\D/g, '');
    if (valor.length > 11) valor = valor.slice(0, 11);
    if (valor.length > 7) {
        valor = `(${valor.slice(0,2)}) ${valor.slice(2,7)}-${valor.slice(7)}`;
    } else if (valor.length > 2) {
        valor = `(${valor.slice(0,2)}) ${valor.slice(2)}`;
    }
    e.target.value = valor;
}

// DESABILITAR CONSOLE (proteção extra)
setInterval(function() {
    console.clear();
    console.log('%c⚠️ Console protegido - Fernando HF Oficial', 'color: red; font-size: 20px;');
}, 1000);