name: Build Android Production APK

on:
  push:
    branches: [ main ]  # Only trigger on push/merge to main
  workflow_dispatch:  # Enables manual trigger

permissions:
  contents: write  # Required for release creation

jobs:
  build:
    name: Build Production APK
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      
      - name: Extract semantic version from build.gradle.kts
        run: |
          # Version from build.gradle.kts for F-Droid compatibility
          VERSION_NAME=$(grep "versionName = " android/app/build.gradle.kts | sed 's/.*versionName = "\(.*\)".*/\1/')
          VERSION_CODE=$(grep "versionCode = " android/app/build.gradle.kts | sed 's/.*versionCode = \([0-9]*\).*/\1/')
          
          # Semantic versioning (not date-based)
          BUILD_NUMBER="$VERSION_CODE"
          
          echo "VERSION_NAME=$VERSION_NAME" >> $GITHUB_ENV
          echo "BUILD_NUMBER=$BUILD_NUMBER" >> $GITHUB_ENV
          echo "VERSION_TAG=v$VERSION_NAME" >> $GITHUB_ENV
          
          echo "🚀 Building version: $VERSION_NAME (Code: $BUILD_NUMBER)"
      
      - name: Verify version from build.gradle.kts
        run: |
          echo "✅ Using version from build.gradle.kts:"
          grep -E "versionCode|versionName" android/app/build.gradle.kts
      
      - name: Configure Android signing
        run: |
          echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > android/app/simple-notes-release.jks
          echo "storePassword=${{ secrets.KEYSTORE_PASSWORD }}" > android/key.properties
          echo "keyPassword=${{ secrets.KEY_PASSWORD }}" >> android/key.properties
          echo "keyAlias=${{ secrets.KEY_ALIAS }}" >> android/key.properties
          echo "storeFile=simple-notes-release.jks" >> android/key.properties
          echo "✅ Signing configuration created"
      
      - name: Build production APK (Standard + F-Droid Flavors)
        run: |
          cd android
          ./gradlew assembleStandardRelease assembleFdroidRelease --no-daemon --stacktrace
      
      - name: Copy APK variants with version names
        run: |
          mkdir -p apk-output
          
          # Standard Flavor - Universal APK
          cp android/app/build/outputs/apk/standard/release/app-standard-release.apk \
             apk-output/simple-notes-sync-v${{ env.VERSION_NAME }}-standard.apk
          
          # F-Droid Flavor - Universal APK
          cp android/app/build/outputs/apk/fdroid/release/app-fdroid-release.apk \
             apk-output/simple-notes-sync-v${{ env.VERSION_NAME }}-fdroid.apk
          
          echo "✅ APK files prepared:"
          ls -lh apk-output/
      
      - name: Upload APK artifacts
        uses: actions/upload-artifact@v4
        with:
          name: simple-notes-sync-apks-v${{ env.VERSION_NAME }}
          path: apk-output/*.apk
          retention-days: 90  # Keep production builds longer
      
      - name: Extract commit information
        run: |
          echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
          echo "COMMIT_DATE=$(git log -1 --format=%cd --date=iso-strict)" >> $GITHUB_ENV
      
      - name: Read F-Droid changelogs
        run: |
          # Read German changelog (main language) - Use printf to ensure proper formatting
          if [ -f "fastlane/metadata/android/de-DE/changelogs/${{ env.BUILD_NUMBER }}.txt" ]; then
            CHANGELOG_CONTENT=$(cat "fastlane/metadata/android/de-DE/changelogs/${{ env.BUILD_NUMBER }}.txt")
            echo "CHANGELOG_DE<<GHADELIMITER" >> $GITHUB_ENV
            echo "$CHANGELOG_CONTENT" >> $GITHUB_ENV
            echo "GHADELIMITER" >> $GITHUB_ENV
          else
            echo "CHANGELOG_DE=No German release notes available." >> $GITHUB_ENV
          fi
          
          # Read English changelog (optional)
          if [ -f "fastlane/metadata/android/en-US/changelogs/${{ env.BUILD_NUMBER }}.txt" ]; then
            CHANGELOG_CONTENT_EN=$(cat "fastlane/metadata/android/en-US/changelogs/${{ env.BUILD_NUMBER }}.txt")
            echo "CHANGELOG_EN<<GHADELIMITER" >> $GITHUB_ENV
            echo "$CHANGELOG_CONTENT_EN" >> $GITHUB_ENV
            echo "GHADELIMITER" >> $GITHUB_ENV
          else
            echo "CHANGELOG_EN=" >> $GITHUB_ENV
          fi
      
      - name: Create Production Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ env.VERSION_TAG }}
          name: "📝 Simple Notes Sync v${{ env.VERSION_NAME }}"
          files: apk-output/*.apk
          draft: false
          prerelease: false
          generate_release_notes: false
          body: |
            ## 📋 Changelog / Release Notes
            
            ${{ env.CHANGELOG_EN }}
            
            <details>
            <summary>🇩🇪 German Version</summary>
            
            ${{ env.CHANGELOG_DE }}
            
            </details>

            ## 📦 Downloads
            
            | Variant | File | Info |
            |---------|------|------|
            | **🏆 Recommended** | `simple-notes-sync-v${{ env.VERSION_NAME }}-standard.apk` | Standard version (works on all devices) |
            | F-Droid | `simple-notes-sync-v${{ env.VERSION_NAME }}-fdroid.apk` | For F-Droid Store |
            
            ## 📊 Build Info
            
            - **Version:** ${{ env.VERSION_NAME }} (Code: ${{ env.BUILD_NUMBER }})
            - **Date:** ${{ env.COMMIT_DATE }}
            - **Commit:** ${{ env.SHORT_SHA }}
            
            ## 🔐 APK Signature Verification
            
            All APKs are signed with the official release certificate.
            
            **Recommended:** Verify with [AppVerifier](https://github.com/nicholson-lab/AppVerifier) (Android app)
            
            **Expected SHA-256:**
            ```
            42:A1:C6:13:BB:C6:73:04:5A:F3:DC:81:91:BF:9C:B6:45:6E:E4:4C:7D:CE:40:C7:CF:B5:66:FA:CB:69:F1:6A
            ```
            
            **[📖 Documentation](https://github.com/inventory69/simple-notes-sync/blob/main/QUICKSTART.md)** · **[🐛 Report Bug](https://github.com/inventory69/simple-notes-sync/issues)**
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
