name: Build Flutter App

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

jobs:
  build-android:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      attestations: write
      contents: read
    steps:
      - uses: actions/checkout@v4

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

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

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

      - name: Cache Gradle
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Install dependencies
        run: flutter pub get

      - name: Build APK
        run: flutter build apk --release

      - name: Rename APK
        run: |
          cp build/app/outputs/flutter-apk/app-release.apk plezy-android.apk

      - name: Attest APK
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: plezy-android.apk

      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: plezy-android.apk

  build-ios:
    runs-on: macos-latest
    permissions:
      id-token: write
      attestations: write
      contents: read
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Cache CocoaPods
        uses: actions/cache@v4
        with:
          path: |
            ios/Pods
            ~/Library/Caches/CocoaPods
            ~/.cocoapods
          key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-pods-

      - name: Install dependencies
        run: flutter pub get

      - name: Build iOS (no codesign)
        run: flutter build ios --release --no-codesign

      - name: Create IPA
        run: |
          mkdir -p Payload
          cp -r build/ios/iphoneos/Runner.app Payload/
          zip -r plezy-ios.ipa Payload

      - name: Attest IPA
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: plezy-ios.ipa

      - name: Upload IPA
        uses: actions/upload-artifact@v4
        with:
          name: ios-ipa
          path: plezy-ios.ipa

  build-macos:
    runs-on: macos-latest
    permissions:
      id-token: write
      attestations: write
      contents: read
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Cache CocoaPods
        uses: actions/cache@v4
        with:
          path: |
            macos/Pods
            ~/Library/Caches/CocoaPods
            ~/.cocoapods
          key: ${{ runner.os }}-macos-pods-${{ hashFiles('**/Podfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-macos-pods-

      - name: Install dependencies
        run: flutter pub get

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

      - name: Create ZIP
        run: |
          cd build/macos/Build/Products/Release
          zip -r $GITHUB_WORKSPACE/plezy-macos.zip plezy.app

      - name: Attest macOS ZIP
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: plezy-macos.zip

      - name: Upload macOS ZIP
        uses: actions/upload-artifact@v4
        with:
          name: macos-zip
          path: plezy-macos.zip

  build-windows:
    runs-on: windows-latest
    permissions:
      id-token: write
      attestations: write
      contents: read
    steps:
      - uses: actions/checkout@v4

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

      - name: Cache Pub dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~\AppData\Local\Pub\Cache
          key: ${{ runner.os }}-pub-${{ hashFiles('**/pubspec.lock') }}
          restore-keys: |
            ${{ runner.os }}-pub-

      - name: Install dependencies
        run: flutter pub get

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

      - name: Build Windows Installer
        run: .\windows\build-installer.ps1

      - name: Attest Windows artifacts
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: |
            plezy-windows-portable.zip
            plezy-windows-installer.exe

      - name: Upload Portable Archive
        uses: actions/upload-artifact@v4
        with:
          name: windows-portable
          path: plezy-windows-portable.zip

      - name: Upload Installer
        uses: actions/upload-artifact@v4
        with:
          name: windows-installer
          path: plezy-windows-installer.exe

  build-linux:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      attestations: write
      contents: read
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Cache APT packages
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev libstdc++-12-dev libasound2-dev libmpv-dev mpv keybinder-3.0
          version: 1.0

      - name: Install Linux dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev libstdc++-12-dev libasound2-dev libmpv-dev mpv keybinder-3.0

      - name: Install dependencies
        run: flutter pub get

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

      - name: Create Archive
        run: |
          cd build/linux/x64/release/bundle
          tar -czf $GITHUB_WORKSPACE/plezy-linux.tar.gz *

      - name: Attest Linux tarball
        uses: actions/attest-build-provenance@v2
        with:
          subject-path: plezy-linux.tar.gz

      - name: Upload Linux App
        uses: actions/upload-artifact@v4
        with:
          name: linux-app
          path: plezy-linux.tar.gz

  create-release:
    needs: [build-android, build-ios, build-macos, build-windows, build-linux]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Display structure of downloaded files
        run: ls -R artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            artifacts/android-apk/plezy-android.apk
            artifacts/ios-ipa/plezy-ios.ipa
            artifacts/macos-zip/plezy-macos.zip
            artifacts/windows-portable/plezy-windows-portable.zip
            artifacts/windows-installer/plezy-windows-installer.exe
            artifacts/linux-app/plezy-linux.tar.gz
          draft: true
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
