# NeoMovies – GitHub Actions CI/CD for Flutter (Android APK + Linux desktop)
# Requires GitHub-hosted Ubuntu runners.

name: Flutter CI

on:
  push:
    branches: [ main ]
  pull_request:
  workflow_dispatch:
  release:
    types: [created]

env:
  FLUTTER_VERSION: "3.22.1"

jobs:
  # ---------------------------------------------------------------------------
  test:
    name: Test & Analyze
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Flutter ${{ env.FLUTTER_VERSION }} (beta)
        uses: subosito/flutter-action@v2
        with:
          channel: beta
          flutter-version: ${{ env.FLUTTER_VERSION }}

      - name: Get dependencies
        run: flutter pub get

      - name: Static analysis
        run: flutter analyze --no-pub --fatal-infos --fatal-warnings

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

      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage
          path: coverage/

  # ---------------------------------------------------------------------------
  build_android:
    name: Build Android APK
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: subosito/flutter-action@v2
        with:
          channel: beta
          flutter-version: ${{ env.FLUTTER_VERSION }}

      - name: Get dependencies
        run: flutter pub get

      - name: Build release APK & AAB
        run: |
          flutter build apk --release
          flutter build appbundle --release

      - name: Upload APK & AAB artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android-build
          path: |
            build/app/outputs/flutter-apk/app-release.apk
            build/app/outputs/bundle/release/app-release.aab

  # ---------------------------------------------------------------------------
  build_linux:
    name: Build Linux desktop bundle
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: subosito/flutter-action@v2
        with:
          channel: beta
          flutter-version: ${{ env.FLUTTER_VERSION }}

      - name: Install Linux build dependencies
        run: sudo apt-get update && sudo apt-get install -y libjsoncpp-dev libsecret-1-dev clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev

      - name: Enable Linux desktop and get deps
        run: |
          flutter config --enable-linux-desktop
          flutter pub get

      - name: Build Linux release bundle
        run: flutter build linux --release

      - name: Upload Linux bundle artifact
        uses: actions/upload-artifact@v4
        with:
          name: linux-build
          path: build/linux/x64/release/bundle/

  # ---------------------------------------------------------------------------
  release_assets:
    name: Attach assets to GitHub Release
    if: github.event_name == 'release'
    needs: [build_android, build_linux]
    runs-on: ubuntu-latest
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v4

      - name: Upload to GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            android-build/app-release.apk
            android-build/app-release.aab
            linux-build/**
