name: Mac App Store release
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version'
        required: true

jobs:
  code_quality:
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v6
      - name: set up JDK 18
        uses: actions/setup-java@v5
        with:
          distribution: 'zulu'
          java-version: 18
          cache: gradle
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5
      - name: Unit tests
        run: ./gradlew testDebugUnitTest
      - name: Static checks
        run: ./gradlew spotlessCheck
  build_and_release:
    needs: code_quality
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v6

      - name: set up JDK 18
        uses: actions/setup-java@v5
        with:
          distribution: 'zulu'
          java-version: 18
          cache: gradle

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v5
        with:
          gradle-home-cache-cleanup: true
          cache-encryption-key: ${{ secrets.GRADLE_CACHE_ENCRYPTION_KEY }}

      - name: Cache KMP tooling
        uses: actions/cache@v5
        with:
          path: |
            ~/.konan
          key: ${{ runner.os }}-v1-${{ hashFiles('*.versions.toml') }}

      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable

      - name: Import certs
        uses: apple-actions/import-codesign-certs@v6
        with:
          p12-file-base64: ${{ secrets.MAC_CERTIFICATES_P12 }}
          p12-password: ${{ secrets.MAC_CERTIFICATES_PASSWORD }}

      - name: Create Embedded Provision Profile
        run: |
          echo "$EMBEDDED_PROVISION" > desktopApp/embedded.provisionprofile.b64
          base64 -d -i desktopApp/embedded.provisionprofile.b64 > desktopApp/embedded.provisionprofile
        env:
          EMBEDDED_PROVISION: ${{ secrets.MAC_EMBEDDED_PROVISION }}

      - name: Create Runtime Provision Profile
        run: |
          echo "$RUNTIME_PROVISION" > desktopApp/runtime.provisionprofile.b64
          base64 -d -i desktopApp/runtime.provisionprofile.b64 > desktopApp/runtime.provisionprofile
        env:
          RUNTIME_PROVISION: ${{ secrets.MAC_RUNTIME_PROVISION }}

      - name: Create PKG
        run: ./gradlew packageReleasePkg -PmacOsAppStoreRelease=true

      - uses: Apple-Actions/upload-testflight-build@v4
        with:
          app-type: 'osx'
          app-path: desktopApp/build/compose/binaries/main-release/pkg/CPU-Info-${{ github.event.inputs.version }}.pkg
          issuer-id: ${{ secrets.IOS_APPSTORE_ISSUER_ID }}
          api-key-id: ${{ secrets.IOS_APPSTORE_KEY_ID }}
          api-private-key: ${{ secrets.IOS_APPSTORE_PRIVATE_KEY }}
