# documentation: https://pterodactyl.io/
# slogan: Pterodactyl is a free, open-source game server management panel
# category: media
# tags: game, game server, management, panel, minecraft
# logo: svgs/pterodactyl.png
# port: 80

services:
  mariadb:
    image: mariadb:11.8
    healthcheck:
      test:
        ["CMD-SHELL", "healthcheck.sh --connect --innodb_initialized || exit 1"]
      start_period: 10s
      interval: 10s
      timeout: 1s
      retries: 3
    environment:
      - MYSQL_ROOT_PASSWORD=$SERVICE_PASSWORD_MYSQLROOT
      - MYSQL_DATABASE=pterodactyl-db
      - MYSQL_USER=$SERVICE_USER_MYSQL
      - MYSQL_PASSWORD=$SERVICE_PASSWORD_MYSQL
    volumes:
      - pterodactyl-db:/var/lib/mysql

  redis:
    image: redis:alpine
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping || exit 1"]
      interval: 10s
      timeout: 1s
      retries: 3

  pterodactyl:
    image: ghcr.io/pterodactyl/panel:latest
    volumes:
      - "panel-var:/app/var/"
      - "panel-nginx:/etc/nginx/http.d/"
      - "panel-certs:/etc/letsencrypt/"
      - type: bind
        source: ./etc/entrypoint.sh
        target: /entrypoint.sh
        mode: "0755"
        content: |
          #!/bin/sh
          set -e

           echo "Setting logs permissions..."
           chown -R nginx: /app/storage/logs/

           USER_EXISTS=$(php artisan tinker --no-ansi --execute='echo \Pterodactyl\Models\User::where("email", "'"$ADMIN_EMAIL"'")->exists() ? "1" : "0";')

           if [ "$USER_EXISTS" = "0" ]; then
             echo "Admin User does not exist, creating user now."
             php artisan p:user:make --no-interaction \
               --admin=1 \
               --email="$ADMIN_EMAIL" \
               --username="$ADMIN_USERNAME" \
               --name-first="$ADMIN_FIRSTNAME" \
               --name-last="$ADMIN_LASTNAME" \
               --password="$ADMIN_PASSWORD"
             echo "Admin user created successfully!"
           else
             echo "Admin User already exists, skipping creation."
           fi

           exec supervisord --nodaemon
    command: ["/entrypoint.sh"]
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:80 || exit 1"]
      interval: 10s
      timeout: 1s
      retries: 3
    environment:
      - HASHIDS_SALT=$SERVICE_PASSWORD_HASHIDS
      - HASHIDS_LENGTH=8
      - SERVICE_URL_PTERODACTYL_80
      - ADMIN_EMAIL=${ADMIN_EMAIL:-admin@example.com}
      - ADMIN_USERNAME=${SERVICE_USER_ADMIN}
      - ADMIN_FIRSTNAME=${ADMIN_FIRSTNAME:-Admin}
      - ADMIN_LASTNAME=${ADMIN_LASTNAME:-User}
      - ADMIN_PASSWORD=${SERVICE_PASSWORD_ADMIN}
      - PTERODACTYL_HTTPS=${PTERODACTYL_HTTPS:-false}
      - APP_ENV=production
      - APP_ENVIRONMENT_ONLY=false
      - APP_URL=$SERVICE_URL_PTERODACTYL
      - APP_TIMEZONE=${TIMEZONE:-UTC}
      - APP_SERVICE_AUTHOR=${APP_SERVICE_AUTHOR:-author@example.com}
      - LOG_LEVEL=${LOG_LEVEL:-debug}
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_DRIVER=redis
      - REDIS_HOST=redis
      - DB_DATABASE=pterodactyl-db
      - DB_USERNAME=$SERVICE_USER_MYSQL
      - DB_HOST=mariadb
      - DB_PORT=3306
      - DB_PASSWORD=$SERVICE_PASSWORD_MYSQL
      - MAIL_FROM=$MAIL_FROM
      - MAIL_DRIVER=$MAIL_DRIVER
      - MAIL_HOST=$MAIL_HOST
      - MAIL_PORT=$MAIL_PORT
      - MAIL_USERNAME=$MAIL_USERNAME
      - MAIL_PASSWORD=$MAIL_PASSWORD
      - MAIL_ENCRYPTION=$MAIL_ENCRYPTION
