on:
  pull_request:
    paths:
      - "**/*.dart"

name: Deploy PR on Github Pages

env:
  FLUTTER_VERSION: 3.32.8

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}

jobs:
  deploy:
    name: Build web version and deploy
    runs-on: ubuntu-latest
    environment:
      name: PR-${{ github.event.pull_request.number }}
      url: ${{ steps.configure.outputs.URL }}

    steps:
      # 🧹 Free up space before building
      - name: Free up disk space before build
        run: |
          echo "=== Disk space before cleanup ==="
          df -h
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /usr/local/lib/android
          sudo rm -rf /opt/ghc
          sudo apt-get clean
          sudo apt-get autoclean
          echo "=== Disk space after cleanup ==="
          df -h

      # 🔄 Checkout code
      - name: Checkout repository
        uses: actions/checkout@v4

      # 🧰 Setup Flutter
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: "stable"
          cache: true
          cache-key: deps-${{ hashFiles('**/pubspec.lock') }} # optional, change this to force refresh cache
          cache-path: ${{ runner.tool_cache }}/flutter # optional, change this to specify the cache path

      # 🧹 Clean Flutter cache before building
      - name: Flutter clean
        run: flutter clean

      # 📦 Run prebuild (if any)
      - name: Run prebuild
        run: ./scripts/prebuild.sh

      # ⚙️ Configure environment for PR
      - name: Configure environments
        id: configure
        env:
          FOLDER: ${{ github.event.pull_request.number }}
        run: ./scripts/configure-web-environment.sh

      # 🧱 Build Flutter Web (release)
      - name: Build Web (Release)
        env:
          FOLDER: ${{ github.event.pull_request.number }}
        run: |
          echo "=== Disk usage before build ==="
          df -h
          ./scripts/build-web.sh
          echo "=== Disk usage after build ==="
          df -h

      # 🚀 Deploy to GitHub Pages
      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          destination_dir: ${{ github.event.pull_request.number }}
          keep_files: true
          publish_dir: "build/web"

      # 🧹 Clean up after build to save space
      - name: Cleanup after deploy
        if: always()
        run: |
          rm -rf build/
          rm -rf .dart_tool/
          echo "=== Disk usage after cleanup ==="
          df -h

      # 💬 Create or update comments on PR
      - name: Find deployment comment
        uses: peter-evans/find-comment@v3
        id: fc
        with:
          comment-author: "github-actions[bot]"
          issue-number: ${{ github.event.pull_request.number }}
          body-includes: "This PR has been deployed to"

      - name: Create or update deployment comment
        uses: peter-evans/create-or-update-comment@v4
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            This PR has been deployed to ${{ steps.configure.outputs.URL }}.
          edit-mode: replace
