name: Flutter Build (Signed APK)
on: [push, pull_request, workflow_dispatch]

env:
  FLUTTER_PATH: "flutter" # Путь к подмодулю Flutter

jobs:
  build:
    runs-on: docker
    container:
      image: "data.forgejo.org/oci/node:20-bullseye"
      env:
        TAR_OPTIONS: "--no-same-owner"
    # defaults:
    #   run:
    #     working-directory: /home/runner/openstore
    steps:
      # 1. Checkout репозиторий + подмодули
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: recursive
          # fetch-depth: 0

      - name: Pull flutter tags
        run: |
          cd flutter && git fetch --tags --depth=1 && cd ..

      - name: Set up JDK 21
        uses: https://github.com/actions/setup-java@v3
        with:
          java-version: "21"
          distribution: "temurin"

      # - name: Cache Android SDK
      #   uses: actions/cache@v3
      #   with:
      #     path: |
      #       ~/android-sdk
      #       ~/.android
      #     key: ${{ runner.os }}-android-${{ hashFiles('android/build.gradle.kts') }}

      - name: Setup Android SDK
        uses: https://github.com/android-actions/setup-android@v3

      # - name: Cache Flutter
      #   uses: actions/cache@v3
      #   with:
      #     path: |
      #       ./flutter/bin/cache
      #     key: ${{ runner.os }}-flutter-${{ hashFiles('.gitmodules') }}

      - name: Flutter install and check
        run: ./flutter/bin/flutter doctor -v

      # 3. Создаем key.properties и загружаем ключ (секреты Forgejo)
      - name: Setup signing keys
        run: |
          # Создаем директорию для ключа
          mkdir -p android/app

          # Создаем key.properties
          echo "storePassword=${{ secrets.KEY_PASSWORD }}" > android/key.properties
          echo "keyPassword=${{ secrets.KEY_PASSWORD }}" >> android/key.properties
          echo "keyAlias=upload" >> android/key.properties
          echo "storeFile=key.jks" >> android/key.properties

          # Декодируем base64-ключ (хранится в secrets.KEYSTORE_BASE64)
          echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 --decode > android/app/key.jks

      # 4. Кэширование
      # - name: Cache dependencies
      #   uses: actions/cache@v4
      #   with:
      #     path: |
      #       flutter/bin/cache
      #       .dart_tool
      #     key: ${{ runner.os }}-flutter-${{ hashFiles('pubspec.lock') }}

      # 6. Собираем подписанный APK

      - name: Cache Build
        uses: actions/cache@v3
        with:
          path: |
            ./android/.gradle
            ~/.gradle
          key: ${{ runner.os }}-android-${{ hashFiles('android/') }}

      - name: Build signed APK
        run: |
          ./flutter/bin/flutter build apk --release --split-per-abi -v

      - name: Print build dir
        run: pwd

      # 7. Сохраняем артефакты
      - name: Upload release apk
        uses: actions/upload-artifact@v3
        with:
          name: app-release
          path: build/app/outputs/flutter-apk/*-release.apk
