name: bundle

on:
  workflow_dispatch:
  schedule:
    - cron: "22 9 * * *"

jobs:
  bundle:
    runs-on: [ubuntu-24.04]
    steps:
      - name: Checkout knoxmakers-inkscape (plain git)
        run: |
          set -euo pipefail
          rm -rf .git || true
          rm -rf ./* ./.??* || true
          git clone --depth 1 https://git.knoxmakers.org/KnoxMakers/knoxmakers-inkscape.git .
          git checkout main

      - name: Configure git identity
        run: |
          git config user.name "haxbot"
          git config user.email "haxbot@knoxmakers.sh"

      - name: Run bundle script
        run: |
          python3 scripts/bundle.py

      - name: Push + tag + create release with filtered bundle (if bundle update commit was created)
        env:
          HAXBOT_TOKEN: ${{ secrets.HAXBOT_TOKEN }}
        run: |
          set -euo pipefail

          if git log -1 --pretty=%B | grep -q '^bundle: update'; then
            OWNER="KnoxMakers"
            REPO="knoxmakers-inkscape"
            BASE_URL="https://git.knoxmakers.org"

            # Authenticated push URL (PAT belongs to haxbot)
            REPO_URL="https://x-access-token:${HAXBOT_TOKEN}@git.knoxmakers.org/${OWNER}/${REPO}.git"
            git remote set-url origin "$REPO_URL"

            # Push the commit to main
            git push origin HEAD:main

            # Create a unique tag for this bundle run
            DATETIME="$(date -u +%Y%m%d-%H%M%S)"
            TAG="knox-makers-inkscape-${DATETIME}"
            SHA="$(git rev-parse HEAD)"

            git tag -a "$TAG" -m "Automated bundle: $TAG ($SHA)"
            git push origin "$TAG"

            # Build a release artifact: include everything except scripts/ and .gitea/ and .git/
            ART="${TAG}.zip"
            rm -f "$ART"

            zip -r "$ART" . \
              -x "scripts/*" \
              -x ".gitea/*" \
              -x ".git/*"

            # Create a Gitea Release for the tag
            API_CREATE="${BASE_URL}/api/v1/repos/${OWNER}/${REPO}/releases"

            CREATE_JSON="$(jq -n \
              --arg tag "$TAG" \
              --arg name "$TAG" \
              --arg body "Automated bundle release.\n\nExcludes: scripts/, .gitea/\nCommit: $SHA\nUTC: $(date -u +'%Y-%m-%d %H:%M:%S')" \
              '{tag_name:$tag, name:$name, body:$body, draft:false, prerelease:false}')"

            RELEASE_RESP="$(curl -fsS -X POST "$API_CREATE" \
              -H "Authorization: token ${HAXBOT_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "$CREATE_JSON")"

            RELEASE_ID="$(printf '%s' "$RELEASE_RESP" | jq -r '.id')"
            if [ -z "$RELEASE_ID" ] || [ "$RELEASE_ID" = "null" ]; then
              echo "Failed to parse release id. Response:"
              echo "$RELEASE_RESP"
              exit 1
            fi

            # Upload the bundle as a release asset
            API_UPLOAD="${BASE_URL}/api/v1/repos/${OWNER}/${REPO}/releases/${RELEASE_ID}/assets?name=$(printf '%s' "$ART" | jq -sRr @uri)"

            curl -fsS -X POST "$API_UPLOAD" \
              -H "Authorization: token ${HAXBOT_TOKEN}" \
              -H "Content-Type: multipart/form-data" \
              -F "attachment=@${ART};type=application/zip"

            echo "Release created: $TAG (id=$RELEASE_ID) with asset: $ART"
          else
            echo "No update commit created."
          fi
