name: Build Android APK

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

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

      - name: Setup Keystore
        run: |
          echo "${{ secrets.ANDROIDKEYSTOREBASE64 }}" | base64 -d > app/helldivers.keystore
        env:
          ANDROIDKEYSTOREPATH: app/helldivers.keystore
          ANDROIDKEYSTOREPASS: ${{ secrets.ANDROIDKEYSTOREPASS }}
          ANDROIDKEYALIAS: ${{ secrets.ANDROIDKEYALIAS }}
          ANDROIDKEYPASS: ${{ secrets.ANDROIDKEYPASS }}

      - name: Build with Gradle
        run: |
          chmod +x ./gradlew
          ./gradlew assembleRelease --no-daemon

      - name: Upload APK
        uses: actions/upload-artifact@v3
        with:
          name: apk
          path: app/build/outputs/apk/release/*.apk

      - name: Create Release
        run: |
          # Fetch the specific tag from remote
          git fetch origin tag v0.2.0 --force
          
          # Get tag name from git
          TAG_NAME=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
          
          if [ -z "$TAG_NAME" ]; then
            TAG_NAME="${GITHUB_REF#refs/tags/}"
          fi
          
          # Quitar la v del tag si existe
          TAG_NAME_CLEAN="${TAG_NAME#v}"
          
          APK_FILE=$(ls app/build/outputs/apk/release/*.apk | head -1)
          
          # Rename APK with version
          NEW_NAME="Helldivers-app-${TAG_NAME}.apk"
          cp "$APK_FILE" "$NEW_NAME"
          
          echo "=== Creating release for tag: $TAG_NAME ==="
          echo "APK file: $APK_FILE"
          
          # Use the token from secrets
          if [ -z "${{ secrets.GITEATOKEN }}" ]; then
            echo "ERROR: No GITEATOKEN found"
            exit 1
          fi
          
          # Create release using Gitea API - Create from existing tag
          echo "Creating release..."
          RELEASE_RESP=$(curl -s -X POST "https://git-dangilcal.duckdns.org/api/v1/repos/dangilcal/Helldivers-app-movil/releases" \
            -H "accept: application/json" \
            -H "Content-Type: application/json" \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -d "{
              \"tag_name\": \"${TAG_NAME}\",
              \"name\": \"Release ${TAG_NAME_CLEAN}\",
              \"body\": \"Built from tag ${TAG_NAME}\",
              \"draft\": false,
              \"prerelease\": false
            }")
          
          echo "Release response: $RELEASE_RESP"
          
          # Extract release ID
          RELEASE_ID=$(echo "$RELEASE_RESP" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
          
          if [ -z "$RELEASE_ID" ]; then
            echo "ERROR: Could not get release ID"
            echo "$RELEASE_RESP"
            exit 1
          fi
          
          echo "Release ID: $RELEASE_ID"
          
          # Upload the APK as an asset
          echo "Uploading APK..."
          UPLOAD_RESP=$(curl -s -X POST "https://git-dangilcal.duckdns.org/api/v1/repos/dangilcal/Helldivers-app-movil/releases/${RELEASE_ID}/assets" \
            -H "accept: application/json" \
            -H "Content-Type: multipart/form-data" \
            -H "Authorization: token ${{ secrets.GITEATOKEN }}" \
            -F "attachment=@${NEW_NAME}")
          
          echo "Upload response: $UPLOAD_RESP"
          echo "=== Release created successfully ==="
