name: ${COMPOSE_PROJECT_NAME}
services:
  # =====================
  # Database Layer (db)
  # =====================
  ac-mysql:
    profiles: ["db"]
    image: ${MYSQL_IMAGE}
    container_name: ${CONTAINER_MYSQL}
    userns_mode: "keep-id"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_HOST: '${MYSQL_ROOT_HOST}'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATADIR: /var/lib/mysql-runtime
      MYSQL_CHARACTER_SET: ${MYSQL_CHARACTER_SET}
      MYSQL_COLLATION: ${MYSQL_COLLATION}
      MYSQL_MAX_CONNECTIONS: ${MYSQL_MAX_CONNECTIONS}
      MYSQL_INNODB_BUFFER_POOL_SIZE: ${MYSQL_INNODB_BUFFER_POOL_SIZE}
      MYSQL_INNODB_LOG_FILE_SIZE: ${MYSQL_INNODB_LOG_FILE_SIZE}
    ports:
      - "${MYSQL_EXTERNAL_PORT}:${MYSQL_PORT}"
    entrypoint:
      - /usr/local/bin/mysql-entrypoint.sh
    volumes:
      - ./scripts/mysql-entrypoint.sh:/usr/local/bin/mysql-entrypoint.sh:ro
      - ${STORAGE_PATH_LOCAL}/mysql-data:/var/lib/mysql-persistent
      - ${BACKUP_PATH}:/backups
      - ${HOST_ZONEINFO_PATH}:/usr/share/zoneinfo:ro
    tmpfs:
      - /var/lib/mysql-runtime:size=${MYSQL_RUNTIME_TMPFS_SIZE}
    command:
      - mysqld
      - --datadir=/var/lib/mysql-runtime
      - --default-authentication-plugin=mysql_native_password
      - --character-set-server=${MYSQL_CHARACTER_SET}
      - --collation-server=${MYSQL_COLLATION}
      - --max_connections=${MYSQL_MAX_CONNECTIONS}
      - --innodb-buffer-pool-size=${MYSQL_INNODB_BUFFER_POOL_SIZE}
      - --innodb-log-file-size=${MYSQL_INNODB_LOG_FILE_SIZE}
      - --innodb-redo-log-capacity=${MYSQL_INNODB_REDO_LOG_CAPACITY}
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "sh", "-c", "mysqladmin ping -h localhost -u ${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} --silent || exit 1"]
      interval: ${MYSQL_HEALTHCHECK_INTERVAL}
      timeout: ${MYSQL_HEALTHCHECK_TIMEOUT}
      retries: ${MYSQL_HEALTHCHECK_RETRIES}
      start_period: ${MYSQL_HEALTHCHECK_START_PERIOD}
    networks:
      - azerothcore

  ac-db-import:
    profiles: ["db"]
    image: ${AC_DB_IMPORT_IMAGE}
    container_name: ${CONTAINER_DB_IMPORT}
    user: "${CONTAINER_USER}"
    userns_mode: "keep-id"
    depends_on:
      ac-mysql:
        condition: service_healthy
      ac-storage-init:
        condition: service_completed_successfully
    networks:
      - azerothcore
    volumes:
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
      - ${STORAGE_PATH}/logs:/azerothcore/logs
      - ${STORAGE_PATH_LOCAL}/mysql-data:/var/lib/mysql-persistent
      - ./scripts/db-import-conditional.sh:/tmp/db-import-conditional.sh:ro
    environment:
      AC_DATA_DIR: "/azerothcore/data"
      AC_LOGS_DIR: "/azerothcore/logs"
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_WORLD_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_WORLD_NAME}"
      AC_CHARACTER_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_CHARACTERS_NAME}"
      AC_CLOSE_IDLE_CONNECTIONS: "false"
      AC_UPDATES_ENABLE_DATABASES: "7"
      AC_UPDATES_AUTO_SETUP: "1"
      AC_LOG_LEVEL: "1"
      AC_LOGGER_ROOT_CONFIG: "1,Console"
      AC_LOGGER_SERVER_CONFIG: "1,Console"
      AC_APPENDER_CONSOLE_CONFIG: "1,2,0"
      CONTAINER_MYSQL: ${CONTAINER_MYSQL}
      MYSQL_PORT: ${MYSQL_PORT}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      DB_AUTH_NAME: ${DB_AUTH_NAME}
      DB_WORLD_NAME: ${DB_WORLD_NAME}
      DB_CHARACTERS_NAME: ${DB_CHARACTERS_NAME}
      CONTAINER_USER: ${CONTAINER_USER}
    entrypoint:
      - sh
      - -c
      - |
        echo "📥 Using local database import script..."
        /tmp/db-import-conditional.sh
    restart: "no"

  ac-db-init:
    profiles: ["db"]
    image: ${MYSQL_IMAGE}
    container_name: ${CONTAINER_DB_INIT}
    userns_mode: "keep-id"
    depends_on:
      ac-db-import:
        condition: service_completed_successfully
    volumes:
      - ${STORAGE_PATH_LOCAL}/mysql-data:/var/lib/mysql-persistent
      - ${BACKUP_PATH}:/backups
    networks:
      - azerothcore
    environment:
      MYSQL_PWD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_HOST: ${CONTAINER_MYSQL}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      DB_WAIT_RETRIES: ${DB_WAIT_RETRIES}
      DB_WAIT_SLEEP: ${DB_WAIT_SLEEP}
      DB_AUTH_NAME: ${DB_AUTH_NAME}
      DB_WORLD_NAME: ${DB_WORLD_NAME}
      DB_CHARACTERS_NAME: ${DB_CHARACTERS_NAME}
      MYSQL_CHARACTER_SET: ${MYSQL_CHARACTER_SET}
      MYSQL_COLLATION: ${MYSQL_COLLATION}
    command:
      - sh
      - -c
      - |
        if [ -f "/var/lib/mysql-persistent/.restore-completed" ]; then
          echo "✅ Databases already restored from backup - init not needed"; exit 0; fi
        if mysql -h ${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "
          SELECT COUNT(*) FROM information_schema.tables WHERE table_schema IN ('${DB_AUTH_NAME}', '${DB_WORLD_NAME}', '${DB_CHARACTERS_NAME}');" -s -N 2>/dev/null | grep -q -v '^0$'; then
          echo "✅ Databases already populated - init not needed"; exit 0; fi
        echo "🔧 Creating fresh AzerothCore databases..."
        microdnf install -y curl || yum install -y curl || (apt-get update && apt-get install -y curl)
        mysql -h ${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "
        CREATE DATABASE IF NOT EXISTS ${DB_AUTH_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
        CREATE DATABASE IF NOT EXISTS ${DB_WORLD_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
        CREATE DATABASE IF NOT EXISTS ${DB_CHARACTERS_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
        SHOW DATABASES;" || { echo "❌ Failed to create databases"; exit 1; }
        echo "$(date): Fresh databases created - import needed" > /var/lib/mysql-persistent/.restore-failed
        echo "✅ Fresh databases created!"
    restart: "no"

  ac-backup:
    profiles: ["db"]
    image: ${MYSQL_IMAGE}
    container_name: ${CONTAINER_BACKUP}
    userns_mode: "keep-id"
    depends_on:
      ac-db-import:
        condition: service_completed_successfully
    environment:
      MYSQL_HOST: ${CONTAINER_MYSQL}
      MYSQL_PORT: ${MYSQL_PORT}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS}
      BACKUP_RETENTION_HOURS: ${BACKUP_RETENTION_HOURS}
      BACKUP_DAILY_TIME: ${BACKUP_DAILY_TIME}
      DB_AUTH_NAME: ${DB_AUTH_NAME}
      DB_WORLD_NAME: ${DB_WORLD_NAME}
      DB_CHARACTERS_NAME: ${DB_CHARACTERS_NAME}
      TZ: ${TZ}
      CONTAINER_USER: ${CONTAINER_USER}
    volumes:
      - ${BACKUP_PATH}:/backups
      - ./scripts:/tmp/scripts:ro
    working_dir: /tmp
    command:
      - /bin/bash
      - -c
      - |
        microdnf install -y curl || yum install -y curl || (apt-get update && apt-get install -y curl)
        echo "📥 Downloading backup scheduler script (local copy preferred if mounted)..."
        if [ -f /tmp/scripts/backup-scheduler.sh ]; then
          chmod +x /tmp/scripts/backup-scheduler.sh 2>/dev/null || true
          bash /tmp/scripts/backup-scheduler.sh
        else
          echo "No local scheduler provided"
        fi
        # Fix permissions for any backup directories created
        chown -R ${CONTAINER_USER} /backups 2>/dev/null || true
        chmod -R 755 /backups 2>/dev/null || true
    restart: unless-stopped
    healthcheck:
      test:
        - "CMD"
        - "sh"
        - "-c"
        - >
          mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e 'SELECT 1'
          >/dev/null 2>&1 &&
          (
            find /backups -name '*.sql.gz' -mmin -${BACKUP_HEALTHCHECK_MAX_MINUTES} -print -quit >/dev/null ||
            awk -v limit=${BACKUP_HEALTHCHECK_GRACE_SECONDS} 'NR==1 { exit ($1 < limit) ? 0 : 1 }' /proc/uptime
          )
      interval: ${BACKUP_HEALTHCHECK_INTERVAL}
      timeout: ${BACKUP_HEALTHCHECK_TIMEOUT}
      retries: ${BACKUP_HEALTHCHECK_RETRIES}
      start_period: ${BACKUP_HEALTHCHECK_START_PERIOD}
    networks:
      - azerothcore

  # =====================
  # Volume Initialization
  # =====================
  ac-volume-init:
    profiles: ["client-data", "client-data-bots"]
    image: ${ALPINE_IMAGE}
    container_name: ac-volume-init
    user: "0:0"
    volumes:
      - ac-client-data:/azerothcore/data
      - client-data-cache:/cache
    command:
      - sh
      - -c
      - |
        echo "🔧 Fixing Docker volume permissions..."
        chown -R ${CONTAINER_USER} /azerothcore/data /cache
        chmod -R 755 /azerothcore/data /cache
        echo "✅ Docker volume permissions fixed"
    restart: "no"

  ac-storage-init:
    profiles: ["db", "modules"]
    image: ${ALPINE_IMAGE}
    container_name: ac-storage-init
    user: "0:0"
    volumes:
      - ${STORAGE_PATH}:/storage-root
      - ${STORAGE_PATH_LOCAL}:/local-storage-root
    command:
      - sh
      - -c
      - |
        echo "🔧 Initializing storage directories with proper permissions..."
        mkdir -p /storage-root/config /storage-root/logs /storage-root/modules /storage-root/lua_scripts /storage-root/install-markers
        mkdir -p /storage-root/backups /local-storage-root/mysql-data
        # Fix ownership of root directories and all contents
        chown -R ${CONTAINER_USER} /storage-root /local-storage-root
        chmod -R 755 /storage-root /local-storage-root
        echo "✅ Storage permissions initialized"
    restart: "no"

  # =====================
  # Client Data (client-data)
  # =====================
  ac-client-data-standard:
    profiles: ["client-data"]
    image: ${AC_CLIENT_DATA_IMAGE}
    container_name: ac-client-data
    user: "${CONTAINER_USER}"
    depends_on:
      ac-volume-init:
        condition: service_completed_successfully
    volumes:
      - ac-client-data:/azerothcore/data
      - client-data-cache:/cache
      - ./scripts:/tmp/scripts:ro
    working_dir: /tmp
    environment:
      - CONTAINER_USER=${CONTAINER_USER}
      - CLIENT_DATA_VERSION=${CLIENT_DATA_VERSION}
    command:
      - sh
      - -c
      - |
        mkdir -p /cache
        if [ -f /tmp/scripts/download-client-data.sh ]; then
          chmod +x /tmp/scripts/download-client-data.sh 2>/dev/null || true
          bash /tmp/scripts/download-client-data.sh
        else
          echo "No local client-data script"
        fi
    restart: "no"
    networks:
      - azerothcore

  ac-client-data-playerbots:
    profiles: ["client-data-bots"]
    image: ${AC_CLIENT_DATA_IMAGE_PLAYERBOTS}
    container_name: ac-client-data
    user: "${CONTAINER_USER}"
    depends_on:
      ac-volume-init:
        condition: service_completed_successfully
    volumes:
      - ac-client-data:/azerothcore/data
      - client-data-cache:/cache
      - ./scripts:/tmp/scripts:ro
    working_dir: /tmp
    environment:
      - CONTAINER_USER=${CONTAINER_USER}
      - CLIENT_DATA_VERSION=${CLIENT_DATA_VERSION}
    command:
      - sh
      - -c
      - |
        mkdir -p /cache
        if [ -f /tmp/scripts/download-client-data.sh ]; then
          chmod +x /tmp/scripts/download-client-data.sh 2>/dev/null || true
          bash /tmp/scripts/download-client-data.sh
        else
          echo "No local client-data script"
        fi
    restart: "no"
    networks:
      - azerothcore

  # =====================
  # Services - Standard (services-standard)
  # =====================
  ac-authserver-standard:
    profiles: ["services-standard"]
    image: ${AC_AUTHSERVER_IMAGE}
    container_name: ac-authserver
    user: "${CONTAINER_USER}"
    depends_on:
      ac-mysql:
        condition: service_healthy
      ac-db-import:
        condition: service_completed_successfully
      ac-db-init:
        condition: service_completed_successfully
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_LOG_LEVEL: "1"
      AC_LOGGER_ROOT_CONFIG: "1,Console"
      AC_LOGGER_SERVER_CONFIG: "1,Console"
      AC_APPENDER_CONSOLE_CONFIG: "1,2,0"
    ports:
      - "${AUTH_EXTERNAL_PORT}:${AUTH_PORT}"
    restart: unless-stopped
    networks:
      - azerothcore
    volumes:
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[a]uthserver' | grep -v grep || exit 1"]
      interval: ${AUTH_HEALTHCHECK_INTERVAL}
      timeout: ${AUTH_HEALTHCHECK_TIMEOUT}
      retries: ${AUTH_HEALTHCHECK_RETRIES}
      start_period: ${AUTH_HEALTHCHECK_START_PERIOD}

  ac-worldserver-standard:
    profiles: ["services-standard"]
    image: ${AC_WORLDSERVER_IMAGE}
    container_name: ac-worldserver
    user: "${CONTAINER_USER}"
    stdin_open: true
    tty: true
    depends_on:
      - ac-authserver-standard
      - ac-client-data-standard
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_WORLD_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_WORLD_NAME}"
      AC_CHARACTER_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_CHARACTERS_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_DATA_DIR: "/azerothcore/data"
      AC_SOAP_PORT: "7878"
      AC_PROCESS_PRIORITY: "0"
      AC_ELUNA_ENABLED: "${AC_ELUNA_ENABLED}"
      AC_ELUNA_TRACE_BACK: "${AC_ELUNA_TRACE_BACK}"
      AC_ELUNA_AUTO_RELOAD: "${AC_ELUNA_AUTO_RELOAD}"
      AC_ELUNA_BYTECODE_CACHE: "${AC_ELUNA_BYTECODE_CACHE}"
      AC_ELUNA_SCRIPT_PATH: "${AC_ELUNA_SCRIPT_PATH}"
      AC_ELUNA_REQUIRE_PATHS: "${AC_ELUNA_REQUIRE_PATHS}"
      AC_ELUNA_REQUIRE_CPATHS: "${AC_ELUNA_REQUIRE_CPATHS}"
      AC_ELUNA_AUTO_RELOAD_INTERVAL: "${AC_ELUNA_AUTO_RELOAD_INTERVAL}"
      PLAYERBOT_ENABLED: "${PLAYERBOT_ENABLED}"
      PLAYERBOT_MAX_BOTS: "${PLAYERBOT_MAX_BOTS}"
      AC_LOG_LEVEL: "2"
    ports:
      - "${WORLD_EXTERNAL_PORT}:${WORLD_PORT}"
      - "${SOAP_EXTERNAL_PORT}:${SOAP_PORT}"
    volumes:
      - ac-client-data:/azerothcore/data
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
      - ${STORAGE_PATH}/logs:/azerothcore/logs
      - ${STORAGE_PATH}/modules:/azerothcore/modules
      - ${STORAGE_PATH}/lua_scripts:/azerothcore/lua_scripts
    restart: unless-stopped
    networks:
      - azerothcore
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[w]orldserver' | grep -v grep || exit 1"]
      interval: ${WORLD_HEALTHCHECK_INTERVAL}
      timeout: ${WORLD_HEALTHCHECK_TIMEOUT}
      retries: ${WORLD_HEALTHCHECK_RETRIES}
      start_period: ${WORLD_HEALTHCHECK_START_PERIOD}

  # =====================
  # Services - Playerbots (services-playerbots)
  # =====================
  ac-authserver-playerbots:
    profiles: ["services-playerbots"]
    image: ${AC_AUTHSERVER_IMAGE_PLAYERBOTS}
    container_name: ac-authserver
    user: "${CONTAINER_USER}"
    depends_on:
      ac-mysql:
        condition: service_healthy
      ac-db-import:
        condition: service_completed_successfully
      ac-db-init:
        condition: service_completed_successfully
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_LOG_LEVEL: "1"
      AC_LOGGER_ROOT_CONFIG: "1,Console"
      AC_LOGGER_SERVER_CONFIG: "1,Console"
      AC_APPENDER_CONSOLE_CONFIG: "1,2,0"
    ports:
      - "${AUTH_EXTERNAL_PORT}:${AUTH_PORT}"
    restart: unless-stopped
    networks:
      - azerothcore
    volumes:
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[a]uthserver' | grep -v grep || exit 1"]
      interval: ${AUTH_HEALTHCHECK_INTERVAL}
      timeout: ${AUTH_HEALTHCHECK_TIMEOUT}
      retries: ${AUTH_HEALTHCHECK_RETRIES}
      start_period: ${AUTH_HEALTHCHECK_START_PERIOD}

  ac-authserver-modules:
    profiles: ["services-modules"]
    image: ${AC_AUTHSERVER_IMAGE_MODULES}
    container_name: ac-authserver
    user: "${CONTAINER_USER}"
    depends_on:
      ac-mysql:
        condition: service_healthy
      ac-db-import:
        condition: service_completed_successfully
      ac-db-init:
        condition: service_completed_successfully
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_LOG_LEVEL: "1"
      AC_LOGGER_ROOT_CONFIG: "1,Console"
      AC_LOGGER_SERVER_CONFIG: "1,Console"
      AC_APPENDER_CONSOLE_CONFIG: "1,2,0"
    ports:
      - "${AUTH_EXTERNAL_PORT}:${AUTH_PORT}"
    restart: unless-stopped
    networks:
      - azerothcore
    volumes:
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[a]uthserver' | grep -v grep || exit 1"]
      interval: ${AUTH_HEALTHCHECK_INTERVAL}
      timeout: ${AUTH_HEALTHCHECK_TIMEOUT}
      retries: ${AUTH_HEALTHCHECK_RETRIES}
      start_period: ${AUTH_HEALTHCHECK_START_PERIOD}

  ac-worldserver-playerbots:
    profiles: ["services-playerbots"]
    image: ${AC_WORLDSERVER_IMAGE_PLAYERBOTS}
    container_name: ac-worldserver
    user: "${CONTAINER_USER}"
    stdin_open: true
    tty: true
    depends_on:
      - ac-authserver-playerbots
      - ac-client-data-playerbots
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_WORLD_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_WORLD_NAME}"
      AC_CHARACTER_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_CHARACTERS_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_DATA_DIR: "/azerothcore/data"
      AC_SOAP_PORT: "7878"
      AC_PROCESS_PRIORITY: "0"
      AC_ELUNA_ENABLED: "${AC_ELUNA_ENABLED}"
      AC_ELUNA_TRACE_BACK: "${AC_ELUNA_TRACE_BACK}"
      AC_ELUNA_AUTO_RELOAD: "${AC_ELUNA_AUTO_RELOAD}"
      AC_ELUNA_BYTECODE_CACHE: "${AC_ELUNA_BYTECODE_CACHE}"
      AC_ELUNA_SCRIPT_PATH: "${AC_ELUNA_SCRIPT_PATH}"
      AC_ELUNA_REQUIRE_PATHS: "${AC_ELUNA_REQUIRE_PATHS}"
      AC_ELUNA_REQUIRE_CPATHS: "${AC_ELUNA_REQUIRE_CPATHS}"
      AC_ELUNA_AUTO_RELOAD_INTERVAL: "${AC_ELUNA_AUTO_RELOAD_INTERVAL}"
      PLAYERBOT_ENABLED: "${PLAYERBOT_ENABLED}"
      PLAYERBOT_MAX_BOTS: "${PLAYERBOT_MAX_BOTS}"
      AC_LOG_LEVEL: "2"
    ports:
      - "${WORLD_EXTERNAL_PORT}:${WORLD_PORT}"
      - "${SOAP_EXTERNAL_PORT}:${SOAP_PORT}"
    volumes:
      - ac-client-data:/azerothcore/data
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
      - ${STORAGE_PATH}/logs:/azerothcore/logs
      - ${STORAGE_PATH}/modules:/azerothcore/modules
      - ${STORAGE_PATH}/lua_scripts:/azerothcore/lua_scripts
    restart: unless-stopped
    networks:
      - azerothcore
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[w]orldserver' | grep -v grep || exit 1"]
      interval: ${WORLD_HEALTHCHECK_INTERVAL}
      timeout: ${WORLD_HEALTHCHECK_TIMEOUT}
      retries: ${WORLD_HEALTHCHECK_RETRIES}
      start_period: ${WORLD_HEALTHCHECK_START_PERIOD}

  ac-worldserver-modules:
    profiles: ["services-modules"]
    image: ${AC_WORLDSERVER_IMAGE_MODULES}
    container_name: ac-worldserver
    user: "${CONTAINER_USER}"
    stdin_open: true
    tty: true
    depends_on:
      - ac-authserver-modules
      - ac-client-data-standard
    environment:
      AC_LOGIN_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_AUTH_NAME}"
      AC_WORLD_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_WORLD_NAME}"
      AC_CHARACTER_DATABASE_INFO: "${CONTAINER_MYSQL};${MYSQL_PORT};${MYSQL_USER};${MYSQL_ROOT_PASSWORD};${DB_CHARACTERS_NAME}"
      AC_UPDATES_ENABLE_DATABASES: "0"
      AC_BIND_IP: "0.0.0.0"
      AC_DATA_DIR: "/azerothcore/data"
      AC_SOAP_PORT: "7878"
      AC_PROCESS_PRIORITY: "0"
      AC_ELUNA_ENABLED: "${AC_ELUNA_ENABLED}"
      AC_ELUNA_TRACE_BACK: "${AC_ELUNA_TRACE_BACK}"
      AC_ELUNA_AUTO_RELOAD: "${AC_ELUNA_AUTO_RELOAD}"
      AC_ELUNA_BYTECODE_CACHE: "${AC_ELUNA_BYTECODE_CACHE}"
      AC_ELUNA_SCRIPT_PATH: "${AC_ELUNA_SCRIPT_PATH}"
      AC_ELUNA_REQUIRE_PATHS: "${AC_ELUNA_REQUIRE_PATHS}"
      AC_ELUNA_REQUIRE_CPATHS: "${AC_ELUNA_REQUIRE_CPATHS}"
      AC_ELUNA_AUTO_RELOAD_INTERVAL: "${AC_ELUNA_AUTO_RELOAD_INTERVAL}"
      PLAYERBOT_ENABLED: "${PLAYERBOT_ENABLED}"
      PLAYERBOT_MAX_BOTS: "${PLAYERBOT_MAX_BOTS}"
      AC_LOG_LEVEL: "2"
    volumes:
      - ac-client-data:/azerothcore/data
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
      - ${STORAGE_PATH}/logs:/azerothcore/logs
      - ${STORAGE_PATH}/modules:/azerothcore/modules
      - ${STORAGE_PATH}/lua_scripts:/azerothcore/lua_scripts
    networks:
      - azerothcore
    ports:
      - "${WORLD_EXTERNAL_PORT}:${WORLD_PORT}"
      - "${SOAP_EXTERNAL_PORT}:${SOAP_PORT}"
    restart: unless-stopped
    cap_add: ["SYS_NICE"]
    healthcheck:
      test: ["CMD", "sh", "-c", "ps aux | grep '[w]orldserver' | grep -v grep || exit 1"]
      interval: ${WORLD_HEALTHCHECK_INTERVAL}
      timeout: ${WORLD_HEALTHCHECK_TIMEOUT}
      retries: ${WORLD_HEALTHCHECK_RETRIES}
      start_period: ${WORLD_HEALTHCHECK_START_PERIOD}

  # =====================
  # Modules & Post-install (modules)
  # =====================
  ac-modules:
    profiles: ["modules"]
    image: ${ALPINE_IMAGE}
    container_name: ${CONTAINER_MODULES}
    user: "0:0"
    depends_on:
      ac-mysql:
        condition: service_healthy
      ac-db-import:
        condition: service_completed_successfully
      ac-db-init:
        condition: service_completed_successfully
      ac-storage-init:
        condition: service_completed_successfully
    volumes:
      - ${STORAGE_PATH}/modules:/modules
      - ${STORAGE_PATH}/config:/azerothcore/env/dist/etc
      - ./scripts:/tmp/scripts:ro
      - ./config:/tmp/config:ro
    env_file:
      - ./.env
    environment:
      MODULES_MANIFEST_PATH: /tmp/config/modules.json
    entrypoint: ["/bin/sh"]
    command:
      - -c
      - |
        apk add --no-cache curl bash git python3
        (chmod +x /tmp/scripts/manage-modules.sh /tmp/scripts/manage-modules-sql.sh 2>/dev/null || true) && /tmp/scripts/manage-modules.sh
        # Fix permissions after module operations
        chown -R ${CONTAINER_USER} /modules /azerothcore/env/dist/etc 2>/dev/null || true
        chmod -R 755 /modules /azerothcore/env/dist/etc 2>/dev/null || true
    restart: "no"
    networks:
      - azerothcore

  ac-post-install:
    profiles: ["modules"]
    image: ${ALPINE_IMAGE}
    container_name: ${CONTAINER_POST_INSTALL}
    user: "0:0"
    volumes:
      - ${STORAGE_PATH}/config:/azerothcore/config
      - ${STORAGE_PATH}/install-markers:/install-markers
      - ./scripts:/tmp/scripts:ro
      - /var/run/docker.sock:/var/run/docker.sock:rw
    working_dir: /tmp
    environment:
      MYSQL_HOST: ${CONTAINER_MYSQL}
      MYSQL_PORT: ${MYSQL_PORT}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      DB_AUTH_NAME: ${DB_AUTH_NAME}
      DB_WORLD_NAME: ${DB_WORLD_NAME}
      DB_CHARACTERS_NAME: ${DB_CHARACTERS_NAME}
      DB_PLAYERBOTS_NAME: ${DB_PLAYERBOTS_NAME}
      MYSQL_CHARACTER_SET: ${MYSQL_CHARACTER_SET}
      MYSQL_COLLATION: ${MYSQL_COLLATION}
      STORAGE_PATH: ${STORAGE_PATH}
      SERVER_ADDRESS: ${SERVER_ADDRESS}
      REALM_PORT: ${REALM_PORT}
      NETWORK_NAME: ${NETWORK_NAME}
      CONTAINER_AUTHSERVER: ac-authserver
      CONTAINER_WORLDSERVER: ac-worldserver
      CONTAINER_USER: ${CONTAINER_USER}
    depends_on:
      ac-modules:
        condition: service_completed_successfully
      ac-mysql:
        condition: service_healthy
      ac-storage-init:
        condition: service_completed_successfully
    command:
      - sh
      - -c
      - |
        apk add --no-cache bash curl docker-cli
        chown -R ${CONTAINER_USER} /azerothcore/config /install-markers 2>/dev/null || true
        chmod -R 755 /azerothcore/config /install-markers 2>/dev/null || true
        echo "📥 Running local auto-post-install script..."
        (chmod +x /tmp/scripts/auto-post-install.sh 2>/dev/null || true) && bash /tmp/scripts/auto-post-install.sh
        # Fix permissions for all files created during post-install
        chown -R ${CONTAINER_USER} /azerothcore/config /install-markers 2>/dev/null || true
        chmod -R 755 /azerothcore/config /install-markers 2>/dev/null || true
    restart: "no"
    networks:
      - azerothcore

  # =====================
  # Tools (tools)
  # =====================
  ac-phpmyadmin:
    profiles: ["tools"]
    image: phpmyadmin/phpmyadmin:latest
    container_name: ac-phpmyadmin
    environment:
      PMA_HOST: ${PMA_HOST}
      PMA_PORT: ${PMA_PORT}
      PMA_USER: ${PMA_USER}
      PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      PMA_ARBITRARY: ${PMA_ARBITRARY}
      PMA_ABSOLUTE_URI: ${PMA_ABSOLUTE_URI}
      UPLOAD_LIMIT: ${PMA_UPLOAD_LIMIT}
      MEMORY_LIMIT: ${PMA_MEMORY_LIMIT}
      MAX_EXECUTION_TIME: ${PMA_MAX_EXECUTION_TIME}
    ports:
      - "${PMA_EXTERNAL_PORT}:80"
    healthcheck:
      test: ["CMD", "sh", "-c", "curl -fsS http://localhost:80/ || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    restart: unless-stopped
    networks:
      - azerothcore

  ac-keira3:
    profiles: ["tools"]
    image: uprightbass360/keira3:latest
    container_name: ac-keira3
    restart: unless-stopped
    environment:
      - NODE_ENV=production
      - KEIRA_PORT=8080
      - KEIRA_HOST=0.0.0.0
      - KEIRA_DATABASE_HOST=${KEIRA_DATABASE_HOST}
      - KEIRA_DATABASE_PORT=${KEIRA_DATABASE_PORT}
      - KEIRA_DATABASE_USER=root
      - KEIRA_DATABASE_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - KEIRA_DATABASE_NAME=${DB_WORLD_NAME}
    ports:
      - "${KEIRA3_EXTERNAL_PORT}:8080"
    healthcheck:
      test: ["CMD", "sh", "-c", "curl -f http://localhost:8080/health >/dev/null 2>&1 || nc -z localhost 8080 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
    security_opt:
      - no-new-privileges:true
    networks:
      - azerothcore

volumes:
  ac-client-data:
    name: ${CLIENT_DATA_VOLUME}
    driver: local
  client-data-cache:
    driver: local

networks:
  azerothcore:
    name: ${NETWORK_NAME}
    driver: bridge
    ipam:
      config:
        - subnet: ${NETWORK_SUBNET}
          gateway: ${NETWORK_GATEWAY}
