name: Build NeoMovies Mobile

on:
  push:
    branches: [ dev, feature/torrent-engine-integration ]
  pull_request:
    branches: [ dev ]
  workflow_dispatch:

env:
  FLUTTER_VERSION: '3.35.5'
  JAVA_VERSION: '17'

jobs:
  # ============================================
  # Сборка TorrentEngine модуля
  # ============================================
  build-torrent-engine:
    name: Build TorrentEngine Library
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'
          cache: 'gradle'
      
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3
        with:
          gradle-version: wrapper
      
      - name: Build TorrentEngine AAR
        working-directory: android
        run: |
          ./gradlew :torrentengine:assembleRelease \
            --no-daemon \
            --parallel \
            --build-cache \
            -Dorg.gradle.jvmargs="-Xmx2g -XX:MaxMetaspaceSize=512m"
      
      - name: Upload TorrentEngine AAR
        uses: actions/upload-artifact@v4
        with:
          name: torrentengine-aar
          path: android/torrentengine/build/outputs/aar/*.aar
          retention-days: 7

  # ============================================
  # Сборка Debug APK
  # ============================================
  build-debug-apk:
    name: Build Debug APK
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
          cache: true
      
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'
          cache: 'gradle'
      
      - name: Flutter Doctor
        run: flutter doctor -v
      
      - name: Get Flutter dependencies
        run: flutter pub get
      
      - name: Build Debug APK
        run: |
          flutter build apk \
            --debug \
            --target-platform android-arm64
      
      - name: Upload Debug APK
        uses: actions/upload-artifact@v4
        with:
          name: debug-apk
          path: build/app/outputs/flutter-apk/app-debug.apk
          retention-days: 7

  # ============================================
  # Сборка Release APK
  # ============================================
  build-release-apk:
    name: Build Release APK
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/dev'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
          cache: true
      
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'
          cache: 'gradle'
      
      - name: Get Flutter dependencies
        run: flutter pub get
      
      - name: Build Release APK (split per ABI)
        run: |
          flutter build apk \
            --release \
            --split-per-abi \
            --target-platform android-arm64
      
      - name: Upload Release APK (ARM64)
        uses: actions/upload-artifact@v4
        with:
          name: release-apk-arm64
          path: build/app/outputs/flutter-apk/app-arm64-v8a-release.apk
          retention-days: 30

  # ============================================
  # Анализ кода
  # ============================================
  code-quality:
    name: Code Quality Checks
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
          cache: true
      
      - name: Get Flutter dependencies
        run: flutter pub get
      
      - name: Flutter Analyze
        run: flutter analyze --no-fatal-infos
        continue-on-error: true
      
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'
      
      - name: Android Lint
        working-directory: android
        run: ./gradlew lint --no-daemon
        continue-on-error: true
      
      - name: Upload Lint Reports
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: lint-reports
          path: |
            android/app/build/reports/lint-results*.html
            android/torrentengine/build/reports/lint-results*.html
          retention-days: 7
