name: Reusable macOS Build

on:
  workflow_call:

permissions:
  contents: read
  pull-requests: read

env:
  LUKKA_RUN_VCPKG_SHA: "${{ vars.LUKKA_RUN_VCPKG_SHA != '' && vars.LUKKA_RUN_VCPKG_SHA || 'b3dd708d38df5c856fe1c18dc0d59ab771f93921' }}"
  CMAKE_BUILD_PARALLEL_LEVEL: 2
  MAKEFLAGS: "-j 2"

jobs:
  build:
    if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
    name: ${{ matrix.os }}-${{ matrix.buildtype }}
    runs-on: ${{ matrix.os }}

    concurrency:
      group: otclient-${{ github.workflow }}-${{ github.ref }}-${{ matrix.buildtype }}
      cancel-in-progress: true

    strategy:
      fail-fast: false
      matrix:
        os: [macos-15]
        buildtype: [macos-release]
        include:
          - os: macos-15
            triplet: arm64-osx
            cmake_build_type: Release

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Get vcpkg commit ID
        id: vcpkg-step
        run: |
          vcpkgCommitId=$(grep '.builtin-baseline' vcpkg.json | awk -F: '{print $2}' | tr -d '," ')
          echo "vcpkgGitCommitId=$vcpkgCommitId" >> $GITHUB_OUTPUT

      - name: Compute vcpkg.json hash
        id: hash
        run: |
          hash=$(shasum -a 256 vcpkg.json | awk '{print toupper($1)}')
          echo "hash=$hash" >> $GITHUB_OUTPUT

      - name: Cache vcpkg binary artifacts
        uses: actions/cache@v4
        with:
          path: |
            ~/.cache/vcpkg/archives
            ${{ github.workspace }}/vcpkg/downloads
            ${{ github.workspace }}/vcpkg/installed
            ${{ github.workspace }}/vcpkg/buildtrees
          key: vcpkg-${{ matrix.os }}-${{ matrix.buildtype }}-${{ steps.hash.outputs.hash }}
          restore-keys: |
            vcpkg-${{ matrix.buildtype }}-

      - name: Setup Python
        id: python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Setup vcpkg
        uses: lukka/run-vcpkg@a400452f634fe49e9f18d388aeb1809dcc642136
        env:
          VCPKG_PYTHON3: ${{ steps.python.outputs.python-path }}
        with:
          vcpkgGitCommitId: ${{ steps.vcpkg-step.outputs.vcpkgGitCommitId }}
          vcpkgJsonGlob: 'vcpkg.json'

      - name: Validate vcpkg baseline SHA
        env:
          VCPKG_GIT_COMMIT_ID: ${{ steps.vcpkg-step.outputs.vcpkgGitCommitId }}
        run: |
          SHAS="${VCPKG_GIT_COMMIT_ID}"
          if [ -z "${SHAS}" ]; then
            echo "::error title=Missing vcpkg baseline::Provide a full 40-char vcpkgGitCommitId."
            exit 1
          fi
          if ! echo "${SHAS}" | grep -Eq '^[0-9a-f]{40}$'; then
            echo "::error title=Invalid vcpkg baseline::vcpkgGitCommitId must be a full 40-char commit SHA."
            exit 1
          fi

      - name: Install CMake and Ninja
        uses: lukka/get-cmake@v3.31.6

      - name: Cache CMake build directory
        uses: actions/cache@v4
        with:
          path: ${{ github.workspace }}/build-${{ matrix.buildtype }}
          key: cmake-dir-${{ matrix.buildtype }}
          restore-keys: |
            cmake-dir-${{ matrix.buildtype }}

      - name: Configure and Build
        env:
          BUILD_DIR: build-${{ matrix.buildtype }}
          CMAKE_BUILD_TYPE: ${{ matrix.cmake_build_type }}
          VCPKG_TARGET_TRIPLET: ${{ matrix.triplet }}
        run: |
          cmake -G Ninja -S . -B "${BUILD_DIR}" \
            -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
            -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake \
            -DVCPKG_TARGET_TRIPLET="${VCPKG_TARGET_TRIPLET}" \
            -DTOGGLE_BIN_FOLDER=ON \
            -DOPTIONS_ENABLE_IPO=OFF \
            -DOTCLIENT_BUILD_TESTS=OFF \
            -DCMAKE_CXX_FLAGS_RELEASE="-O2 -DNDEBUG" \
            -DCMAKE_C_FLAGS_RELEASE="-O2 -DNDEBUG" \
            -DTOGGLE_BOT_PROTECTION=OFF

          cmake --build "${BUILD_DIR}" --target otclient

      - name: Create app bundle archive
        env:
          BUILD_DIR: build-${{ matrix.buildtype }}
          ARTIFACT_NAME: otclient-${{ matrix.os }}-${{ matrix.buildtype }}
        run: |
          cd "${BUILD_DIR}/bin"
          xattr -cr OTClient.app
          codesign --force --deep --sign - OTClient.app
          ditto -c -k --keepParent OTClient.app "${ARTIFACT_NAME}.zip"

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: otclient-${{ matrix.os }}-${{ matrix.buildtype }}-${{ github.sha }}
          path: build-${{ matrix.buildtype }}/bin/otclient-${{ matrix.os }}-${{ matrix.buildtype }}.zip
          if-no-files-found: error
