name: Build APK

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: cimg/android:2024.01

    steps:
      - name: Install Node.js
        run: |
          sudo apt-get update
          sudo apt-get install -y curl
          curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
          sudo apt-get install -y nodejs
      - name: Checkout
        uses: actions/checkout@v4

      - name: Generate Gradle Wrapper
        run: gradle wrapper

      - name: Grant execute permission
        run: chmod +x ./gradlew

      - name: Build APK
        run: ./gradlew assembleRelease

      - name: Locate APK
        id: locate_apk
        run: |
          set -e
          APK_PATH=$(ls app/build/outputs/apk/release/*.apk | head -n1 || true)
          if [ -z "$APK_PATH" ]; then
            echo "No APK found in app/build/outputs/apk/release" >&2
            exit 1
          fi
          echo "APK_PATH=$APK_PATH" >> $GITHUB_ENV
          echo "Found $APK_PATH"

      - name: Upload APK (artifact)
        uses: actions/upload-artifact@v4
        with:
          name: app-release
          path: ${{ env.APK_PATH }}

      - name: Create Gitea release
        id: create_release
        env:
          GITEA_TOKEN: ${{ secrets.GIT_TOKEN }}
          GITEA_SERVER: ${{ secrets.GIT_SERVER }}
        run: |
          set -e
          if [ -n "$GITEA_SERVER" ]; then
            API_BASE="$GITEA_SERVER/api/v1"
          else
            API_BASE="https://gitea.com/api/v1"
          fi

          REMOTE_URL=$(git config --get remote.origin.url || true)
          if [ -z "$REMOTE_URL" ]; then
            echo "Cannot determine remote origin URL" >&2
            exit 1
          fi
          REPO_FULL=$(echo "$REMOTE_URL" | sed -E 's#.*[:/](.+/.+)\.git$#\1#')
          OWNER=$(echo "$REPO_FULL" | cut -d/ -f1)
          REPO_NAME=$(echo "$REPO_FULL" | cut -d/ -f2)

          TAG=${GITHUB_REF_NAME:-$(echo $GITHUB_REF | sed 's#refs/tags/##')}
          if [ -z "$TAG" ]; then
            TAG=$(git describe --tags --exact-match 2>/dev/null || true)
          fi
          if [ -z "$TAG" ]; then
            echo "Cannot determine tag name" >&2
            exit 1
          fi

          echo "Creating release $TAG for $OWNER/$REPO_NAME against $API_BASE"

          CREATE_RESPONSE=$(curl -s -H "Content-Type: application/json" -H "Authorization: token $GITEA_TOKEN" \
            -d "{\"tag_name\": \"$TAG\", \"name\": \"$TAG\", \"body\": \"Automated release for $TAG\", \"draft\": false, \"prerelease\": false}" \
            "$API_BASE/repos/$OWNER/$REPO_NAME/releases")

          RELEASE_ID=$(echo "$CREATE_RESPONSE" | grep -o '"id":[0-9]*' | head -n1 | cut -d: -f2 | tr -d ' ')
          if [ -z "$RELEASE_ID" ]; then
            echo "Failed to create release. Response: $CREATE_RESPONSE" >&2
            exit 1
          fi
          echo "RELEASE_ID=$RELEASE_ID" >> $GITHUB_ENV
          echo "Created release id $RELEASE_ID"

      - name: Upload APK to Gitea release
        env:
          GITEA_TOKEN: ${{ secrets.GIT_TOKEN }}
          GITEA_SERVER: ${{ secrets.GIT_SERVER }}
        run: |
          set -e
          API_BASE=${GITEA_SERVER:+$GITEA_SERVER/api/v1}
          API_BASE=${API_BASE:-https://gitea.com/api/v1}
          REMOTE_URL=$(git config --get remote.origin.url || true)
          REPO_FULL=$(echo "$REMOTE_URL" | sed -E 's#.*[:/](.+/.+)\.git$#\1#')
          OWNER=$(echo "$REPO_FULL" | cut -d/ -f1)
          REPO_NAME=$(echo "$REPO_FULL" | cut -d/ -f2)
          APK_PATH=${APK_PATH:-$APK_PATH}
          if [ -z "$APK_PATH" ]; then
            echo "APK_PATH not set" >&2
            exit 1
          fi
          RELEASE_ID=${RELEASE_ID}
          if [ -z "$RELEASE_ID" ]; then
            echo "RELEASE_ID not set" >&2
            exit 1
          fi

          UPLOAD_URL="$API_BASE/repos/$OWNER/$REPO_NAME/releases/$RELEASE_ID/assets?name=$(basename $APK_PATH)"
          echo "Uploading $APK_PATH to $UPLOAD_URL"

          curl --fail -H "Authorization: token $GITEA_TOKEN" -H "Content-Type: application/octet-stream" \
            --data-binary @"$APK_PATH" "$UPLOAD_URL"
