#############################
# Ports Exposed
# 4000 - Imex Node API 
# 4556 - LocalStack (Local AWS)
# 3333 - SocketIO Admin-UI (Optional)
# 3334 - Redis-Insights (Optional)
#############################

services:
  
  # Redis Node 1 
  redis-node-1:
    build:
      context: ./redis
    container_name: redis-node-1
    hostname: redis-node-1
    restart: unless-stopped
    networks:
      - redis-cluster-net
    volumes:
      - redis-node-1-data:/data
      - redis-lock:/redis-lock
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 10

  # Redis Node 2  
  redis-node-2:
    build:
      context: ./redis
    container_name: redis-node-2
    hostname: redis-node-2
    restart: unless-stopped
    networks:
      - redis-cluster-net
    volumes:
      - redis-node-2-data:/data
      - redis-lock:/redis-lock
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 10

  # Redis Node 3
  redis-node-3:
    build:
      context: ./redis
    container_name: redis-node-3
    hostname: redis-node-3
    restart: unless-stopped
    networks:
      - redis-cluster-net
    volumes:
      - redis-node-3-data:/data
      - redis-lock:/redis-lock
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 10

  # LocalStack: Used to emulate AWS services locally, currently setup for SES
  # Notes: Set the ENV Debug to 1 for additional logging
  localstack:
    image: localstack/localstack
    container_name: localstack
    hostname: localstack
    networks:
      - redis-cluster-net
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - SERVICES=s3,ses,secretsmanager,cloudwatch,logs
      - DEBUG=0
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
      - AWS_DEFAULT_REGION=ca-central-1
      - EXTRA_CORS_ALLOWED_HEADERS=Authorization,Content-Type
      - EXTRA_CORS_ALLOWED_ORIGINS=*
      - EXTRA_CORS_EXPOSE_HEADERS=Authorization,Content-Type
    ports:
      - "4566:4566"
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:4566/_localstack/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  # AWS-CLI - Used in conjunction with LocalStack to set required permission to send emails
  aws-cli:
    image: amazon/aws-cli
    container_name: aws-cli
    hostname: aws-cli
    networks:
      - redis-cluster-net
    depends_on:
      localstack:
        condition: service_healthy
    volumes:
      - './localstack:/tmp/localstack'
      - './certs:/tmp/certs'

    environment:
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
      - AWS_DEFAULT_REGION=ca-central-1
    entrypoint: /bin/sh -c
    command: >
      "
        aws --endpoint-url=http://localstack:4566 ses verify-domain-identity --domain imex.online --region ca-central-1
        aws --endpoint-url=http://localstack:4566 ses verify-email-identity --email-address noreply@imex.online --region ca-central-1
        aws --endpoint-url=http://localstack:4566 secretsmanager create-secret --name CHATTER_PRIVATE_KEY --secret-string file:///tmp/certs/io-ftp-test.key
        aws --endpoint-url=http://localstack:4566 logs create-log-group --log-group-name development --region ca-central-1
        aws --endpoint-url=http://localstack:4566 s3api create-bucket --bucket imex-large-log --create-bucket-configuration LocationConstraint=ca-central-1
        aws --endpoint-url=http://localstack:4566 s3api create-bucket --bucket imex-job-totals --create-bucket-configuration LocationConstraint=ca-central-1
        aws --endpoint-url=http://localstack:4566 s3api create-bucket --bucket parts-estimates --create-bucket-configuration LocationConstraint=ca-central-1
      "
  # Node App: The Main IMEX API
  node-app:
    build:
      context: .
    container_name: node-app
    hostname: imex-api
    networks:
      - redis-cluster-net
    env_file:
      - .env.development
    depends_on:
      redis-node-1:
        condition: service_healthy
      redis-node-2:
        condition: service_healthy
      redis-node-3:
        condition: service_healthy
      localstack:
        condition: service_healthy
      aws-cli:
        condition: service_completed_successfully
    ports:
      - "4000:4000"
      - "9229:9229"
    volumes:
      - .:/app
      - node-app-npm-cache:/app/node_modules

#  ## Optional Container to Observe SocketIO data
#  socketio-admin-ui:
#    image: maitrungduc1410/socket.io-admin-ui
#    container_name: socketio-admin-ui
#    networks:
#      - redis-cluster-net
#    ports:
#      - "3333:80"

#  ##Optional Container to Observe Redis Cluster Data
#  redis-insight:
#    image: redislabs/redisinsight:latest
#    container_name: redis-insight
#    hostname: redis-insight
#    restart: unless-stopped
#    ports:
#      - "3334:5540"
#    networks:
#      - redis-cluster-net
#    volumes:
#      - redis-insight-data:/db

# ##Optional Container for SFTP/SSH Server for testing
#   ssh-sftp-server: 
#     image: atmoz/sftp:alpine  # Using an image with SFTP support
#     container_name: ssh-sftp-server
#     hostname: ssh-sftp-server
#     networks:
#       - redis-cluster-net
#     ports:
#       - "2222:22"  # Expose port 22 for SSH/SFTP (mapped to 2222 on the host)
#     volumes:
#       - ./certs/io-ftp-test.key.pub:/home/user/.ssh/keys/io-ftp-test.key.pub:ro  # Mount the SSH public key as authorized_keys
#       - ./upload:/home/user/upload  # Mount a local directory for SFTP uploads
#     environment:
#       # - SFTP_USERS=user::1000::upload
#       - SFTP_USERS=user:password:1000::upload
#     command: >
#       /bin/sh -c "
#         chmod -R 007 /home/user/upload &&
#         echo 'Match User user' >> /etc/ssh/sshd_config &&
#         sed -i -e 's#ForceCommand internal-sftp#ForceCommand internal-sftp -d /upload#' /etc/ssh/sshd_config &&
#         /usr/sbin/sshd -D
#       "

networks:
  redis-cluster-net:
    driver: bridge

volumes:
  node-app-npm-cache:
  redis-node-1-data:
  redis-node-2-data:
  redis-node-3-data:
  redis-lock:
  redis-insight-data:
