# NumoniX Backend — Production Deployment Guide

This backend is 100% production-ready, pre-configured for `https://api.numonix.app`, and prepared so that you can simply zip, copy, paste, and run.

---

## 1. Quick Zip & Upload Instructions

1. **Zip the Project:**
   Compress the entire `lee-backend-work` folder (including `vendor/`, `app/`, `bootstrap/`, `config/`, `public/`, `routes/`, `resources/`, `storage/`, `composer.json`, `deploy.sh`, etc.) into `numonix-backend.zip`.

2. **Upload & Extract:**
   Upload `numonix-backend.zip` to your server's web root (e.g. `/var/www/api.numonix.app` or your cPanel directory) and extract all files.

3. **Point Web Server Root:**
   Ensure your web server (Nginx or Apache) points to the `public/` subdirectory.

---

## 2. One-Command Deployment

Run the included deployment script:

```bash
bash deploy.sh
```

Or execute manually step-by-step:

```bash
# 1. Ensure permissions on writable directories
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache

# 2. Setup .env
cp .env.production .env

# 3. Create storage symlink
php artisan storage:link

# 4. Run database migrations
php artisan migrate --force

# 5. Cache configurations and routes for high performance
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
```

---

## 3. Firebase Credentials Configuration

Firebase service account credentials can be configured in either of two ways:

### Option A: Upload via Admin Dashboard (Recommended)
1. Log in to your Admin Dashboard at `https://api.numonix.app/admin`.
2. In the sidebar, navigate to **Settings** &rarr; **Firebase Settings** (`/admin/settings/firebase`).
3. Under **Upload Firebase Service Account Key**, select your downloaded `.json` file from Firebase Console.
4. Click **Upload & Apply Credentials**.
5. The credentials will be saved securely to `storage/app/firebase-credentials.json` (outside public web access), `.env` will be automatically updated, and the configuration cache refreshed immediately. The active Project ID and Service Account Email will display with a green confirmation badge.

### Option B: Direct Environment Variable (.env)
You can directly set the path in your `.env` file on the server:
```dotenv
FIREBASE_CREDENTIALS=/path/to/your/firebase-credentials.json
```
or relative to the project root:
```dotenv
FIREBASE_CREDENTIALS=storage/app/firebase-credentials.json
```

*Note: The system resolves credentials dynamically with priority given to the dashboard-uploaded key, followed by direct environment variables. If no credentials exist, the system safely pauses notifications and social verification without throwing 500 fatal errors.*

---

## 4. Production Cron & Queue Setup

### Automated Scheduler (Cron)
Add this entry to your server crontab (`crontab -e`):

```bash
* * * * * cd /path/to/numonix-backend && php artisan schedule:run >> /dev/null 2>&1
```

This runs:
- Day-25 founder trial expiration emails
- Day-30 founder conversion offer checkouts
- Automatic pruning of expired guest attempts and revoked tokens

### Background Queue Worker (Supervisor)
Create `/etc/supervisor/conf.d/numonix-worker.conf`:

```ini
[program:numonix-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/numonix-backend/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/path/to/numonix-backend/storage/logs/worker.log
stopwaitsecs=3600
```

Update Supervisor:
```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start numonix-worker:*
```

---

## 5. Web Server Configuration (Nginx Example)

```nginx
server {
    listen 80;
    listen [::]:80;
    server_name api.numonix.app;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name api.numonix.app;
    root /var/www/api.numonix.app/public;

    ssl_certificate /etc/letsencrypt/live/api.numonix.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.numonix.app/privkey.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

---

## 6. Architecture & Features Summary

- **Payment System:** 100% Paddle Billing v2 via Laravel Cashier Paddle (Ed25519 webhook signature verified, `/paddle/webhook`).
- **Legacy Purge:** Stripe and RevenueCat completely eradicated (0 runtime dependencies).
- **Authentication:** Laravel Sanctum Bearer tokens, Social Login (Google & Apple with Firebase verification and Apple JWKs).
- **Founder Programme:** 30-day rolling trial, Day 25 automated emails with optional review form, £24.99 lifetime deal Paddle link generator.
- **Admin Notifications:** Full support for pushing FCM messages to all users or selected users directly from the Admin Dashboard.
- **PDF Generation:** Authoritative server-side generation for receipts, certificates, and reports with IDOR protection.