name: Pre-release (dev/0.5.0)

on:
  workflow_dispatch:
    inputs:
      build_number:
        description: 'Override build number (e.g. 13). Leave empty to use the auto run number.'
        required: false
        default: ''
  push:
    branches:
      - 'dev/0.5.0'

permissions:
  contents: write

env:
  FLUTTER_VERSION: '3.44.3'

jobs:
  android:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        flavor: [komet, oneme]
    steps:
      - uses: actions/checkout@v3

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'gradle'
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - 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/komet-release.jks
          {
            echo "storePassword=$KEYSTORE_PASSWORD"
            echo "keyPassword=$KEY_PASSWORD"
            echo "keyAlias=$KEY_ALIAS"
            echo "storeFile=komet-release.jks"
          } > android/key.properties
          echo "Release signing configured."

      - name: Build APKs and App Bundle (${{ matrix.flavor }})
        run: |
          flutter build apk --release --flavor ${{ matrix.flavor }} --obfuscate --split-debug-info=build/symbols
          flutter build apk --release --split-per-abi --flavor ${{ matrix.flavor }} --obfuscate --split-debug-info=build/symbols
          flutter build appbundle --release --flavor ${{ matrix.flavor }} --obfuscate --split-debug-info=build/symbols

      - name: Collect Android artifacts (${{ matrix.flavor }})
        run: |
          F=${{ matrix.flavor }}
          mkdir -p dist
          cp build/app/outputs/flutter-apk/app-$F-release.apk            dist/Komet-android-$F-universal.apk
          cp build/app/outputs/flutter-apk/app-arm64-v8a-$F-release.apk  dist/Komet-android-$F-arm64-v8a.apk
          cp build/app/outputs/flutter-apk/app-armeabi-v7a-$F-release.apk dist/Komet-android-$F-armeabi-v7a.apk
          cp build/app/outputs/flutter-apk/app-x86_64-$F-release.apk     dist/Komet-android-$F-x86_64.apk
          cp build/app/outputs/bundle/${F}Release/app-$F-release.aab     dist/Komet-android-$F.aab
          tar -czf dist/Komet-android-$F-symbols.tar.gz -C build/symbols .

      - name: Upload Android artifacts (${{ matrix.flavor }})
        uses: actions/upload-artifact@v4
        with:
          name: android-${{ matrix.flavor }}
          path: dist/*
          if-no-files-found: error
          retention-days: 7

  windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
      - name: Get dependencies
        run: flutter pub get
      - name: Build Windows
        run: flutter build windows --release
      - name: Package Windows
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path dist | Out-Null
          Compress-Archive -Path build/windows/x64/runner/Release/* -DestinationPath dist/Komet-windows-x64.zip
      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows
          path: dist/*
          if-no-files-found: error
          retention-days: 7

  linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          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 libmpv-dev mpv libopus-dev libogg-dev
      - name: Get dependencies
        run: flutter pub get
      - name: Build Linux
        run: flutter build linux --release
      - name: Package Linux
        run: |
          mkdir -p dist
          tar -C build/linux/x64/release/bundle -czf dist/Komet-linux-x64.tar.gz .
      - name: Upload Linux artifact
        uses: actions/upload-artifact@v4
        with:
          name: linux
          path: dist/*
          if-no-files-found: error
          retention-days: 7

  macos:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
      - name: Get dependencies
        run: flutter pub get
      - name: Fix opus/ogg libs for macOS
        run: ./scripts/fix_opus_macos.sh
      - name: Build macOS
        run: flutter build macos --release
      - name: Package macOS
        run: |
          mkdir -p dist
          APP=$(find build/macos/Build/Products/Release -maxdepth 1 -name "*.app" | head -1)
          ditto -c -k --keepParent "$APP" dist/Komet-macos.zip
      - name: Upload macOS artifact
        uses: actions/upload-artifact@v4
        with:
          name: macos
          path: dist/*
          if-no-files-found: error
          retention-days: 7

  ios:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}
          channel: 'stable'
      - name: Get dependencies
        run: flutter pub get
      - name: Build iOS (no codesign)
        run: |
          flutter config --no-enable-swift-package-manager
          flutter build ios --release --no-codesign
      - name: Package unsigned IPA
        run: |
          mkdir -p dist build/ios/Payload
          cp -R build/ios/iphoneos/Runner.app build/ios/Payload/
          (cd build/ios && zip -qr ../../dist/Komet-ios-unsigned.ipa Payload)
          rm -rf build/ios/Payload
      - name: Install ldid
        run: brew install ldid
      - name: Pseudo-sign with ldid
        run: |
          set -euo pipefail
          APP="build/ios/iphoneos/Runner.app"
          ENT="$(mktemp -t ent).plist"
          cat > "$ENT" <<'PLIST'
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
          <dict>
            <key>platform-application</key><true/>
            <key>get-task-allow</key><true/>
            <key>com.apple.private.security.no-container</key><true/>
          </dict>
          </plist>
          PLIST
          if [ -d "$APP/Frameworks" ]; then
            find "$APP/Frameworks" -type f -name "*.dylib" -print0 | xargs -0 -I{} ldid -S "{}"
            find "$APP/Frameworks" -type d -name "*.framework" | while read -r fw; do
              bin="$fw/$(basename "$fw" .framework)"
              [ -f "$bin" ] && ldid -S "$bin"
            done
          fi
          if [ -d "$APP/PlugIns" ]; then
            find "$APP/PlugIns" -type d -name "*.appex" | while read -r ext; do
              bin="$ext/$(basename "$ext" .appex)"
              [ -f "$bin" ] && ldid -S"$ENT" "$bin"
            done
          fi
          ldid -S"$ENT" "$APP/Runner"
      - name: Package IPA
        run: |
          mkdir -p dist build/ios/Payload
          cp -R build/ios/iphoneos/Runner.app build/ios/Payload/
          (cd build/ios && zip -qr ../../dist/Komet-ios-pseudosigned.ipa Payload)
      - name: Upload iOS artifact
        uses: actions/upload-artifact@v4
        with:
          name: ios
          path: dist/*
          if-no-files-found: error
          retention-days: 7

  release:
    needs: [android, windows, linux, macos, ios]
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Compute version
        id: ver
        run: |
          BRANCH_VERSION="${GITHUB_REF_NAME#dev/}"
          BUILD="${{ github.event.inputs.build_number }}"
          BUILD="${BUILD:-$GITHUB_RUN_NUMBER}"
          echo "tag=v${BRANCH_VERSION}-dev.${BUILD}" >> "$GITHUB_OUTPUT"
          echo "name=Komet ${BRANCH_VERSION}-dev (build ${BUILD})" >> "$GITHUB_OUTPUT"

      - name: List artifacts
        run: ls -lhR dist

      - name: Create pre-release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.ver.outputs.tag }}
          name: ${{ steps.ver.outputs.name }}
          target_commitish: ${{ github.sha }}
          prerelease: true
          generate_release_notes: true
          fail_on_unmatched_files: true
          files: dist/*
