# Coolify Deployment Guide for Estate Platform

## Prerequisites

1. **Coolify installed** with Traefik proxy enabled
2. **Domain configured** pointing to your Coolify server
3. **Traefik network** must exist: `docker network create traefik`

## Deployment Steps

### 1. Set Environment Variables in Coolify

In your Coolify application settings, add these environment variables:

```env
# Application
APP_DOMAIN=your-domain.com
APP_BASE_URL=https://your-domain.com
NODE_ENV=production

# Database
POSTGRES_DB=estate_platform
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_secure_password_here
DATABASE_URL=postgresql://postgres:your_secure_password_here@postgres:5432/estate_platform

# Redis
REDIS_PASSWORD=your_redis_password_here
REDIS_URL=redis://:your_redis_password_here@redis:6379

# JWT (generate secure random strings)
JWT_SECRET=your_super_secret_jwt_key_here
JWT_REFRESH_SECRET=your_super_secret_refresh_key_here

# Email (optional - for email verification)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-email-password
EMAIL_FROM=noreply@your-domain.com

# Stripe (optional - for payments)
STRIPE_SECRET_KEY=sk_test_...
STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
```

### 2. Deploy to Coolify

#### Option A: Using Docker Compose (Recommended)

1. In Coolify, create a new **Docker Compose** application
2. Point it to your Git repository
3. Coolify will automatically detect `docker-compose.yml`
4. Make sure the `traefik` network exists:
   ```bash
   docker network create traefik
   ```
5. Deploy!

#### Option B: Using Dockerfile

1. In Coolify, create a new **Dockerfile** application
2. Point it to your Git repository
3. Set build context to root directory
4. Coolify will use the Dockerfile to build
5. Configure domain and SSL in Coolify UI

### 3. Initial Setup After Deployment

Run these commands in Coolify terminal or SSH:

```bash
# Navigate to your app directory
cd /data/coolify/applications/[your-app-id]

# Run database migrations
docker compose exec web npx prisma migrate deploy

# Seed initial data
docker compose exec web npm run db:seed
```

### 4. Access Your Application

- **Web**: https://your-domain.com
- **Admin Login**: admin@ywyw.com / Dev1234#
- **User Login**: cust@ywyw.com / Dev1234#

## Traefik Labels Explained

The docker-compose.yml includes these Traefik labels:

- `traefik.enable=true` - Enable Traefik for this service
- `traefik.http.routers.estate-platform.rule=Host(...)` - Route by domain
- `traefik.http.routers.estate-platform.entrypoints=websecure` - Use HTTPS
- `traefik.http.routers.estate-platform.tls.certresolver=letsencrypt` - Auto SSL
- `traefik.http.services.estate-platform.loadbalancer.server.port=3000` - Backend port

## Network Architecture

```
Internet → Traefik (reverse proxy) → web:3000
                                   ↓
                                postgres:5432
                                   ↓
                                redis:6379
```

- **traefik** network: External network for proxy access
- **internal** network: Private network for database/redis communication

## Troubleshooting

### Issue: Traefik network not found
```bash
docker network create traefik
```

### Issue: Can't connect to database
- Ensure DATABASE_URL uses service name `postgres` not `localhost`
- Check postgres container is healthy: `docker compose ps`

### Issue: Domain not resolving
- Verify DNS points to your Coolify server
- Check Traefik dashboard for routes
- Ensure APP_DOMAIN env variable is set correctly

### Issue: SSL certificate not working
- Wait 1-2 minutes for Let's Encrypt to provision
- Check Traefik logs: `docker logs traefik`
- Ensure ports 80 and 443 are open

## Coolify-Specific Configuration

The docker-compose.yml has been optimized for Coolify:
- ✅ Removed exposed ports (Traefik handles routing)
- ✅ Added Traefik labels for automatic SSL
- ✅ Removed problematic pgbouncer service
- ✅ Added network isolation (internal + traefik)
- ✅ Uses service names for internal communication

## Production Checklist

- [ ] Set strong passwords for POSTGRES_PASSWORD and REDIS_PASSWORD
- [ ] Configure custom APP_DOMAIN
- [ ] Set secure JWT_SECRET values
- [ ] Configure email SMTP settings
- [ ] Set up Stripe keys for payments
- [ ] Enable automatic backups in Coolify
- [ ] Configure monitoring/alerts
- [ ] Test database migrations
- [ ] Seed initial admin user
- [ ] Test SSL certificate renewal

## Support

For Coolify-specific issues, check:
- Coolify docs: https://coolify.io/docs
- Traefik docs: https://doc.traefik.io/traefik/
