permissions:
  contents: write
name: Build Android Flutter App

on:
  push:
    tags:
      - "*"
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Manual override version tag (optional)"
        required: false

jobs:
  build:
    name: Build Android Release
    runs-on: ubuntu-latest

    steps:
      - name: Set Image Tag
        run: echo "IMAGE_TAG=${{ github.event.release.tag_name || github.event.inputs.version || 'latest' }}" >> $GITHUB_ENV

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Fetch full git history for accurate commit count
          token: ${{ secrets.GITHUB_TOKEN }}

      - 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 Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.35.2"
          channel: "stable"

      - name: Install dependencies
        run: |
          cd mobile
          flutter pub get

      - name: Setup Android signing
        run: |
          cd mobile/android
          echo "storePassword=${{ secrets.ANDROID_STORE_PASSWORD }}" > key.properties
          echo "keyPassword=${{ secrets.ANDROID_KEY_PASSWORD }}" >> key.properties
          echo "keyAlias=${{ secrets.ANDROID_KEY_ALIAS }}" >> key.properties
          echo "storeFile=../upload-keystore.jks" >> key.properties
          echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 -d > upload-keystore.jks

      - name: Verify version files
        run: |
          cd mobile
          echo "Current version in pubspec.yaml:"
          grep "^version:" pubspec.yaml
          echo "Current version in environment.dart:"
          grep "_projectVersion\|_build" lib/core/environment.dart
          echo "Build will use versions exactly as they are in the repository"


      - name: Build APK
        run: |
          cd mobile
          flutter build apk --release --split-per-abi

      - name: Build AAB
        run: |
          cd mobile
          flutter build appbundle --release

      - name: Rename APK files
        run: |
          cd mobile/build/app/outputs/flutter-apk
          # Extract version from IMAGE_TAG (remove 'v' prefix if present)
          VERSION=${IMAGE_TAG#v}
          if [[ "$VERSION" == "latest" ]]; then
            VERSION="0.0.0"
          fi
          
          # Rename APK files with proper naming convention
          mv app-armeabi-v7a-release.apk pinepods-armeabi-${VERSION}.apk
          mv app-arm64-v8a-release.apk pinepods-arm64-${VERSION}.apk  
          mv app-x86_64-release.apk pinepods-x86_64-${VERSION}.apk

      - name: Upload APK artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android-apk-builds
          path: mobile/build/app/outputs/flutter-apk/pinepods-*.apk

      - name: Upload AAB artifact
        uses: actions/upload-artifact@v4
        with:
          name: android-aab-build
          path: mobile/build/app/outputs/bundle/release/app-release.aab

      # - name: Upload to Google Play Store
      #   if: github.event_name == 'release'
      #   env:
      #     GOOGLE_PLAY_SERVICE_ACCOUNT_JSON: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON }}
      #   run: |
      #     echo "$GOOGLE_PLAY_SERVICE_ACCOUNT_JSON" > service-account.json
      #     # Install fastlane if needed for Play Store upload
      #     # gem install fastlane
      #     # fastlane supply --aab mobile/build/app/outputs/bundle/release/app-release.aab --json_key service-account.json --package_name com.gooseberrydevelopment.pinepods --track production
