name: Validate Balatro Mods

on:
  pull_request:
    paths:
      - "mods/**"

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v3

      - name: Install ImageMagick
        run: |
          sudo apt-get update
          sudo apt-get install -y imagemagick

      - name: Check Required Files
        run: |
          for dir in mods/*/; do
            if [ -d "$dir" ]; then
              MOD_DIR="$(basename "$dir")"

              # Ensure description.md and meta.json exist
              if [ ! -f "$dir/description.md" ]; then
                echo "Error: Missing description.md in $MOD_DIR"
                exit 1
              fi

              if [ ! -f "$dir/meta.json" ]; then
                echo "Error: Missing meta.json in $MOD_DIR"
                exit 1
              fi
            fi
          done

      - name: Check Thumbnail Dimensions
        run: |
          for dir in mods/*/; do
            if [ -d "$dir" ]; then
              MOD_DIR="$(basename "$dir")"
              THUMBNAIL="$dir/thumbnail.jpg"

              if [ -f "$THUMBNAIL" ]; then
                # Extract width and height using ImageMagick
                DIMENSIONS=$(identify -format "%wx%h" "$THUMBNAIL")
                WIDTH=$(echo "$DIMENSIONS" | cut -dx -f1)
                HEIGHT=$(echo "$DIMENSIONS" | cut -dx -f2)

                # Check if dimensions exceed 1920x1080
                if [ "$WIDTH" -gt 1920 ] || [ "$HEIGHT" -gt 1080 ]; then
                  echo "Error: Thumbnail in $MOD_DIR exceeds the recommended size of 1920×1080."
                  exit 1
                fi
              fi
            fi
          done

      - name: Validate JSON Format
        uses: github/super-linter@v4
        env:
          VALIDATE_JSON: true
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Validate meta.json Against Schema
        uses: dsanders11/json-schema-validate-action@v1.2.0
        with:
          schema: "./schema/meta.schema.json"
          files: |
            mods/*/meta.json

      - name: Validate Download URLs
        run: |
          for dir in mods/*/; do
            if [ -d "$dir" ]; then
              MOD_DIR="$(basename "$dir")"
              META_JSON="$dir/meta.json"
              
              # Check if downloadURL exists and is not empty
              DOWNLOAD_URL=$(jq -r '.downloadURL // empty' "$META_JSON")
              if [ -z "$DOWNLOAD_URL" ]; then
                echo "Error: Missing or empty downloadURL in $MOD_DIR/meta.json"
                exit 1
              fi
              
              # Optional: Check if URL is accessible
              if ! curl --output /dev/null --silent --head --fail "$DOWNLOAD_URL"; then
                echo "Error: downloadURL in $MOD_DIR/meta.json is not accessible"
                exit 1
              fi
            fi
          done


  review-and-approve:
      needs: validate  # This job will only run after 'validate' completes successfully.
      runs-on: ubuntu-latest
      environment:
        name: mod-review  # This is the environment you created earlier.
      steps:
        - name: Await Approval from Maintainers
          run: echo "Waiting for manual approval by maintainers..."
