package com.aistudio.bonjuteri.jwlry.data import android.content.Context import android.util.Log import com.aistudio.bonjuteri.jwlry.R import com.google.firebase.FirebaseApp import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestoreException object FirestoreHelper { private const val TAG = "FirestoreHelper" fun getFirestore(context: Context): FirebaseFirestore { return try { val dbId = context.getString(R.string.firestore_database_id) if (dbId.isNotBlank() && dbId != "(default)") { FirebaseFirestore.getInstance(FirebaseApp.getInstance(), dbId) } else { FirebaseFirestore.getInstance() } } catch (e: Exception) { Log.w(TAG, "Error obtaining custom database ID, falling back to default instance: ${e.message}") FirebaseFirestore.getInstance() } } fun handleFirestoreError(exception: Throwable, operation: String, path: String): String { val isExpectedState = exception is FirebaseFirestoreException && ( exception.code == FirebaseFirestoreException.Code.PERMISSION_DENIED || exception.code == FirebaseFirestoreException.Code.UNAVAILABLE ) if (isExpectedState) { Log.w(TAG, "Firestore $operation at $path: ${exception.message}") } else { Log.e(TAG, "Firestore error during $operation at $path: ${exception.message}", exception) } return when (exception) { is FirebaseFirestoreException -> { when (exception.code) { FirebaseFirestoreException.Code.PERMISSION_DENIED -> "Erişim izni bekleniyor. Firebase Console kurallarını kontrol ediniz." FirebaseFirestoreException.Code.NOT_FOUND -> "İstenen kayıt bulunamadı." FirebaseFirestoreException.Code.ALREADY_EXISTS -> "Bu kayıt zaten mevcut." FirebaseFirestoreException.Code.UNAVAILABLE -> "Bağlantı kurulamadı. Lütfen internet bağlantınızı kontrol ediniz." else -> "Veritabanı hatası: ${exception.localizedMessage ?: exception.message}" } } else -> exception.localizedMessage ?: "Beklenmeyen bir hata oluştu" } } }