# Chatbot Service - Docker Setup Guide

## Prerequisites

- Docker Desktop installed and running
- Docker Compose installed (comes with Docker Desktop)

## Quick Start

### 1. Configure Environment Variables

The `.env.docker` file contains all configuration. **Required** settings:

```bash
# Add your AI API keys (at least one required)
OPENAI_API_KEY=sk-...
# OR
ANTHROPIC_API_KEY=sk-ant-...
```

Optional: Configure email, storage, OAuth, etc.

### 2. Start All Services

```bash
# Start all services (PostgreSQL, Redis, Backend, Celery)
docker-compose up -d

# View logs
docker-compose logs -f

# View specific service logs
docker-compose logs -f backend
docker-compose logs -f celery-worker
```

### 3. Initialize Database

```bash
# Run database migrations (if you have alembic migrations)
docker-compose exec backend alembic upgrade head

# Or connect to PostgreSQL directly
docker-compose exec postgres psql -U postgres -d ai_chatbot
```

### 4. Access Services

- **Backend API**: http://localhost:8000
- **API Docs**: http://localhost:8000/docs
- **PostgreSQL**: localhost:5432 (user: postgres, password: password)
- **Redis**: localhost:6379

## Service Architecture

```
┌─────────────────┐
│  Client Portal  │
│   (Frontend)    │
└────────┬────────┘
         │
         ▼
┌─────────────────┐      ┌──────────┐
│  Backend API    │◄────►│  Redis   │
│  (FastAPI)      │      └──────────┘
└────────┬────────┘
         │
         ▼
┌─────────────────┐      ┌──────────────┐
│   PostgreSQL    │      │ Celery Worker│
│   (Database)    │      │ (Background) │
└─────────────────┘      └──────────────┘
                         ┌──────────────┐
                         │ Celery Beat  │
                         │ (Scheduler)  │
                         └──────────────┘
```

## Individual Service Commands

### Backend Only
```bash
docker-compose up -d postgres redis backend
```

### With Background Workers
```bash
docker-compose up -d postgres redis backend celery-worker celery-beat
```

### Stop Services
```bash
# Stop all services
docker-compose down

# Stop and remove volumes (WARNING: deletes data)
docker-compose down -v
```

### Rebuild Services
```bash
# Rebuild after code changes
docker-compose build backend

# Rebuild and restart
docker-compose up -d --build
```

## Development Tips

### Live Reload
The backend service has `--reload` enabled. Any changes to Python files will automatically restart the server.

### View Database
```bash
# Connect to PostgreSQL
docker-compose exec postgres psql -U postgres -d ai_chatbot

# List tables
\dt

# Query data
SELECT * FROM clients;
```

### View Redis
```bash
# Connect to Redis CLI
docker-compose exec redis redis-cli

# View keys
KEYS *

# Get value
GET key_name
```

### Check Service Health
```bash
# View running containers
docker-compose ps

# Check service health
docker-compose exec backend curl http://localhost:8000/health

# Check logs for errors
docker-compose logs --tail=100 backend
```

## Troubleshooting

### Port Already in Use
If ports 8000, 5432, or 6379 are in use:
```bash
# Find process using port
netstat -ano | findstr :8000

# Change ports in docker-compose.yml
ports:
  - "8001:8000"  # Change 8001 to any available port
```

### Database Connection Errors
```bash
# Wait for database to be ready
docker-compose exec postgres pg_isready -U postgres

# Reset database
docker-compose down -v
docker-compose up -d
```

### Build Errors
```bash
# Clear Docker cache
docker-compose build --no-cache backend

# Remove old images
docker system prune -a
```

## Production Deployment

For production, update:

1. **Security**:
   - Change `SECRET_KEY` to strong random string
   - Update database passwords
   - Set `DEBUG=False`

2. **Environment**:
   - Copy `.env.docker` to `.env.production`
   - Update all API keys and credentials
   - Configure proper CORS origins

3. **Run**:
   ```bash
   docker-compose --env-file .env.production up -d
   ```

## Monitoring

### View Resource Usage
```bash
docker stats
```

### Celery Task Monitoring
```bash
# View active tasks
docker-compose exec backend celery -A celery_worker.celery_app inspect active

# View scheduled tasks
docker-compose exec backend celery -A celery_beat.celery_app inspect scheduled
```

## Backup & Restore

### Database Backup
```bash
docker-compose exec postgres pg_dump -U postgres ai_chatbot > backup.sql
```

### Database Restore
```bash
cat backup.sql | docker-compose exec -T postgres psql -U postgres ai_chatbot
```
