name: Test and Analyze

on:
  push:
    branches:
      - main
      - dev
      - 'feature/**'
      - 'torrent-engine-downloads'
  pull_request:
    branches:
      - main
      - dev

jobs:
  flutter-analyze:
    name: Flutter Analyze
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.19.6'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Run code generation
        run: |
          dart run build_runner build --delete-conflicting-outputs || true

      - name: Run Flutter Analyze
        run: flutter analyze --no-fatal-infos

      - name: Check formatting
        run: dart format --set-exit-if-changed .

  flutter-test:
    name: Flutter Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Run tests
        run: flutter test --coverage

      - name: Run Integration tests  
        run: flutter test test/integration/ --reporter=expanded
        env:
          # Mark that we're running in CI
          CI: true
          GITHUB_ACTIONS: true

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          files: ./coverage/lcov.info
          fail_ci_if_error: false
        if: always()

  android-lint:
    name: Android Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Run Android Lint
        run: |
          cd android
          chmod +x gradlew
          ./gradlew lint

      - name: Upload lint reports
        uses: actions/upload-artifact@v4
        with:
          name: android-lint-reports
          path: |
            android/app/build/reports/lint-*.html
            android/torrentengine/build/reports/lint-*.html
          retention-days: 7
        if: always()

  build-debug:
    name: Build Debug APK
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' || github.ref == 'refs/heads/dev'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '17'

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.35.5'
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Build Debug APK
        run: flutter build apk --debug

      - name: Upload Debug APK
        uses: actions/upload-artifact@v4
        with:
          name: apk-debug
          path: build/app/outputs/flutter-apk/app-debug.apk
          retention-days: 7