name: Release APKs

on:
  push:
    tags:
      - 'v*'

env:
  JAVA_VERSION: "21"

jobs:
  # ====================
  # Build Android TV APK
  # ====================
  build-tv:
    name: Build TV APK
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: ${{ env.JAVA_VERSION }}
          cache: 'gradle'
      
      - name: Grant execute permission
        run: chmod +x android-tv/gradlew
      
      - name: Build Debug APK
        run: |
          cd android-tv
          ./gradlew assembleDebug --no-daemon
      
      - name: Rename APK
        run: mv android-tv/app/build/outputs/apk/debug/app-debug.apk StreamFlix-TV.apk
      
      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: tv-apk
          path: StreamFlix-TV.apk

  # ====================
  # Build Android Mobile APK
  # ====================
  build-mobile:
    name: Build Mobile APK
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: ${{ env.JAVA_VERSION }}
          cache: 'gradle'

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json

      - name: Install dependencies
        run: |
          cd frontend
          npm ci

      - name: Build Web App
        run: |
          cd frontend
          npm run build

      - name: Sync Capacitor
        run: |
          cd frontend
          npx cap sync android --deployment

      - name: Grant execute permission
        run: chmod +x frontend/android/gradlew
      
      - name: Build Mobile APK
        run: |
          cd frontend/android
          ./gradlew assembleDebug --no-daemon
      
      - name: Rename APK
        run: mv frontend/android/app/build/outputs/apk/debug/app-debug.apk StreamFlix.apk
      
      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: mobile-apk
          path: StreamFlix.apk

  # ====================
  # Create GitHub Release
  # ====================
  release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: [build-tv, build-mobile]
    permissions:
      contents: write
    
    steps:
      - uses: actions/checkout@v4

      - name: Download TV APK
        uses: actions/download-artifact@v4
        with:
          name: tv-apk

      - name: Download Mobile APK
        uses: actions/download-artifact@v4
        with:
          name: mobile-apk

      - name: Get version from tag
        id: version
        run: echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: ${{ steps.version.outputs.version }} - StreamFlix Release
          files: |
            StreamFlix-TV.apk
            StreamFlix.apk
          generate_release_notes: true
          draft: false
          prerelease: false
