# Firebase setup for Recolecta project

This document explains the manual steps to create a Firebase project, register Android/iOS apps, obtain the Admin SDK credentials for backend (FCM), and connect the Flutter app.

1) Create a Firebase project
   - Go to https://console.firebase.google.com/ and create a new project (e.g., `recolecta-demo`).

2) Register Android app
   - In the Firebase console, add an Android app. Use the app package name that matches your Flutter app (check `android/app/src/main/AndroidManifest.xml` or `android/app/build.gradle` `applicationId`).
   - Download `google-services.json` and place it in the Flutter project at `recolecta_app/android/app/google-services.json`.
   - Update `android/build.gradle` and `android/app/build.gradle` if needed (standard Flutter - Firebase steps). See https://firebase.flutter.dev/docs/overview/#installation

3) Register iOS app
   - Add an iOS app in Firebase, set the iOS bundle id from your Flutter project (check `ios/Runner.xcodeproj`), download `GoogleService-Info.plist` and add it to `recolecta_app/ios/Runner/GoogleService-Info.plist` (open Xcode and add to Runner target).

4) Enable Cloud Messaging
   - In Firebase console go to Cloud Messaging and ensure that your app is configured.

5) Obtain Admin SDK credentials (Backend)
   - In Firebase Console: Project Settings → Service Accounts → Generate new private key.
   - This downloads a JSON file like `recolecta-adminsdk-xxxxx.json`.
   - Copy that file to the backend secrets folder: `backend/secrets/firebase-adminsdk.json` (create `backend/secrets/` if missing).
   - IMPORTANT: do NOT commit this file to git. `backend/.gitignore` already excludes `secrets/`.

6) Set environment variable for backend
   - Set `FIREBASE_CREDENTIALS_PATH` to the path where you placed the JSON, example in `.env`:

```
FIREBASE_CREDENTIALS_PATH=backend/secrets/firebase-adminsdk.json
```

7) Flutter configuration (pubspec)
   - Add `firebase_core` and `firebase_messaging` to `recolecta_app/pubspec.yaml` and run `flutter pub get`.
   - Initialize Firebase in Flutter `main()` per docs: `WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp();`
   - Subscribe the citizen client to the route topic after confirming their `routeId`:

```dart
FirebaseMessaging.instance.subscribeToTopic('topic_RUTA-01');
```

8) Testing push (backend)
   - The backend contains `app/services/notifications.py` which will use the Admin SDK if `FIREBASE_CREDENTIALS_PATH` is set and the file exists. Otherwise it falls back to a mock that prints messages.
   - Start backend and trigger `POST /simulate/tick` to see mock pushes or real pushes if Admin SDK is configured.

9) Security note
   - Keep the Admin SDK JSON secret. Use environment-managed secrets in production (Cloud Run secret manager, etc.).
