# Firestore Database Setup

## Collection Name
`dev_device_fingerprints`

## Document Structure

Each developer gets one document in the collection. The document ID can be anything (e.g., "dev-1", "john-doe", etc.)

### Required Fields

| Field | Type | Description | Example |
|-------|------|-------------|---------|
| `device_fingerprint` | string | Developer's MAC address | `"88:F4:DA:62:51:F1"` |
| `createdAt` | timestamp | When device was registered | Firestore Timestamp |

### Duration Fields (Choose One)

**Option 1: Direct Expiration Date (String)**
| Field | Type | Description | Example |
|-------|------|-------------|---------|
| `expiresAt` | string | Date in MM/DD/YYYY format | `"12/17/2025"` |

**Option 2: Direct Expiration Date (Timestamp)**
| Field | Type | Description | Example |
|-------|------|-------------|---------|
| `expiresAt` | timestamp | Firestore timestamp | 30 days from now |

**Option 3: Duration in Days**
| Field | Type | Description | Example |
|-------|------|-------------|---------|
| `durationDays` | number | Days until expiration from createdAt | `30` (30 days) |

### Optional Fields

| Field | Type | Description |
|-------|------|-------------|
| `lastVerified` | timestamp | Auto-updated on each successful verification |

## Example Documents

### Example 1: Using String Date (Simplest)

```javascript
// Document ID: "dev-1"
{
  device_fingerprint: "88:F4:DA:62:51:F1",
  createdAt: Timestamp(2025-01-01 00:00:00),
  expiresAt: "12/17/2025",  // String date - Expires Dec 17, 2025
  lastVerified: Timestamp(2025-01-15 10:30:00)
}
```

### Example 2: Using Firestore Timestamp

```javascript
// Document ID: "dev-2"
{
  device_fingerprint: "AA:BB:CC:DD:EE:FF",
  createdAt: Timestamp(2025-01-01 00:00:00),
  expiresAt: Timestamp(2025-01-31 23:59:59),  // Expires Jan 31, 2025
  lastVerified: Timestamp(2025-01-15 10:30:00)
}
```

### Example 3: Using Duration Days

```javascript
// Document ID: "dev-3"
{
  device_fingerprint: "11:22:33:44:55:66",
  createdAt: Timestamp(2025-01-01 00:00:00),
  durationDays: 30,  // Will expire 30 days from createdAt
  lastVerified: Timestamp(2025-01-15 10:30:00)
}
```

### Example 4: Permanent Access (No Expiration)

```javascript
// Document ID: "dev-admin"
{
  device_fingerprint: "11:22:33:44:55:66",
  createdAt: Timestamp(2025-01-01 00:00:00)
  // No expiresAt or durationDays = permanent access
}
```

## How to Add a Developer in Firestore Console

1. Go to Firestore console
2. Navigate to collection: `dev_device_fingerprints`
3. Click "Add Document"
4. Set Document ID: `dev-1` (or any unique ID)
5. Add fields:
   ```
   Field: device_fingerprint
   Type: string
   Value: "88:F4:DA:62:51:F1"

   Field: createdAt
   Type: timestamp
   Value: (click timestamp icon, select now)

   Field: expiresAt
   Type: string
   Value: "12/17/2025"
   ```
6. Save

**Note:** You can also use timestamp type for expiresAt instead of string if you prefer.

## How to Calculate Expiration Date

If you want to give access for 30 days from today:

**Using JavaScript:**
```javascript
const now = new Date();
const expiresAt = new Date(now.getTime() + (30 * 24 * 60 * 60 * 1000)); // 30 days
console.log(expiresAt);
```

**In Firestore Console:**
- Click the timestamp field
- Select the date 30 days from today

## How to Create developer-allowlist.json

After creating the Firestore document, give the developer a JSON file with the document ID:

**If document ID is "dev-1":**

```json
"dev-1"
```

That's it! Just the document ID as a string.

## Verification Flow

1. Developer has `developer-allowlist.json` with content: `"dev-1"`
2. Developer runs: `npx @anisrh/core-bootstrap`
3. Package reads file → gets `"dev-1"`
4. Package gets MAC address → gets `"88:F4:DA:62:51:F1"`
5. Package sends to API: `{ developerId: "dev-1", deviceFingerprint: "88:F4:DA:62:51:F1" }`
6. API queries Firestore for document `dev_device_fingerprints/dev-1`
7. API checks:
   - ✅ MAC matches?
   - ✅ Not expired?
8. API responds: `{ allowed: true/false, message: "..." }`

## Common Durations

| Duration | Days |
|----------|------|
| 1 week | 7 |
| 2 weeks | 14 |
| 1 month | 30 |
| 3 months | 90 |
| 6 months | 180 |
| 1 year | 365 |
| Permanent | (don't set expiresAt or durationDays) |
