name: Compile macOS binaries

on:
  push:
    branches: [ master, NET10 ]
  pull_request:
    branches: [ master ]
  release:
     types: 
       - published
       - prereleased
  workflow_dispatch:

jobs:
  build-universal:
    runs-on: macos-26
    
    steps:
    - uses: actions/checkout@v6
    - name: Setup Xcode
      uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: "26.2"

    - name: Setup .NET
      uses: actions/setup-dotnet@v5
      with:
        global-json-file: global.json

    - name: Build NativeInterop.xcodeproj
      uses: sersoft-gmbh/xcodebuild-action@v4
      with:
        project: GalaxyBudsClient.Platform.OSX/Native/NativeInterop.xcodeproj
        scheme: NativeInterop
        destination: platform=macOS
        build-settings: CONFIGURATION_BUILD_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Release CONFIGURATION_TEMP_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Intermediates.noindex
        action: build

    - name: Restore workloads
      run: dotnet workload restore
    - name: Restore dependencies
      run: dotnet restore --configfile GalaxyBudsClient/nuget.config GalaxyBudsClient/GalaxyBudsClient.csproj
    - name: Build Universal
      run: dotnet publish -f net10.0-macos -o bin_osx -c Release --no-restore GalaxyBudsClient/GalaxyBudsClient.csproj

    - name: Upload artifacts
      uses: actions/upload-artifact@v6
      with:
        name: GalaxyBudsClient_osx-universal
        path: bin_osx/Galaxy Buds Manager-*.pkg

  build-x64:
    runs-on: macos-26
    
    steps:
    - uses: actions/checkout@v6
    - name: Setup Xcode
      uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: "26.2"

    - name: Setup .NET
      uses: actions/setup-dotnet@v5
      with:
        global-json-file: global.json
        
    - name: Build NativeInterop.xcodeproj
      uses: sersoft-gmbh/xcodebuild-action@v4
      with:
        project: GalaxyBudsClient.Platform.OSX/Native/NativeInterop.xcodeproj
        scheme: NativeInterop
        destination: platform=macOS,arch=x86_64
        build-settings: ARCHS=x86_64 ONLY_ACTIVE_ARCH=NO CONFIGURATION_BUILD_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Release CONFIGURATION_TEMP_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Intermediates.noindex
        action: build
       
    - name: Restore workloads
      run: dotnet workload restore
    - name: Restore dependencies
      run: dotnet restore -r osx-x64 --configfile GalaxyBudsClient/nuget.config GalaxyBudsClient/GalaxyBudsClient.csproj
    - name: Build x64
      run: dotnet publish -f net10.0-macos -r osx-x64 -o bin_osxx64 -c Release --self-contained true --no-restore GalaxyBudsClient/GalaxyBudsClient.csproj

    - name: Upload artifacts
      uses: actions/upload-artifact@v6
      with:
        name: GalaxyBudsClient_osx-x64
        path: bin_osxx64/Galaxy Buds Manager-*.pkg

  build-arm64:
    runs-on: macos-26
    
    steps:
    - uses: actions/checkout@v6
    - name: Setup Xcode
      uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: "26.2"

    - name: Setup .NET
      uses: actions/setup-dotnet@v5
      with:
        global-json-file: global.json
        
    - name: Build NativeInterop.xcodeproj
      uses: sersoft-gmbh/xcodebuild-action@v4
      with:
        project: GalaxyBudsClient.Platform.OSX/Native/NativeInterop.xcodeproj
        scheme: NativeInterop
        destination: platform=macOS,arch=arm64
        build-settings: ARCHS=arm64 ONLY_ACTIVE_ARCH=NO CONFIGURATION_BUILD_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Release CONFIGURATION_TEMP_DIR=/Users/runner/work/GalaxyBudsClient/GalaxyBudsClient/GalaxyBudsClient.Platform.OSX/Native/Build/Intermediates.noindex
        action: build

    - name: Restore workloads
      run: dotnet workload restore
    - name: Restore dependencies
      run: dotnet restore -r osx-arm64 --configfile GalaxyBudsClient/nuget.config GalaxyBudsClient/GalaxyBudsClient.csproj
    - name: Build arm64
      run: dotnet publish -f net10.0-macos -r osx-arm64 -o bin_osxarm64 -c Release --self-contained true --no-restore GalaxyBudsClient/GalaxyBudsClient.csproj

    - name: Upload artifacts
      uses: actions/upload-artifact@v6
      with:
        name: GalaxyBudsClient_osx-arm64
        path: bin_osxarm64/Galaxy Buds Manager-*.pkg


  attach-to-release:
    runs-on: ubuntu-latest
    needs: [build-universal, build-x64, build-arm64]
    if: github.event_name == 'release'
     
    steps:
    - name: Download setup artifact (Universal)
      uses: actions/download-artifact@v7
      with:
        name: GalaxyBudsClient_osx-universal

    - name: Rename (Universal)
      run: mv "Galaxy Buds Manager"-*.pkg GalaxyBudsClient_macOS_universal.pkg

    - name: Download setup artifact (x64)
      uses: actions/download-artifact@v7
      with:
        name: GalaxyBudsClient_osx-x64
    
    - name: Rename (x64)
      run: mv "Galaxy Buds Manager"-*.pkg GalaxyBudsClient_macOS_x64.pkg

    - name: Download artifact (arm64)
      uses: actions/download-artifact@v7
      with:
        name: GalaxyBudsClient_osx-arm64

    - name: Rename (arm64)
      run: mv "Galaxy Buds Manager"-*.pkg GalaxyBudsClient_macOS_arm64.pkg

    - uses: AButler/upload-release-assets@v3.0.1
      with:
        files: '*.pkg'
        repo-token: ${{ secrets.GH_TOKEN }}
