name: 🤖 Android APK Build (Phone + TV)

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
  pull_request:
    branches: [develop, master]
  push:
    branches: [develop, master]

jobs:
  build-android:
    runs-on: ubuntu-24.04
    name: 🏗️ Build Android APK
    permissions:
      contents: read

    strategy:
      fail-fast: false
      matrix:
        target: [phone, tv]

    steps:
      - name: 📥 Checkout code
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          show-progress: false
          submodules: recursive
          fetch-depth: 0

      - name: 🍞 Setup Bun
        uses: oven-sh/setup-bun@735343b667d3e6f658f44d0eca948eb6282f2b76 # v2.0.2
        with:
          bun-version: latest

      - name: 💾 Cache Bun dependencies
        uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-cache-${{ hashFiles('bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-cache-

      - name: 📦 Install dependencies and reload submodules
        run: |
          bun install --frozen-lockfile
          bun run submodule-reload

      - name: 💾 Cache Android dependencies
        uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
        with:
          path: android/.gradle
          key: ${{ runner.os }}-android-deps-${{ hashFiles('android/**/build.gradle', 'android/gradle/wrapper/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-android-deps-

      - name: 🛠️ Generate project files
        run: |
          if [ "${{ matrix.target }}" = "tv" ]; then
            bun run prebuild:tv
          else
            bun run prebuild
          fi

      - name: 🚀 Build APK
        env:
          EXPO_TV: ${{ matrix.target == 'tv' && 1 || 0 }}
        run: bun run build:android:local

      - name: 📅 Set date tag
        run: echo "DATE_TAG=$(date +%d-%m-%Y_%H-%M-%S)" >> $GITHUB_ENV

      - name: 📤 Upload APK artifact
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: streamyfin-android-${{ matrix.target }}-apk-${{ env.DATE_TAG }}
          path: |
            android/app/build/outputs/apk/release/*.apk
            android/app/build/outputs/bundle/release/*.aab
          retention-days: 7

