version: '3.8'

services:
  # MySQL Database Service
  db:
    image: mysql:8.0
    container_name: uniosunpay_db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword}
      MYSQL_DATABASE: ${DB_NAME:-uniosun_payment}
      MYSQL_USER: ${DB_USER:-uniosunpay}
      MYSQL_PASSWORD: ${DB_PASSWORD:-uniosunpay123}
    ports:
      - "${DB_PORT:-3306}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./mysql-init:/docker-entrypoint-initdb.d
    networks:
      - uniosunpay_network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$$MYSQL_ROOT_PASSWORD"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

  # Node.js Application Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: uniosunpay_app
    restart: unless-stopped
    ports:
      - "${PORT:-5000}:5000"
    environment:
      # Database Configuration
      DB_HOST: db
      DB_USER: ${DB_USER:-uniosunpay}
      DB_PASSWORD: ${DB_PASSWORD:-uniosunpay123}
      DB_NAME: ${DB_NAME:-uniosun_payment}
      
      # Server Configuration
      PORT: 5000
      NODE_ENV: ${NODE_ENV:-production}
      
      # JWT Configuration
      JWT_SECRET: ${JWT_SECRET}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}
      
      # Remita Payment Gateway Configuration
      REMITA_MERCHANT_ID: ${REMITA_MERCHANT_ID}
      REMITA_API_KEY: ${REMITA_API_KEY}
      REMITA_PUBLIC_KEY: ${REMITA_PUBLIC_KEY}
      REMITA_ENV: ${REMITA_ENV:-demo}
      
      # Rate Limiting
      RATE_LIMIT_WINDOW_MS: ${RATE_LIMIT_WINDOW_MS:-900000}
      RATE_LIMIT_MAX_REQUESTS: ${RATE_LIMIT_MAX_REQUESTS:-100}
    volumes:
      - ./uploads:/app/uploads
    depends_on:
      db:
        condition: service_healthy
    networks:
      - uniosunpay_network
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:5000/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

volumes:
  mysql_data:
    driver: local

networks:
  uniosunpay_network:
    driver: bridge

