# Docker Compose configuration for Payment Service with Nginx
version: '3.8'

services:
  # Payment Service
  payment-service:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: payment-service
    restart: unless-stopped
    environment:
      - SERVER_HOST=0.0.0.0
      - SERVER_PORT=8080
      - GIN_MODE=release
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_USER=payment_user
      - DB_PASSWORD=${DB_PASSWORD:-secure_password_123}
      - DB_NAME=internet
      - DB_MAX_OPEN_CONNS=100
      - DB_MAX_IDLE_CONNS=10
      - DB_CONN_MAX_LIFETIME=3600
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=${REDIS_PASSWORD:-secure_redis_123}
      - REDIS_DB=0
      - JWT_SECRET=${JWT_SECRET:-your_jwt_secret_key_here}
      - JWT_EXPIRY=24h
      - LOG_LEVEL=info
      - LOG_FORMAT=json
      - RATE_LIMIT_REQUESTS=1000
      - RATE_LIMIT_WINDOW=60
      - CACHE_TTL_SECONDS=3600
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - payment-network
    volumes:
      - ./logs:/app/logs
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Nginx Reverse Proxy
  nginx:
    image: nginx:alpine
    container_name: payment-nginx
    restart: unless-stopped
    ports:
      - "9000:80"
      - "9443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - ./nginx/logs:/var/log/nginx
    depends_on:
      - payment-service
    networks:
      - payment-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: payment-mysql
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root_password_123}
      - MYSQL_DATABASE=internet
      - MYSQL_USER=payment_user
      - MYSQL_PASSWORD=${DB_PASSWORD:-secure_password_123}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./mysql/init:/docker-entrypoint-initdb.d:ro
      - ./mysql/conf:/etc/mysql/conf.d:ro
    ports:
      - "3307:3306"
    networks:
      - payment-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "payment_user", "-p${DB_PASSWORD:-secure_password_123}"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --max_connections=200
      --innodb_buffer_pool_size=1G
      --innodb_log_file_size=256M
      --innodb_log_buffer_size=16M
      --slow_query_log=1
      --long_query_time=2

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: payment-redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD:-secure_redis_123} --appendonly yes
    volumes:
      - redis_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
    ports:
      - "6379:6379"
    networks:
      - payment-network
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Prometheus Monitoring
  prometheus:
    image: prom/prometheus:latest
    container_name: payment-prometheus
    restart: unless-stopped
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    networks:
      - payment-network

  # Grafana Dashboard
  grafana:
    image: grafana/grafana:latest
    container_name: payment-grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin123}
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources:ro
    networks:
      - payment-network

volumes:
  mysql_data:
    driver: local
  redis_data:
    driver: local
  prometheus_data:
    driver: local
  grafana_data:
    driver: local

networks:
  payment-network:
    driver: bridge

