import os from typing import Dict _initialized = False _use_mock = True def init_firebase(credentials_path: str = None): global _initialized, _use_mock try: import firebase_admin from firebase_admin import credentials, messaging except Exception: _use_mock = True return if credentials_path is None: credentials_path = os.environ.get('FIREBASE_CREDENTIALS_PATH', 'backend/secrets/firebase-adminsdk.json') if not os.path.exists(credentials_path): _use_mock = True return try: cred = credentials.Certificate(credentials_path) firebase_admin.initialize_app(cred) _initialized = True _use_mock = False except Exception: _use_mock = True def send_to_topic(topic: str, payload: Dict): """Sends a push to an FCM topic. Falls back to mock (prints) if not configured.""" global _use_mock if _use_mock: print(f"[MOCK PUSH] topic={topic} payload={payload}") return {"mock": True, "topic": topic, "payload": payload} try: from firebase_admin import messaging message = messaging.Message( notification=messaging.Notification(title=payload.get('title'), body=payload.get('body')), topic=topic, ) resp = messaging.send(message) return {"result": resp} except Exception as e: print(f"[PUSH ERROR] {e}") return {"error": str(e)}