// Firebase App (obrigatório) import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js"; // Firebase Analytics (opcional) import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-analytics.js"; // Firebase Auth (para usuários anônimos) import { getAuth, signInAnonymously, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js"; // Firebase Firestore (armazenamento de denúncias e rankings) import { getFirestore, collection, addDoc, doc, runTransaction, serverTimestamp, onSnapshot, query, limit } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js"; // Configuração do Firebase const firebaseConfig = { apiKey: "AIzaSyCWWkNJQIQYDuILXq4s2d_RFmPYgNzuTnQ", authDomain: "nalata-863fd.firebaseapp.com", projectId: "nalata-863fd", storageBucket: "nalata-863fd.firebasestorage.app", messagingSenderId: "372641737125", appId: "1:372641737125:web:9e387de2a25d92f16ae04c", measurementId: "G-2SL34RDZ7H" }; // Inicializa Firebase const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); const auth = getAuth(app); const db = getFirestore(app); let uid = null; // Autenticação anônima signInAnonymously(auth) .then(() => { console.log("Usuário anônimo logado"); }) .catch(err => console.error("Erro na autenticação anônima:", err)); onAuthStateChanged(auth, user => { if (user) { uid = user.uid; console.log("UID do usuário:", uid); // Aqui você pode carregar rankings ou habilitar botões } else { console.log("Nenhum usuário logado"); } }); // Função para enviar denúncia async function enviarDenuncia(nome, email, celular, cpf, estado, cidade, texto) { if (!uid) throw new Error("Usuário não autenticado"); const privateRef = collection(db, `artifacts/na-lata-default-v3/users/${uid}/private_submissions`); const userPublicRef = doc(db, "artifacts/na-lata-default-v3/public/data/users_public", uid); const cityKey = `${estado.toUpperCase()}__${cidade.toUpperCase().replace(/\s/g,'_')}`; const cityRef = doc(db, "artifacts/na-lata-default-v3/public/data/locations_public", cityKey); try { // 1️⃣ Grava denúncia privada await addDoc(privateRef, { name: nome, email, celular, cpf, city: cidade, state: estado, denuncia: texto, createdAt: serverTimestamp() }); // 2️⃣ Atualiza rankings públicos await runTransaction(db, async tx => { // Usuário público const userSnap = await tx.get(userPublicRef); if (userSnap.exists()) { tx.update(userPublicRef, { name: nome, city: cidade, state: estado, reports: (userSnap.data().reports || 0) + 1, updatedAt: serverTimestamp() }); } else { tx.set(userPublicRef, { name: nome, city: cidade, state: estado, reports: 1, createdAt: serverTimestamp() }); } // Localidade const citySnap = await tx.get(cityRef); const currentResolutions = citySnap.exists() ? citySnap.data().resolutions || 0 : 0; if (citySnap.exists()) { tx.update(cityRef, { reports: (citySnap.data().reports || 0) + 1, resolutions: currentResolutions, state: estado, city: cidade, updatedAt: serverTimestamp() }); } else { tx.set(cityRef, { city: cidade, state: estado, reports: 1, resolutions: 0, createdAt: serverTimestamp() }); } }); console.log("Denúncia enviada com sucesso!"); } catch (err) { console.error("Erro ao enviar denúncia:", err); } }