name: Build and Release

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Decode Keystore
        run: |
          # Decodes the Base64 string from secrets to a physical file
          echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 --decode > composeApp/release.jks

      - name: Build APK
        run: ./gradlew :composeApp:assembleRelease
        env:
          KEYSTORE_PATH: release.jks
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}

      - name: Gitea Release
        uses: akkuman/gitea-release-action@v1
        with:
          files: "composeApp/build/outputs/apk/release/*.apk"
          server_url: "https://git.reya.su/"
          repository: "reya/coop-mobile"
          token: ${{ secrets.GITEA_TOKEN }}