# Required GitHub secrets:
# SIGNING_KEYSTORE_BASE64 - base64-encoded Android release keystore
# SIGNING_STORE_PASSWORD    - keystore password
# SIGNING_KEY_ALIAS         - key alias
# SIGNING_KEY_PASSWORD      - key password
name: Build and Release APK

on:
  push:
    tags:
      - 'v*'

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Validate tag format
        run: |
          if [[ "${{ github.ref_name }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo "Tag ${{ github.ref_name }} is valid."
          else
            echo "Invalid tag format: ${{ github.ref_name }}. Expected vN.N.N"
            exit 1
          fi

      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3

      - name: Decode keystore
        run: |
          echo "${{ secrets.SIGNING_KEYSTORE_BASE64 }}" | base64 -d > release.keystore

      - name: Export signing environment variables
        run: |
          echo "SIGNING_STORE_FILE=$(pwd)/release.keystore" >> $GITHUB_ENV
          echo "SIGNING_STORE_PASSWORD=${{ secrets.SIGNING_STORE_PASSWORD }}" >> $GITHUB_ENV
          echo "SIGNING_KEY_ALIAS=${{ secrets.SIGNING_KEY_ALIAS }}" >> $GITHUB_ENV
          echo "SIGNING_KEY_PASSWORD=${{ secrets.SIGNING_KEY_PASSWORD }}" >> $GITHUB_ENV

      - name: Test and Build Release APK
        run: chmod +x ./gradlew && ./gradlew test assembleRelease

      - name: Rename APK
        run: |
          mkdir -p artifacts
          cp app/build/outputs/apk/release/app-release.apk artifacts/esp32-aldl-dashboard-${{ github.ref_name }}.apk

      - name: Create Release and Upload APK
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/esp32-aldl-dashboard-${{ github.ref_name }}.apk
          generate_release_notes: true