name: Flutter Main CI

on:
  pull_request:
    branches:
      - 'main'

jobs:
  android:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: recursive

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

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.44.3'
          channel: 'stable'
          cache: true

      - name: Setup Rust
        uses: ./.github/actions/setup-rust
        with:
          targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android

      - name: Install dependencies
        run: flutter pub get

      - name: Flutter analyze
        run: flutter analyze

      - name: Configure Gradle
        run: |
          mkdir -p ~/.gradle
          echo "org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=1024m" >> ~/.gradle/gradle.properties
          echo "kotlin.daemon.jvmargs=-Xmx1536m" >> ~/.gradle/gradle.properties
          echo "org.gradle.caching=true" >> ~/.gradle/gradle.properties
          echo "org.gradle.parallel=true" >> ~/.gradle/gradle.properties
          echo "org.gradle.configureondemand=true" >> ~/.gradle/gradle.properties

      - name: Setup release signing
        env:
          KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
        run: |
          if [ -z "$KEYSTORE_BASE64" ]; then
            echo "KEYSTORE_BASE64 secret not set — release will be debug-signed."
            exit 0
          fi
          echo "$KEYSTORE_BASE64" | base64 -d > android/app/qlyra-release.jks
          {
            echo "storePassword=$KEYSTORE_PASSWORD"
            echo "keyPassword=$KEY_PASSWORD"
            echo "keyAlias=$KEY_ALIAS"
            echo "storeFile=qlyra-release.jks"
          } > android/key.properties
          echo "Release signing configured."

      - name: Build Android APK
        run: flutter build apk --release --flavor qlyra

  linux:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.44.3'
          channel: 'stable'

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

      - name: Setup Rust
        uses: ./.github/actions/setup-rust
        with:
          targets: x86_64-unknown-linux-gnu

      - name: Install dependencies
        run: flutter pub get

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

  windows:
    runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.44.3'
          channel: 'stable'

      - name: Setup Rust
        uses: ./.github/actions/setup-rust
        with:
          targets: x86_64-pc-windows-msvc

      - name: Install dependencies
        run: flutter pub get

      - name: Build Windows
        run: flutter build windows --release

  apple:
    runs-on: macos-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.44.3'
          channel: 'stable'

      - name: Setup Rust
        uses: ./.github/actions/setup-rust
        with:
          targets: aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios,aarch64-apple-darwin,x86_64-apple-darwin

      - name: Install dependencies
        run: flutter pub get

      - name: Build iOS
        run: |
          flutter config --no-enable-swift-package-manager
          flutter build ios --release --no-codesign

      - name: Fix ogg_opus_player macOS libs
        run: ./scripts/fix_opus_macos.sh

      - name: Build macOS
        run: flutter build macos --release
