name: Android Kernel Build

#Запускаем только кнопкой "Run workflow" в Actions
on:
  workflow_dispatch:
  push:
    branches:
      - master
    paths:
      - 'app/**'
      - 'build.gradle.kts'
      - 'app/build.gradle.kts'
      - 'gradle/**'

jobs:
  build:
    runs-on: linux
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install Node.js
        run: |
          if command -v node &> /dev/null; then
            echo "Node.js is already installed"
          elif command -v apt-get &> /dev/null; then
            sudo apt-get update && sudo apt-get install -y nodejs npm
          elif command -v brew &> /dev/null; then
            brew install node
          elif command -v apk &> /dev/null; then
            apk add --no-cache nodejs npm
          fi

      - name: Install JDK 17
        run: |
          if ! java -version 2>&1 | grep -q "17"; then
            apt-get update && apt-get install -y openjdk-17-jdk
          fi
          JAVA_DIR=$(dirname $(dirname $(readlink -f $(which java))))
          echo "JAVA_HOME=$JAVA_DIR" >> $GITHUB_ENV
          export JAVA_HOME="$JAVA_DIR"
          echo "JAVA_HOME set to $JAVA_HOME"

      - name: Install Android SDK
        run: |
          export ANDROID_HOME="$HOME/android-sdk"
          mkdir -p "$ANDROID_HOME/cmdline-tools"
          if [ ! -d "$ANDROID_HOME/cmdline-tools/latest" ]; then
            OS_NAME="$(uname -s)"
            if [ "$OS_NAME" = "Darwin" ]; then
              SDK_URL="https://dl.google.com/android/repository/commandlinetools-mac-11076708_latest.zip"
            else
              SDK_URL="https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip"
            fi
            curl -sL "$SDK_URL" -o /tmp/cmdline-tools.zip
            unzip -q /tmp/cmdline-tools.zip -d "$ANDROID_HOME/cmdline-tools"
            mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest"
            rm /tmp/cmdline-tools.zip
          fi
          yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses || true
          "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \
            "platforms;android-34" \
            "build-tools;34.0.0" \
            "platform-tools"
          echo "ANDROID_HOME=$ANDROID_HOME" >> $GITHUB_ENV
          echo "ANDROID_SDK_ROOT=$ANDROID_HOME" >> $GITHUB_ENV

      - name: Restore debug keystore
        run: |
          mkdir -p ~/.android
          echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > ~/.android/debug.keystore

      - name: Create google-services.json
        run: echo "${{ secrets.GOOGLE_SERVICES_JSON }}" | base64 -d > app/google-services.json

      - name: Setup Gradle wrapper
        run: |
          chmod +x ./gradlew
          ./gradlew --version

      - name: Build Release APK
        run: ./gradlew assembleRelease

      - name: Check if APK exists
        run: |
          echo "=== Checking APK output ==="
          find app/build/outputs/apk/release -type f -name "*.apk" 2>/dev/null || echo "No APK files found"
          ls -la app/build/outputs/apk/release/ 2>/dev/null || echo "release folder not found"

      - name: Upload APK to SSH using SCP
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SDU_SSH_HOST }}
          username: ${{ secrets.SDU_SSH_USER }}
          password: ${{ secrets.SDU_SSH_PASSWORD }}
          port: ${{ secrets.SDU_SSH_PORT }}
          source: "app/build/outputs/apk/release/Rosetta-*.apk"
          target: "${{ secrets.SDU_DEPLOY_PATH }}/kernel/android/universal"
          strip_components: 5
          rm: true
