# Zufiti – Deployment Guide

## Quick Start (Docker)

```bash
cp .env.example .env       # Edit all values
docker compose -f docker/docker-compose.yml up -d
```

## Ubuntu + NGINX (Production)

### 1. Prerequisites
```bash
sudo apt update && sudo apt install -y nodejs npm mariadb-server nginx
sudo npm install -g pm2
```

### 2. Database
```bash
sudo mysql -u root -e "
  CREATE DATABASE IF NOT EXISTS zufiti CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  CREATE USER IF NOT EXISTS 'zufiti_user'@'localhost' IDENTIFIED BY 'CHANGE_ME';
  GRANT ALL ON zufiti.* TO 'zufiti_user'@'localhost';
  FLUSH PRIVILEGES;"
```

### 3. App
```bash
cd backend && npm ci --only=production
cp ../.env.example .env     # Edit values
npm run migrate             # Create tables
npm run seed                # Seed demo data
pm2 start server.js --name zufiti -i max
pm2 save && pm2 startup
```

### 4. NGINX
```bash
sudo cp docker/nginx.conf /etc/nginx/nginx.conf
sudo nginx -t && sudo systemctl reload nginx
```

## Environment Variables

See `.env.example` for all required values. Critical:

| Variable | Description |
|---|---|
| `JWT_SECRET` | 64+ char random string |
| `JWT_REFRESH_SECRET` | 64+ char random string |
| `DB_PASSWORD` | Strong database password |
| `PLATFORM_FEE_PERCENT` | Your revenue cut (default 10%) |

## Seed Accounts

After seeding, use:
- **Admin**: admin@zufiti.com / password123
- **Owner**: owner@zufiti.com / password123  
- **Advertiser**: advertiser@zufiti.com / password123

## API Documentation

Base URL: `http://your-domain/api/v1`

All authenticated routes require: `Authorization: Bearer <access_token>`

### Auth
- `POST /auth/register` — Create account
- `POST /auth/login` — Login → returns `accessToken` + `refreshToken`
- `POST /auth/refresh` — Refresh tokens
- `POST /auth/logout` — Revoke refresh token

### Properties
- `GET /properties/search?lat=&lng=&radius=` — Geo search
- `GET /properties/:id` — Property detail + slots
- `POST /properties` — Create (owner)
- `POST /properties/:id/images` — Upload images

### Bookings
- `GET /bookings/quote?slotId=&startDate=&endDate=` — Price quote
- `POST /bookings` — Create booking
- `POST /bookings/:id/approve` — Approve (owner)
- `POST /bookings/:id/cancel` — Cancel

### Auctions
- `GET /auctions/live` — Live auctions
- `POST /auctions` — Create auction (owner)
- `POST /auctions/:id/bid` — Place bid

### Programmatic
- `POST /programmatic/buy` — Auto-buy slots
- `POST /programmatic/campaigns` — Create campaign
- `GET /programmatic/campaigns` — List campaigns

### Analytics
- `GET /analytics/owner` — Owner dashboard
- `GET /analytics/advertiser` — Advertiser dashboard
- `GET /analytics/admin` — Platform metrics

### WebSocket
Connect: `ws://your-domain/ws?token=<access_token>`

Messages:
```json
{ "type": "join_auction", "auctionId": "..." }
{ "type": "place_bid", "auctionId": "...", "amount": 150 }
```

Events received:
```json
{ "type": "bid_placed", "auctionId": "...", "amount": 150, "isBuyNow": false }
{ "type": "auction_ended", "auctionId": "...", "reason": "buy_now" }
{ "type": "surge_pricing", "slotId": "...", "surgeMultiplier": 1.8 }
```
