# Docker Integration Complete ✅

## What Was Set Up

### Docker Compose Configuration
The TaxCore Node.js service has been fully integrated into the Docker Compose stack:

```yaml
services:
  php-apache:       # Port 8080
  postgres:         # Port 5432
  redis:            # Port 6379
  taxcore-service:  # Port 3001 ⭐ NEW
```

### TaxCore Service Container

**Dockerfile:** `docker/taxcore-service/Dockerfile`
- Base: `node:18-alpine`
- Includes: `curl` for TaxCore API calls
- Mounts:
  - `./websocket-server` → `/usr/src/app` (source code)
  - `./FRCS_Certs_Install` → `/usr/src/app/certs` (certificates, read-only)
  - `./logs` → `/usr/src/logs` (log files)

**Features:**
- ✅ Automatic health checks every 30 seconds
- ✅ Restart policy: `unless-stopped`
- ✅ Connected to `taf-network` with other services
- ✅ Environment variables configurable via `.env` file

## How to Use

### Start All Services
```bash
# Simple command starts everything
docker-compose up --build -d
```

This single command:
1. Builds the TaxCore service Docker image
2. Starts PostgreSQL database
3. Starts Redis cache
4. Starts PHP/Apache web server
5. Starts TaxCore Node.js service
6. Connects all services via Docker network

### Verify Running
```bash
# Check all containers
docker-compose ps

# Test TaxCore
curl http://localhost:3001/health
```

### View Logs
```bash
# All services
docker-compose logs -f

# Just TaxCore
docker-compose logs -f taxcore-service
```

### Stop Everything
```bash
docker-compose down
```

## Architecture with Docker

```
┌─────────────────────────────────────────────────────────────┐
│                    Docker Network (taf-network)              │
│                                                              │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐  │
│  │  php-apache  │    │   postgres   │    │    redis     │  │
│  │   :8080      │    │   :5432      │    │   :6379      │  │
│  └──────┬───────┘    └──────────────┘    └──────────────┘  │
│         │                                                    │
│         │ http://taxcore-service:3001                       │
│         ▼                                                    │
│  ┌──────────────────────────────┐                           │
│  │   taxcore-service            │                           │
│  │   :3001                      │                           │
│  │                              │                           │
│  │  • mTLS certificates         │                           │
│  │  • Transforms payloads       │                           │
│  │  • Calls TaxCore API         │                           │
│  └──────────────┬───────────────┘                           │
│                 │                                            │
└─────────────────┼────────────────────────────────────────────┘
                  │
                  │ HTTPS with mTLS
                  ▼
         ┌──────────────────┐
         │  TaxCore API     │
         │  (FRCS)          │
         └──────────────────┘
```

## Browser Configuration

### Auto-Detection (Recommended) ⭐

The POS module **automatically detects** the environment and configures itself:

**No configuration needed!** 

When you access:
- `http://localhost:8080` (Docker) → Uses `http://localhost:3001`
- `http://192.168.1.100:8080` (Docker on network) → Uses `http://192.168.1.100:3001`
- `http://localhost/TAF` (Development) → Uses `http://localhost:3001`

### Manual Override (If Needed)

**Direct Mode (Default):**
```javascript
localStorage.setItem('taxcore_direct', 'true');
localStorage.setItem('taxcore_service_url', 'http://localhost:3001');
```

**Flow:**
```
Browser → TaxCore service (port 3001) → TaxCore API
```

**Proxy Mode:**
```javascript
localStorage.setItem('taxcore_direct', 'false');
```

**Flow:**
```
Browser → PHP (port 8080) → TaxCore service (internal: taxcore-service:3001) → TaxCore API
```

**When to use Proxy Mode:**
- CORS restrictions
- Complex authentication requirements
- Need centralized logging through PHP
- Network security policies

**When to use Direct Mode (Default):**
- Better performance (one less hop)
- Simpler architecture
- Direct connection available
- TaxCore service port exposed

## Configuration Files

### .env or .env.docker
```bash
# TaxCore Configuration
TAXCORE_SERVICE_PORT=3001
TAXCORE_TAX_URL=https://api.sandbox.vms.frcs.org.fj/api
TAXCORE_VSDC_URL=http://devesdc.sandbox.vms.frcs.org.fj:8888/...
TAXCORE_PFX_FILE=LK2VRSH4-DeveloperAuthenticationCertificate.pfx
TAXCORE_PFX_PASSWORD=2CBP6HMW
TAXCORE_PIN_JSON=3840
TAXCORE_DEBUG=true

# Database
POSTGRES_USER=taf
POSTGRES_PASSWORD=TAF123*
POSTGRES_DB=tafdb
```

