# Firebase Chat Structure

Firebase Firestore will be used strictly for real-time messaging between Customers and Vendors. 
Our Native PHP MySQL backend handles the **Chat References** (the `chat_references` table) to connect a Chat ID to the respective users, contracts, or quotation requests.

## Collection: `chats`
Represents a conversation room.

**Document ID**: `{firebase_chat_id}` (e.g. `customer_1_vendor_5_maid_12`)

### Schema:
```json
{
  "customer_id": 1,
  "vendor_id": 5,
  "maid_id": 12,
  "quotation_request_id": null,
  "contract_id": null,
  "participants": ["customer_1", "vendor_5"],
  "last_message": "Hello, is she available?",
  "last_message_type": "text",
  "last_message_at": "timestamp",
  "unread": {
    "customer_1": 0,
    "vendor_5": 1
  },
  "created_at": "timestamp",
  "updated_at": "timestamp"
}
```

## Collection: `messages` (Subcollection under `chats/{chatId}`)
Represents individual messages within a chat.

**Document ID**: Auto-generated by Firestore

### Schema:
```json
{
  "sender_type": "customer",
  "sender_id": 1,
  "message": "Hello, is she available?",
  "type": "text",
  "file_url": null,
  "created_at": "timestamp",
  "is_deleted": false
}
```

## Backend Workflow
1. The mobile app calls the REST API `POST /api/customer/chats/create-or-get` passing `vendor_id`, `maid_id`, etc.
2. The PHP backend checks the MySQL `chat_references` table.
3. If it exists, returns the existing `firebase_chat_id`.
4. If it doesn't exist, PHP generates a unique `firebase_chat_id`, saves it to MySQL, and returns it.
5. The mobile app uses this `firebase_chat_id` to connect to Firestore and send/listen to messages in real-time.
