services:
  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
      args:
        API_INTERNAL_URL: "${WEB_API_INTERNAL_URL:-http://api:7341}"
    ports:
      - "${WEB_PORT:-7340}:7340"
    environment:
      NODE_ENV: production
      PORT: "7340"
      API_INTERNAL_URL: "${WEB_API_INTERNAL_URL:-http://api:7341}"
    depends_on:
      - api
    restart: unless-stopped

  api:
    build:
      context: .
      dockerfile: apps/api/Dockerfile
    ports:
      - "${API_PORT:-7341}:7341"
    # admin 쪽 env 는 .env 값이 bcrypt($$)/난수(공백 포함) 등을 포함할 수 있어
    # compose 의 `${VAR}` 보간 경로를 타면 $ 재치환이 일어난다. env_file 로 직접 주입해 회피.
    env_file:
      - .env
    environment:
      NODE_ENV: production
      PORT: "7341"
      DB_HOST: mysql
      DB_PORT: "3306"
      DB_USERNAME: "${DB_USERNAME:-portfolio}"
      DB_PASSWORD: "${DB_PASSWORD:-portfolio_secret}"
      DB_DATABASE: "${DB_DATABASE:-portfolio}"
      JWT_EXPIRES_IN: "${JWT_EXPIRES_IN:-2h}"
      UPLOADS_ROOT: /app/uploads
      UPLOAD_MAX_BYTES: "${UPLOAD_MAX_BYTES:-5242880}"
    volumes:
      - uploads:/app/uploads
    depends_on:
      mysql:
        condition: service_healthy
    restart: unless-stopped

  mysql:
    image: mysql:8.0
    ports:
      - "${DB_PORT:-3307}:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD:-root_secret}"
      MYSQL_DATABASE: "${DB_DATABASE:-portfolio}"
      MYSQL_USER: "${DB_USERNAME:-portfolio}"
      MYSQL_PASSWORD: "${DB_PASSWORD:-portfolio_secret}"
      TZ: Asia/Seoul
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped

volumes:
  mysql_data:
  uploads:
