# TaxCore Node.js Service Dockerfile
# This container runs the TaxCore integration service for FRCS API communication

FROM node:18-alpine

# Install build dependencies for native modules (sqlite3) and curl for health checks
RUN apk add --no-cache \
    curl \
    python3 \
    make \
    g++ \
    gcc \
    libc-dev

# Set working directory
WORKDIR /usr/src/app

# Copy package files
COPY websocket-server/package*.json ./

# Install dependencies
RUN npm install --production

# Clean up build dependencies to reduce image size
RUN apk del python3 make g++ gcc libc-dev

# Copy application files
COPY websocket-server/taxcore-service.js ./
COPY websocket-server/logger.js ./

# Copy certificates directory (will be mounted as volume in production)
# Create directory for certificates
RUN mkdir -p /usr/src/app/certs

# Create logs directory (using /usr/src/logs to match docker-compose volume mount)
RUN mkdir -p /usr/src/logs

# Set environment variables with defaults
ENV 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/20f351f3-9b39-4c63-b9e0-d8a00b6e93fb/api \
    TAXCORE_PFX_PATH=/usr/src/app/certs/certificate.pfx \
    TAXCORE_PFX_PASSWORD=2CBP6HMW \
    TAXCORE_PIN_JSON=3840 \
    TAXCORE_DEBUG=true \
    NODE_ENV=production

# Expose the service port
EXPOSE 3001

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD curl -f http://localhost:3001/health || exit 1

# Run the service
CMD ["node", "taxcore-service.js"]
