version: '3.8'

services:
  # Main WhatsApp Bot Application
  pamsika-bot:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        BUILD_DATE: ${BUILD_DATE:-2024-01-01}
        VCS_REF: ${VCS_REF:-main}
        VERSION: ${VERSION:-1.0.0}
    container_name: pamsika-whatsapp-bot
    restart: unless-stopped
    environment:
      - NODE_ENV=${NODE_ENV:-production}
      - PORT=3003
      - API_KEY=${API_KEY}
      - JWT_SECRET=${JWT_SECRET}
      - LOG_LEVEL=${LOG_LEVEL:-info}
      - PAMSIKA_API_URL=${PAMSIKA_API_URL:-https://test.pamsikaonline.com/api}
      - PAMSIKA_IMAGE_BASE_URL=${PAMSIKA_IMAGE_BASE_URL:-https://test.pamsikaonline.com/images/products/}
      - COMPANY_SUPPORT_PHONE=${COMPANY_SUPPORT_PHONE:-+265995684047}
      - COMPANY_EMAIL=${COMPANY_EMAIL:-inquiries@pamsikaonline.com}
      - COMPANY_WEBSITE=${COMPANY_WEBSITE:-https://pamsikaonline.com}
      - REDIS_URL=redis://redis:6379
      - DATABASE_URL=postgresql://${POSTGRES_USER:-pamsika}:${POSTGRES_PASSWORD:-password}@postgres:5432/${POSTGRES_DB:-pamsika_bot}
    ports:
      - "${HOST_PORT:-3003}:3003"
    volumes:
      - ./logs:/app/logs:rw
      - ./sessions:/app/sessions:rw
      - ./users:/app/users:rw
      - ./media:/app/media:rw
    depends_on:
      - redis
      - postgres
    networks:
      - pamsika-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3003/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.pamsika-bot.rule=Host(`${DOMAIN:-localhost}`)"
      - "traefik.http.services.pamsika-bot.loadbalancer.server.port=3003"

  # Redis for message queuing and caching
  redis:
    image: redis:7-alpine
    container_name: pamsika-redis
    restart: unless-stopped
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-}
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
    volumes:
      - redis-data:/data
    networks:
      - pamsika-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # PostgreSQL for persistent data storage
  postgres:
    image: postgres:15-alpine
    container_name: pamsika-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_DB=${POSTGRES_DB:-pamsika_bot}
      - POSTGRES_USER=${POSTGRES_USER:-pamsika}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
      - POSTGRES_INITDB_ARGS=--encoding=UTF-8 --lc-collate=C --lc-ctype=C
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
    networks:
      - pamsika-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-pamsika} -d ${POSTGRES_DB:-pamsika_bot}"]
      interval: 30s
      timeout: 10s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Nginx reverse proxy (optional)
  nginx:
    image: nginx:alpine
    container_name: pamsika-nginx
    restart: unless-stopped
    ports:
      - "${NGINX_HTTP_PORT:-80}:80"
      - "${NGINX_HTTPS_PORT:-443}:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - ./logs/nginx:/var/log/nginx:rw
    depends_on:
      - pamsika-bot
    networks:
      - pamsika-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    profiles:
      - nginx
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Monitoring with Prometheus (optional)
  prometheus:
    image: prom/prometheus:latest
    container_name: pamsika-prometheus
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    ports:
      - "${PROMETHEUS_PORT:-9090}:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    networks:
      - pamsika-network
    profiles:
      - monitoring
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Grafana for dashboards (optional)
  grafana:
    image: grafana/grafana:latest
    container_name: pamsika-grafana
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
      - GF_INSTALL_PLUGINS=redis-datasource
    ports:
      - "${GRAFANA_PORT:-3000}:3000"
    volumes:
      - grafana-data:/var/lib/grafana
      - ./monitoring/grafana:/etc/grafana/provisioning:ro
    depends_on:
      - prometheus
    networks:
      - pamsika-network
    profiles:
      - monitoring
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Log aggregation with Loki (optional)
  loki:
    image: grafana/loki:latest
    container_name: pamsika-loki
    restart: unless-stopped
    command: -config.file=/etc/loki/local-config.yaml
    ports:
      - "${LOKI_PORT:-3100}:3100"
    volumes:
      - ./monitoring/loki-config.yaml:/etc/loki/local-config.yaml:ro
      - loki-data:/loki
    networks:
      - pamsika-network
    profiles:
      - monitoring
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  postgres-data:
    driver: local
  redis-data:
    driver: local
  prometheus-data:
    driver: local
  grafana-data:
    driver: local
  loki-data:
    driver: local

networks:
  pamsika-network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.20.0.0/16

# Development override
# To use: docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
---
version: '3.8'

# Development overrides
x-development: &development-overrides
  volumes:
    - .:/app:rw
    - /app/node_modules
  environment:
    - NODE_ENV=development
    - LOG_LEVEL=debug
  command: ["npm", "run", "dev"]

services:
  pamsika-bot:
    <<: *development-overrides
    build:
      target: builder
    ports:
      - "3003:3003"
      - "9229:9229"  # Debug port
