name: Android Release Build

on:
  push:
    tags:
      - '*'
  workflow_dispatch: {}

jobs:
  build:
    runs-on: debian

    steps:
      - name: Checkout source
        uses: actions/checkout@v4

      - name: Configure SDK path
        run: echo "sdk.dir=/opt/android-sdk" > local.properties

      - name: Check Java version
        run: |
          if ! command -v java >/dev/null; then
            echo "❌ Java is not installed"
            exit 1
          fi
          echo "✅ Java version:"
          java -version

      - name: Decode keystore
        run: |
          echo "$RELEASE_KEY_B64" | base64 -d > app/release-key.jks
        env:
          RELEASE_KEY_B64: ${{ secrets.RELEASE_KEY_B64 }}

      - name: Test keystore
        run: |
          keytool -list -v -keystore app/release-key.jks -storepass "$KEYSTORE_PASSWORD"
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}

      - name: Extract version from build.gradle
        id: version
        run: |
          VERSION=$(sed -nE 's/^[[:space:]]*versionName[[:space:]]*=[[:space:]]*"([^"]+)"/\1/p' app/build.gradle)
          VERSION="v${VERSION#v}"  # normalize to vX.Y.Z
          echo "$VERSION" > version.txt
          echo "✅ Detected version: $VERSION"

      - name: Set up gradle
        run: chmod +x ./gradlew

      - name: Inject signing credentials
        run: |
          echo "RELEASE_STORE_FILE=app/release-key.jks" >> gradle.properties
          echo "RELEASE_STORE_PASSWORD=$KEYSTORE_PASSWORD" >> gradle.properties
          echo "RELEASE_KEY_ALIAS=release-key" >> gradle.properties
          echo "RELEASE_KEY_PASSWORD=$KEY_PASSWORD" >> gradle.properties
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}

      - name: Build signed APK
        run: ./gradlew clean assembleRelease
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}

      - name: Create Forgejo release
        run: |
          TAG_NAME=$(cat version.txt)
          echo "📦 Creating release for tag: $TAG_NAME"

          curl -sSL -X POST "$FORGEJO_API/repos/${OWNER}/${REPO}/releases" \
            -H "Authorization: token $FORGEJO_TOKEN" \
            -H "Content-Type: application/json" \
            -d "{
              \"tag_name\": \"$TAG_NAME\",
              \"name\": \"$TAG_NAME\",
              \"body\": \"Automatically generated Android release\",
              \"draft\": false,
              \"prerelease\": false
            }"
        env:
          FORGEJO_API: https://weforge.xyz/api/v1
          OWNER: partisan
          REPO: Pulse
          FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}

      - name: Upload APK to release
        run: |
          TAG_NAME=$(cat version.txt)
          echo "📤 Uploading APK to release tag: $TAG_NAME"

          RELEASE_ID=$(curl -s -H "Authorization: token $FORGEJO_TOKEN" \
            "$FORGEJO_API/repos/${OWNER}/${REPO}/releases/tags/$TAG_NAME" | jq -r .id)

          curl -sSL -X POST "$FORGEJO_API/repos/${OWNER}/${REPO}/releases/${RELEASE_ID}/assets?name=app-release.apk" \
            -H "Authorization: token $FORGEJO_TOKEN" \
            -H "Content-Type: application/vnd.android.package-archive" \
            --data-binary "@app/build/outputs/apk/release/app-release.apk"
        env:
          FORGEJO_API: https://weforge.xyz/api/v1
          OWNER: partisan
          REPO: Pulse
          FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}

      - name: Cleanup secrets
        run: |
          rm -f app/release-key.jks gradle.properties
          echo "🧹 Secrets cleaned up"