name: Build and Release

on:
  push:
    branches:
      - main
      - dev
    tags:
      - 'v*'
  pull_request:
    branches:
      - main
      - dev

jobs:
  build-arm64:
    name: Build APK (ARM64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Build ARM64 APK
        run: flutter build apk --release --target-platform android-arm64 --split-per-abi

      - name: Upload ARM64 APK
        uses: actions/upload-artifact@v4
        with:
          name: apk-arm64
          path: build/app/outputs/flutter-apk/app-arm64-v8a-release.apk
          retention-days: 30

  build-arm32:
    name: Build APK (ARM32)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Build ARM32 APK
        run: flutter build apk --release --target-platform android-arm --split-per-abi

      - name: Upload ARM32 APK
        uses: actions/upload-artifact@v4
        with:
          name: apk-arm32
          path: build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk
          retention-days: 30

  build-x64:
    name: Build APK (x86_64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Build x86_64 APK
        run: flutter build apk --release --target-platform android-x64 --split-per-abi

      - name: Upload x86_64 APK
        uses: actions/upload-artifact@v4
        with:
          name: apk-x64
          path: build/app/outputs/flutter-apk/app-x86_64-release.apk
          retention-days: 30

  release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: [build-arm64, build-arm32, build-x64]
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download ARM64 APK
        uses: actions/download-artifact@v4
        with:
          name: apk-arm64
          path: ./apks

      - name: Download ARM32 APK
        uses: actions/download-artifact@v4
        with:
          name: apk-arm32
          path: ./apks

      - name: Download x86_64 APK
        uses: actions/download-artifact@v4
        with:
          name: apk-x64
          path: ./apks

      - name: Generate version
        id: version
        run: |
          if [[ "${{ github.ref }}" == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          else
            VERSION="v0.0.${{ github.run_number }}"
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

      - name: Get file sizes
        id: sizes
        run: |
          ARM64_SIZE=$(du -h ./apks/app-arm64-v8a-release.apk | cut -f1)
          ARM32_SIZE=$(du -h ./apks/app-armeabi-v7a-release.apk | cut -f1)
          X64_SIZE=$(du -h ./apks/app-x86_64-release.apk | cut -f1)
          
          echo "arm64_size=$ARM64_SIZE" >> $GITHUB_OUTPUT
          echo "arm32_size=$ARM32_SIZE" >> $GITHUB_OUTPUT
          echo "x64_size=$X64_SIZE" >> $GITHUB_OUTPUT

      - name: Create Release Notes
        id: notes
        run: |
          cat << EOF > release_notes.md
          ## NeoMovies Mobile ${{ steps.version.outputs.version }}

          **Build Info:**
          - Commit: \`${{ github.sha }}\`
          - Branch: \`${{ github.ref_name }}\`
          - Workflow Run: [#${{ github.run_number }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})

          **Downloads:**
          - **ARM64 (arm64-v8a)**: \`app-arm64-v8a-release.apk\` (${{ steps.sizes.outputs.arm64_size }}) - Recommended for modern devices
          - **ARM32 (armeabi-v7a)**: \`app-armeabi-v7a-release.apk\` (${{ steps.sizes.outputs.arm32_size }}) - For older devices
          - **x86_64**: \`app-x86_64-release.apk\` (${{ steps.sizes.outputs.x64_size }}) - For emulators

          ### What's Changed
          See the [full commit history](${{ github.server_url }}/${{ github.repository }}/compare/${{ github.event.before }}...${{ github.sha }})
          EOF

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ steps.version.outputs.version }}
          name: NeoMovies ${{ steps.version.outputs.version }}
          body_path: release_notes.md
          draft: false
          prerelease: ${{ github.ref != 'refs/heads/main' && !startsWith(github.ref, 'refs/tags/') }}
          files: |
            ./apks/app-arm64-v8a-release.apk
            ./apks/app-armeabi-v7a-release.apk
            ./apks/app-x86_64-release.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Summary
        run: |
          echo "## Release Created Successfully!" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** ${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "**Release URL:** ${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### APK Files:" >> $GITHUB_STEP_SUMMARY
          echo "- ARM64: ${{ steps.sizes.outputs.arm64_size }}" >> $GITHUB_STEP_SUMMARY
          echo "- ARM32: ${{ steps.sizes.outputs.arm32_size }}" >> $GITHUB_STEP_SUMMARY
          echo "- x86_64: ${{ steps.sizes.outputs.x64_size }}" >> $GITHUB_STEP_SUMMARY