### docker-compose.yaml
Already configured with:
- Service definitions
- Volume mounts
- Network configuration
- Health checks
- Environment variables

## Development Workflow

### 1. Start Services
```bash
docker-compose up --build -d
```

### 2. Make Code Changes
Edit files in:
- `websocket-server/taxcore-service.js`
- `FrontEnd/js/modules/POS/POS.js`
- `api/controllers/PosController.php`

### 3. Apply Changes
```bash
# Restart TaxCore service
docker-compose restart taxcore-service

# Or rebuild if dependencies changed
docker-compose up --build -d taxcore-service

# Restart PHP if changed
docker-compose restart php-apache
```

### 4. View Logs
```bash
docker-compose logs -f taxcore-service
```

### 5. Test
- Open browser: http://localhost:8080
- Complete a sale
- Check logs for debugging

## Production Deployment

### 1. Update Environment
```bash
cp .env.docker .env.production

# Edit .env.production
TAXCORE_DEBUG=false
APP_ENV=production
TAXCORE_TAX_URL=https://production-url/api
```

### 2. Use Production Compose File
```bash
docker-compose -f docker-compose.yaml -f docker-compose.prod.yaml up -d
```

### 3. Enable Auto-Restart
Already configured with `restart: unless-stopped`

### 4. Set Up Monitoring
- Use `docker-compose ps` for health checks
- Monitor logs with logging driver
- Set up alerts for service failures

## Advantages of Docker Setup

✅ **Single Command Start**: `docker-compose up --build -d`
✅ **Consistent Environment**: Same setup across dev, staging, production
✅ **Isolated Services**: Each service in its own container
✅ **Easy Scaling**: Can run multiple instances
✅ **Simple Networking**: Internal DNS resolution
✅ **Volume Persistence**: Database data persists
✅ **Health Monitoring**: Automatic health checks
✅ **Log Management**: Centralized logging
✅ **Easy Updates**: Just rebuild and restart

## Documentation

- **Quick Start**: `DOCKER_QUICKSTART_TAXCORE.md` (this directory)
- **Full Docker Guide**: `docs/TaxCore_Docker_Setup.md`
- **Architecture**: `docs/TaxCore_Current_Architecture.md`
- **Implementation**: `docs/TaxCore_Implementation_Summary.md`
- **Service Docs**: `websocket-server/TAXCORE_SERVICE.md`

## Common Commands Reference

```bash
# Start everything
docker-compose up --build -d

# Check status
docker-compose ps

# View all logs
docker-compose logs -f

# View TaxCore logs only
docker-compose logs -f taxcore-service

# Restart a service
docker-compose restart taxcore-service

# Stop everything
docker-compose down

# Stop and remove data (⚠️)
docker-compose down -v

# Execute command in container
docker-compose exec taxcore-service sh
docker-compose exec php-apache bash

# Test connections
docker-compose exec php-apache curl http://taxcore-service:3001/health

# Check certificate
docker-compose exec taxcore-service ls -la /usr/src/app/certs/
```

## Troubleshooting

### Container Won't Start
```bash
docker-compose logs taxcore-service
```

### Certificate Issues
```bash
# Verify certificate exists
ls -la FRCS_Certs_Install/*.pfx

# Check in container
docker-compose exec taxcore-service ls -la /usr/src/app/certs/
```

### Network Issues
```bash
# Test internal connectivity
docker-compose exec php-apache curl http://taxcore-service:3001/health

# Inspect network
docker network inspect taf_taf-network
```

### Port Conflicts
```bash
# Find what's using the port
lsof -i :3001

# Or change port in docker-compose.yaml
```

## Success Checklist

- [x] Docker Compose configured
- [x] TaxCore service Dockerfile created
- [x] Volume mounts configured
- [x] Health checks enabled
- [x] Environment variables set
- [x] Network configured
- [x] Documentation created
- [x] Restart policy set

## Next Steps

1. **Test the Docker setup**:
   ```bash
   docker-compose up --build -d
   docker-compose ps
   curl http://localhost:3001/health
   ```

2. **Complete a sale**:
   - Open http://localhost:8080
   - Navigate to POS
   - Complete a test sale
   - Verify in logs

3. **Deploy to staging**:
   - Use production environment variables
   - Test with production certificates
   - Verify all functionality

4. **Monitor and maintain**:
   - Set up log rotation
   - Configure monitoring alerts
   - Document operational procedures

---

**Docker Integration Complete!** 🐳

**Start with:** `docker-compose up --build -d`  
**Test with:** `curl http://localhost:3001/health`  
**Access at:** `http://localhost:8080`

All services (PHP, PostgreSQL, Redis, TaxCore) now start together with a single command! 🚀
