# Docker Deployment Guide

This guide covers setting up Black Canyon Tickets with optimized Docker deployment for your Astro application.

## Overview

- **Astro App**: Rebuilt on each Git deployment using existing Supabase backend
- **Database**: Uses your existing hosted Supabase PostgreSQL + Auth
- **NGINX**: Reverse proxy to Astro application
- **Certbot**: SSL certificates (existing setup)

## Server Setup (One-Time)

### 1. Install Dependencies

```bash
# Update system
sudo apt update && sudo apt upgrade -y

# Install Docker & Docker Compose
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER

# Log out and back in for Docker group to take effect
```

### 2. Clone Repository

```bash
cd /var/www
sudo git clone https://github.com/your-org/bct-whitelabel.git
sudo chown -R $USER:$USER bct-whitelabel
cd bct-whitelabel
```

### 3. Configure Environment

Your application uses Supabase (hosted) so just ensure your `.env` file has:

```bash
# Supabase (your existing hosted database)
PUBLIC_SUPABASE_URL=https://zctjaivtfyfxokfaemek.supabase.co
PUBLIC_SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key

# Stripe
STRIPE_PUBLISHABLE_KEY=pk_...
STRIPE_SECRET_KEY=sk_...
STRIPE_WEBHOOK_SECRET=whsec_...

# Email
RESEND_API_KEY=re_...

# Monitoring
SENTRY_DSN=https://...
SENTRY_RELEASE=production
```

### 4. Configure NGINX

```bash
# Copy example configuration
sudo cp nginx-example.conf /etc/nginx/sites-available/blackcanyontickets

# Enable site
sudo ln -s /etc/nginx/sites-available/blackcanyontickets /etc/nginx/sites-enabled/

# Test configuration
sudo nginx -t
```

### 5. Setup SSL with Certbot

```bash
# Get SSL certificate (Certbot handles NGINX config automatically)
sudo certbot --nginx -d bct.crispygoat.com

# Reload NGINX with SSL
sudo systemctl reload nginx
```

### 6. Set Up Log Rotation

```bash
# Install log rotation
sudo cp logrotate-bct /etc/logrotate.d/bct

# Test log rotation
sudo logrotate -d /etc/logrotate.d/bct
```

## Git Deployment Script

Update your deployment script to rebuild only the Astro app:

### Simple Deploy Script

```bash
#!/bin/bash
set -e

echo "Deploying BCT Astro app..."

# Navigate to project directory
cd /var/www/bct-whitelabel

# Pull latest changes
git pull origin main

# Rebuild Astro app
npm run docker:astro:up

echo "Deployment complete!"
```

**Your Supabase database stays online** - no downtime for deployments!

## Daily Operations

### Check Service Status

```bash
# View running containers
docker ps

# Check logs
npm run docker:astro:logs

# Health checks
curl http://localhost:3000/api/health
```

### Restart Services

```bash
# Restart Astro app
npm run docker:astro:down
npm run docker:astro:up
```

### Service URLs

- **Main App**: https://bct.crispygoat.com
- **Admin Panel**: https://bct.crispygoat.com/admin
- **Health Check**: https://bct.crispygoat.com/api/health

## Available Commands

### Docker Commands
```bash
# Production deployment (Astro only)
npm run docker:astro:up        # Deploy Astro app
npm run docker:astro:down      # Stop Astro app  
npm run docker:astro:logs      # View Astro logs

# Production (pre-built image)
npm run docker:prod:up         # Deploy pre-built image
npm run docker:prod:down       # Stop production image

# Local development
npm run docker:dev             # Start development container
npm run docker:dev:build       # Start with rebuild
```

## Backup Strategy

### Supabase Backups

Since you're using hosted Supabase:
- **Automatic backups** are handled by Supabase
- **Point-in-time recovery** available through Supabase dashboard
- **Manual exports** can be done through Supabase SQL editor

### Application Backups

```bash
# Create backup script for logs and uploads
cat > backup-app.sh << 'EOF'
#!/bin/bash
BACKUP_DIR="/var/backups/bct"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

# Backup logs
tar -czf $BACKUP_DIR/logs_$DATE.tar.gz logs/

# Keep only last 7 days
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete

echo "Backup completed: $BACKUP_DIR"
EOF

chmod +x backup-app.sh

# Add to crontab for daily backups
echo "0 2 * * * /var/www/bct-whitelabel/backup-app.sh" | crontab -
```

## Troubleshooting

### Common Issues

1. **Container won't start**
   ```bash
   # Check logs
   docker logs bct-astro
   
   # Check environment variables
   env | grep SUPABASE
   ```

2. **NGINX proxy errors**
   ```bash
   # Test NGINX config
   sudo nginx -t
   
   # Check upstream connectivity
   curl http://localhost:3000/api/health
   ```

3. **SSL certificate issues**
   ```bash
   # Renew certificate
   sudo certbot renew
   
   # Check certificate status
   sudo certbot certificates
   ```

## Auto-Start Services on Boot

### Configure Docker Services to Auto-Start

```bash
# Create systemd service for Astro app
sudo tee /etc/systemd/system/bct-astro.service > /dev/null << 'EOF'
[Unit]
Description=BCT Astro Application
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/var/www/bct-whitelabel
ExecStart=/usr/bin/docker-compose -f docker-compose.astro.yml up -d
ExecStop=/usr/bin/docker-compose -f docker-compose.astro.yml down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target
EOF

# Enable and start the service
sudo systemctl enable bct-astro.service
sudo systemctl start bct-astro.service
```

### One-Command Deployment

Add this to your server for quick deployments:

```bash
# Create deployment alias
echo 'alias redeploy-bct="cd /var/www/bct-whitelabel && git pull && npm run docker:astro:up"' >> ~/.bashrc
source ~/.bashrc

# Now you can simply run:
redeploy-bct
```

## Monitoring

### Log Monitoring

```bash
# Real-time logs
tail -f /var/log/nginx/access.log
npm run docker:astro:logs -f

# Container stats
docker stats bct-astro

# Disk usage
docker system df
```

### Resource Monitoring

```bash
# Container resource usage
docker stats

# System resources
htop
df -h
```

This setup provides a robust, maintainable deployment pipeline where your Astro app can be updated frequently while your Supabase database remains stable and always available.