# Venuezy — Deployment & Operations Guide

## 1. Production Setup

### Environment

```bash
cp .env.production.example .env
php artisan key:generate
# Edit .env with real credentials
```

Key production values:
- `APP_ENV=production`, `APP_DEBUG=false`
- `CACHE_STORE=redis`, `QUEUE_CONNECTION=redis`, `SESSION_DRIVER=redis`
- `MIDTRANS_IS_PRODUCTION=true`
- `TRUSTED_PROXIES=*` (behind nginx/load balancer)

### Optimize Laravel

```bash
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
```

Or use the composer shortcut:

```bash
composer run deploy:optimize
```

### Storage Symlink

```bash
php artisan storage:link
```

Serves uploaded files from `storage/app/public` via `public/storage`.

### Queue Worker (Supervisor — recommended)

Copy `deploy/supervisor/venuezy-worker.conf` to `/etc/supervisor/conf.d/` and adjust paths:

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start venuezy-worker:*
sudo supervisorctl start venuezy-scheduler
```

**Cron alternative** (scheduler only — still need a queue worker):

```cron
* * * * * cd /var/www/venuezy && php artisan schedule:run >> /dev/null 2>&1
```

---

## 2. Performance

### Database Indexes

Run migrations (includes production indexes):

```bash
php artisan migrate --force
```

Composite indexes added for: `bookings`, `tenants`, `users`, `fields`, `reviews`.

### Caching (Redis)

Set in `.env`:

```
CACHE_STORE=redis
VENUE_CACHE_TTL=3600
FIELD_CACHE_TTL=1800
AMENITY_CACHE_TTL=86400
```

Cached: tenant detail, field detail, amenities list. Invalidated on update.

**When to move to Redis:** >100 concurrent users, or when database cache becomes a bottleneck.

### Rate Limiting

| Limiter | Default | Routes |
|---------|---------|--------|
| `api` | 60/min | All API routes |
| `booking` | 10/min | Create/cancel booking, availability check |
| `auth` | 20/min | Register, login |

Configure via `RATE_LIMIT_API`, `RATE_LIMIT_BOOKING`, `RATE_LIMIT_AUTH` in `.env`.

### Pagination

All list endpoints support `?per_page=N`. Amenities support `?paginate=1`.

---

## 3. Monitoring

### Log Channels

| Channel | File | Use |
|---------|------|-----|
| `daily` | `storage/logs/laravel.log` | General app logs |
| `queue` | `storage/logs/queue.log` | Queue jobs |
| `payment` | `storage/logs/payment.log` | Midtrans webhooks |
| `notification` | `storage/logs/notification.log` | Notifications |

Usage in code: `Log::channel('payment')->info(...)`.

### Sentry (Error Tracking)

```bash
composer require sentry/sentry-laravel
```

Set `SENTRY_LARAVEL_DSN` in `.env`. Auto-registers when DSN is set.

### Laravel Telescope (Local/Dev ONLY)

```bash
composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate
```

Never enable in production (`TELESCOPE_ENABLED=false`).

---

## 4. Backup

### Manual

```bash
php artisan backup:database
php artisan backup:storage
```

Backups stored in `storage/app/backups/`.

### Scheduled (already configured)

- Database: daily at 02:00
- Storage: weekly Sunday 03:00

Requires `schedule:run` cron or supervisor scheduler.

---

## 5. Security

| Item | Implementation |
|------|----------------|
| HTTPS | `URL::forceScheme('https')` in production + nginx redirect |
| Rate limiting | 60/min API, 10/min booking (see above) |
| SQL injection | Eloquent / Query Builder only — no raw user input in SQL |
| XSS | API returns JSON; Blade views use `{{ }}` auto-escaping |
| CSRF | Sanctum SPA + API token auth |
| Secrets | Never commit `.env`; use strong DB passwords |

---

## 6. Deployment Steps

### Shared Hosting (cPanel / Plesk)

1. Upload project via Git or FTP (exclude `node_modules`, `vendor`)
2. Point document root to `public/`
3. Create MySQL database, configure `.env`
4. Run via SSH or cPanel terminal:
   ```bash
   composer install --no-dev --optimize-autoloader
   php artisan migrate --force
   php artisan storage:link
   php artisan config:cache && php artisan route:cache
   ```
5. Build frontend locally, upload `booking-frontend/dist/` or serve separately
6. Set cron: `* * * * * php /path/to/artisan schedule:run`
7. **Limitation:** Shared hosting often lacks Redis and persistent queue workers — use `QUEUE_CONNECTION=database` and cron-based queue processing

### VPS Deployment

1. Provision Ubuntu 22.04+ with nginx, PHP 8.2, MySQL 8, Redis
2. Clone repo to `/var/www/venuezy`
3. Copy `deploy/nginx/venuezy.conf`, enable site, obtain SSL via certbot
4. Configure `.env` from `.env.production.example`
5. Run `scripts/deploy.sh`
6. Install supervisor config from `deploy/supervisor/`
7. Deploy frontend to CDN or nginx static block

### Docker (Optional)

```bash
cp .env.production.example .env
docker compose -f docker-compose.prod.yml up -d --build
docker compose -f docker-compose.prod.yml exec app php artisan migrate --force
docker compose -f docker-compose.prod.yml exec app php artisan storage:link
```

API available at `http://localhost:8080`.

### Rollback Strategy

**Simple (git-based):**

```bash
git checkout <previous-commit>
composer install --no-dev --optimize-autoloader
php artisan migrate --force   # only if migration is backward-compatible
php artisan config:cache && php artisan route:cache
php artisan queue:restart
```

**Release-based:**

```bash
./scripts/rollback.sh
```

**Database rollback:** Restore from `storage/app/backups/database/`:

```bash
gunzip -c storage/app/backups/database/db-YYYY-MM-DD-HHMMSS.sql.gz | mysql -u user -p venuezy
```

---

## 7. Scalability Notes

### When to move to Redis

- Cache/queue on database causes slow queries
- >50 concurrent booking requests
- Session sharing needed across multiple app servers

### When to move from Shared Hosting to VPS

- Need custom queue workers (Supervisor)
- Need Redis, scheduled tasks every minute
- Traffic exceeds shared hosting limits (~500 bookings/day)
- Require SSH, custom nginx config, WebSocket (future)

### When to add Load Balancing

- Single VPS CPU >70% sustained
- Need zero-downtime deploys
- >1000 concurrent users

Architecture: nginx load balancer → 2+ app servers (shared Redis + MySQL), S3 for storage, managed Redis (ElastiCache / DigitalOcean).

---

## Quick Reference

```bash
# Full production deploy
composer run deploy

# Health check
curl https://api.venuezy.com/up

# Clear caches (debugging)
php artisan optimize:clear

# Restart workers after deploy
php artisan queue:restart
```
