name: Release Build

on:
  push:
    tags:
      - 'v*.*.*'  # Aciona quando uma tag como v1.0.0 é criada

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    container:
      image: harbor.seu-dominio.com/library/android-build-env:latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      
      - name: Get version from tag
        id: get_version
        run: |
          VERSION=${GITHUB_REF#refs/tags/}
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Building version: $VERSION"
      
      - name: Build with Gradle
        run: ./gradlew assembleRelease
      
      - name: Rename APK with version
        run: |
          VERSION=${{ steps.get_version.outputs.VERSION }}
          mv app/build/outputs/apk/release/app-release.apk \
             app/build/outputs/apk/release/entregas-app-${VERSION}.apk
      
      - name: Create Release
        uses: https://gitea.com/actions/release-action@main
        with:
          files: |-
            app/build/outputs/apk/release/entregas-app-${{ steps.get_version.outputs.VERSION }}.apk
          api_key: '${{secrets.RELEASE_TOKEN}}'
          title: 'Entregas App ${{ steps.get_version.outputs.VERSION }}'
          body: |
            ${{ steps.get_version.outputs.VERSION }}
            
            - Build automático do APK 
            - Versão: ${{ steps.get_version.outputs.VERSION }}