version: "3.8"

services:
  db:
    image: mariadb:10.9
    container_name: db_hyungi_net
    restart: unless-stopped
    env_file:
      - ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    volumes:
      - db_data:/var/lib/mysql
      - ./migrations:/docker-entrypoint-initdb.d  # SQL 마이그레이션 자동 실행
    ports:
      - "3306:3306"  # 개발 시 외부 접속용 (운영 시 제거)
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: api_hyungi_net
    depends_on:
      db:
        condition: service_healthy  # DB가 준비된 후 시작
    restart: unless-stopped
    ports:
      - "${PORT:-3005}:3005"
    env_file:
      - ./.env
    environment:
      - NODE_ENV=production
    volumes:
      - ./public/img:/usr/src/app/public/img:ro
      - ./uploads:/usr/src/app/uploads
      - ./logs:/usr/src/app/logs  # 로그 파일 저장
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: pma_hyungi_net
    depends_on:
      - db
    restart: unless-stopped
    ports:
      - "18080:80"
    env_file:
      - ./.env
    environment:
      - PMA_HOST=${DB_HOST:-db}
      - PMA_USER=${DB_ROOT_USER:-root}
      - PMA_PASSWORD=${DB_ROOT_PASSWORD}
      - UPLOAD_LIMIT=50M

  # Redis 캐시 서버 (선택사항 - 세션 관리 및 속도 제한용)
  # redis:
  #   image: redis:7-alpine
  #   container_name: redis_hyungi_net
  #   restart: unless-stopped
  #   ports:
  #     - "6379:6379"
  #   volumes:
  #     - redis_data:/data
  #   command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-yourredispassword}

  # Nginx 리버스 프록시 (선택사항 - HTTPS 및 로드밸런싱용)
  # nginx:
  #   image: nginx:alpine
  #   container_name: nginx_hyungi_net
  #   restart: unless-stopped
  #   ports:
  #     - "80:80"
  #     - "443:443"
  #   volumes:
  #     - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
  #     - ./nginx/ssl:/etc/nginx/ssl:ro
  #   depends_on:
  #     - api

volumes:
  db_data:
    external: true
    name: 7a5a13668b77b18bc1efaf1811d09560aa3be0e722d782e8460cb74f37328d81  # 기존 볼륨명으로 연결
  # redis_data:  # Redis 사용 시 주석 해제

networks:
  default:
    name: hyungi_network