name: CI

on:
  pull_request:
  push:
    branches:
      - main

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  changes:
    name: Detect changed paths
    runs-on: ubuntu-latest
    permissions:
      pull-requests: read
    outputs:
      dart: ${{ steps.filter.outputs.dart }}
      go: ${{ steps.filter.outputs.go }}
      android: ${{ steps.filter.outputs.android }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Filter paths
        uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            dart:
              - 'lib/**'
              - 'test/**'
              - 'assets/**'
              - 'pubspec.yaml'
              - 'pubspec.lock'
              - 'analysis_options.yaml'
              - 'l10n.yaml'
              - '.github/workflows/ci.yml'
            go:
              - 'go_backend/**'
              - '.github/workflows/ci.yml'
            android:
              - 'android/**'
              - 'go_backend/**'
              - 'lib/models/settings.dart'
              - 'lib/providers/download_queue_provider*.dart'
              - 'lib/services/download_request_payload.dart'
              - 'lib/services/history_database.dart'
              - 'lib/services/platform_bridge.dart'
              - 'pubspec.yaml'
              - 'pubspec.lock'
              - '.github/workflows/ci.yml'

  flutter:
    name: Flutter analyze & test
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.dart == 'true'

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

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

      - name: Cache pub dependencies
        uses: actions/cache@v5
        with:
          path: ~/.pub-cache
          key: pub-${{ runner.os }}-${{ hashFiles('pubspec.lock') }}
          restore-keys: pub-${{ runner.os }}-

      - name: Get Flutter dependencies
        run: flutter pub get

      - name: Analyze
        run: flutter analyze

      - name: Run tests
        run: flutter test

  go:
    name: Go vet & test
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.go == 'true'

    defaults:
      run:
        working-directory: go_backend

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          # Keep in sync with release.yml
          go-version: "1.26.5"
          cache-dependency-path: go_backend/go.sum

      - name: Check formatting
        run: test -z "$(gofmt -l .)"

      - name: Vet
        run: go vet ./...

      - name: Run tests
        run: go test ./...

  android:
    name: Android compile & native tests
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.android == 'true'

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Java
        uses: actions/setup-java@v5
        with:
          distribution: "temurin"
          java-version: "25"

      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version: "1.26.5"
          cache-dependency-path: go_backend/go.sum

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

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5
        with:
          gradle-version: "9.6.1"

      - name: Install Android SDK & NDK
        run: |
          yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses || true
          $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager \
            "ndk;29.0.14206865" \
            "platforms;android-37.0" \
            "build-tools;37.0.0"
          echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/29.0.14206865" >> "$GITHUB_ENV"

      - name: Build Go backend for Android
        working-directory: go_backend
        run: |
          go install golang.org/x/mobile/cmd/gomobile
          gomobile init
          mkdir -p ../android/app/libs
          gomobile bind \
            -target=android/arm,android/arm64 \
            -androidapi 24 \
            -o ../android/app/libs/gobackend.aar \
            .
        env:
          CGO_ENABLED: 1

      - name: Get Flutter dependencies
        run: flutter pub get

      - name: Configure Flutter SDK for Gradle
        run: echo "flutter.sdk=$FLUTTER_ROOT" > android/local.properties

      - name: Compile Kotlin and run native unit tests
        run: gradle -p android :app:compileDebugKotlin :app:testDebugUnitTest
