name: Build Android APK

on:
  workflow_dispatch:

permissions:
  contents: write

jobs:
  direct_build:
    name: Build Android TV APK
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "yarn"

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Get version from package.json
        id: package-version
        run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT

      - name: Setup JDK
        uses: actions/setup-java@v3
        with:
          distribution: "zulu"
          java-version: "17"

      - name: Prebuild TV App
        run: yarn prebuild

      - name: Build TV APK
        run: yarn build

      - name: Rename APK file
        run: |
          mkdir -p artifacts
          cp android/app/build/outputs/apk/release/app-release.apk artifacts/orionTV.${{ steps.package-version.outputs.version }}.apk

      - name: Create Release and Upload APK
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ steps.package-version.outputs.version }}
          name: Release v${{ steps.package-version.outputs.version }}
          body: Automated release for version v${{ steps.package-version.outputs.version }}.
          draft: false
          prerelease: false
          files: artifacts/orionTV.${{ steps.package-version.outputs.version }}.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
