import firebase_admin
from firebase_admin import credentials, messaging
import os

# Configura la ruta a tu llave
# 1. Obtenemos la ruta absoluta de la carpeta donde vive ESTE script (.py)
current_dir = os.path.dirname(os.path.abspath(__file__))

# 2. Construimos la ruta al JSON combinando la carpeta y el nombre del archivo
CRED_PATH = os.path.join(current_dir, 'serviceAccountKey.json')

def test_push():
    print(f"Testing key at: {CRED_PATH}")
    try:
        cred = credentials.Certificate(CRED_PATH)
        firebase_admin.initialize_app(cred)
        print("✅ [OK] Credenciales válidas y App inicializada.")
        
        # Prueba de envío a un tema de prueba (dry_run=True no envía al celular, solo valida con Google)
        msg = messaging.Message(
            topic='test_topic',
            notification=messaging.Notification(title='Test', body='Test'),
        )
        response = messaging.send(msg, dry_run=True)
        print(f"✅ [OK] Conexión con FCM exitosa. ID: {response}")
        
    except Exception as e:
        print(f"❌ [ERROR] Falló la conexión: {e}")

if __name__ == "__main__":
    test_push()