name: build-macos-arm

on:
  workflow_call:
    inputs:
      release:
        required: false
        type: boolean
        default: false
  workflow_dispatch:
    inputs:
      release:
        description: 'Set to true to create a release build'
        required: false
        type: boolean
        default: false

jobs:
  build-and-sign:
    runs-on: "macos-latest"
    permissions:
      contents: write
    defaults:
      run:
        working-directory: desktop
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Import Code Signing Certificate
        uses: apple-actions/import-codesign-certs@v6
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          APPLE_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE }}
          APPLE_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PWD }}
        with:
          p12-file-base64: ${{ secrets.MACOS_CERTIFICATE }}
          p12-password: ${{ secrets.MACOS_CERTIFICATE_PWD }}

      - name: Setup Cargo Cache
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: macos-arm-cargo-registry-${{ hashFiles('desktop/**/Cargo.lock') }}
          restore-keys: |
            macos-arm-cargo-registry-

      - name: Setup Node Cache
        uses: actions/cache@v5
        with:
          path: ~/.npm
          key: macos-arm-node-${{ hashFiles('desktop/**/package-lock.json') }}
          restore-keys: |
            macos-arm-node-

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: lts/*

      - name: Install cyclonedx-npm
        run: npm install -g @cyclonedx/cyclonedx-npm

      - name: install Rust stable
        uses: dtolnay/rust-toolchain@v1
        with:
          toolchain: stable
          targets: "aarch64-apple-darwin"

      - name: Install Modules
        run: |
          npm install
          cyclonedx-npm --output-format XML --gather-license-texts --output-file src-tauri/open-data-platform-SBOM-npm.cdx.xml

      - name: Install cargo-cyclonedx
        run: |
          cargo install cargo-cyclonedx
          cargo cyclonedx
          if [ -f "src-tauri/openbb-platform.cdx.xml" ]; then
            mv src-tauri/openbb-platform.cdx.xml "src-tauri/open-data-platform-SBOM-cargo.cdx.xml"
          fi

      - name: Build Tauri App
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          APPLE_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE }}
          APPLE_CERTIFICATE_PASSWORD: ${{ secrets.MACOS_CERTIFICATE_PWD }}
          APPLE_ID: ${{ secrets.NOTARIZE_APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.NOTARIZE_APPLE_PWD }}
          APPLE_TEAM_ID: ${{ secrets.NOTARIZE_APPLE_TEAM_ID }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
        run: npm run tauri build

      - name: Rename artifacts
        run: |
          VERSION=$(jq -r .version src-tauri/tauri.conf.json)
          cd target/release/bundle/dmg
          for file in *.dmg; do mv "$file" "Open-Data-Platform_${VERSION}_aarch64.dmg"; done
          cd ../macos
          for file in *.app.tar.gz; do mv "$file" "Open-Data-Platform_${VERSION}_aarch64.app.tar.gz"; done
          for file in *.app.tar.gz.sig; do mv "$file" "Open-Data-Platform_${VERSION}_aarch64.app.tar.gz.sig"; done

      - name: Upload Artifacts
        uses: actions/upload-artifact@v6
        with:
          name: macos-arm-artifacts
          path: |
            desktop/target/release/bundle/dmg/*.dmg
            desktop/target/release/bundle/macos/*.app.tar.gz
            desktop/target/release/bundle/macos/*.app.tar.gz.sig

      - name: Clean old bundle artifacts
        if: always()
        run: |
          bundlePath="${{ github.workspace }}/desktop/target/release/bundle"
          if [ -d "$bundlePath" ]; then
            rm -rf "$bundlePath"
          fi
