name: MacOS (Intel/Apple Silicon) Nightly Build

on:
  workflow_dispatch:
    inputs:
      release_upload_url:
        description: 'Release upload URL'
        required: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build MacOS Apps
    runs-on: macos-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 'lts/*'
          cache: 'npm'
          
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        
      - name: Install Dependencies
        run: |
          rustup target add x86_64-apple-darwin
          rustup target add aarch64-apple-darwin
          npm install
          
      - name: Set up keychain
        run: |
          security create-keychain -p "" build.keychain
          security default-keychain -s build.keychain
          security unlock-keychain -p "" build.keychain
          echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/certificate.p12
          security import /tmp/certificate.p12 -k build.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
          security set-key-partition-list -S apple-tool:,apple: -s -k "" build.keychain
        env:
          MACOS_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE }}
          MACOS_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PASSWORD }}
          
      - name: Build Frontend
        run: npm run build
        
      - name: Build for Intel Mac
        run: |
          echo "Building for Intel Mac..."
          npm run tauri build -- --target x86_64-apple-darwin --bundles dmg --config "{\"bundle\":{\"macOS\":{\"signingIdentity\": \"Developer ID Application: Abdenasser Elidrissi (785JV74B9Y)\"}}}"
          # Rename the Intel build and store the filename
          cd src-tauri/target/x86_64-apple-darwin/release/bundle/dmg/
          for f in *.dmg; do 
            mv "$f" "intel-$f"
            echo "INTEL_DMG_PATH=src-tauri/target/x86_64-apple-darwin/release/bundle/dmg/intel-$f" >> $GITHUB_ENV
          done
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          
      - name: Build for Apple Silicon
        run: |
          echo "Building for aarch64..."
          npm run tauri build -- --target aarch64-apple-darwin --bundles dmg --config "{\"bundle\":{\"macOS\":{\"signingIdentity\": \"Developer ID Application: Abdenasser Elidrissi (785JV74B9Y)\"}}}"
          # Rename the Apple Silicon build and store the filename
          cd src-tauri/target/aarch64-apple-darwin/release/bundle/dmg/
          for f in *.dmg; do 
            mv "$f" "silicon-$f"
            echo "SILICON_DMG_PATH=src-tauri/target/aarch64-apple-darwin/release/bundle/dmg/silicon-$f" >> $GITHUB_ENV
          done
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          
      - name: Get version from package.json
        id: version
        run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT

      - name: Upload Intel Build to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.INTEL_DMG_PATH }}
          asset_name: intel-NeoHtop_${{ steps.version.outputs.version }}_x64.dmg
          asset_content_type: application/x-apple-diskimage

      - name: Upload Silicon Build to Release
        if: github.event.inputs.release_upload_url != ''
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
        with:
          upload_url: ${{ github.event.inputs.release_upload_url }}
          asset_path: ${{ env.SILICON_DMG_PATH }}
          asset_name: silicon-NeoHtop_${{ steps.version.outputs.version }}_aarch64.dmg
          asset_content_type: application/x-apple-diskimage