name: "Build macOS"

on:
  workflow_call:
    inputs:
      build-number:
        required: true
        type: string
      build-version:
        required: true
        type: string

    secrets:
      APPLE_ID:
        required: true
      APPLE_ITC_TEAM_ID:
        required: true
      APPLE_STORE_CONNECT_ISSUER_ID:
        required: true
      APPLE_STORE_CONNECT_KEY:
        required: true
      APPLE_STORE_CONNECT_KEY_ID:
        required: true
      APPLE_TEAM_ID:
        required: true
      MACOS_INSTALLER_CERT_APP_STORE:
        required: true
      MACOS_INSTALLER_CERT_DIRECT:
        required: true
      MATCH_KEYCHAIN_NAME:
        required: true
      MATCH_KEYCHAIN_PASSWORD:
        required: true
      MATCH_PASSWORD:
        required: true
      MATCH_SSH_PRIVATE_KEY:
        required: true

jobs:
  build-app-store:
    name: App Store
    runs-on: macos-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/lunasea/.github/actions/prepare_for_build@master
        with:
          platform: macos
          appstore-connect-key: ${{ secrets.APPLE_STORE_CONNECT_KEY }}
          match-ssh-private-key: ${{ secrets.MATCH_SSH_PRIVATE_KEY }}

      - name: Build LunaSea
        working-directory: ${{ github.workspace }}/macos
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_ITC_TEAM_ID: ${{ secrets.APPLE_ITC_TEAM_ID }}
          APPLE_STORE_CONNECT_ISSUER_ID: ${{ secrets.APPLE_STORE_CONNECT_ISSUER_ID }}
          APPLE_STORE_CONNECT_KEY_FILEPATH: ${{ github.workspace }}/keys/appstore.p8
          APPLE_STORE_CONNECT_KEY_ID: ${{ secrets.APPLE_STORE_CONNECT_KEY_ID }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          MACOS_INSTALLER_CERT_APP_STORE: ${{ secrets.MACOS_INSTALLER_CERT_APP_STORE }}
          MATCH_KEYCHAIN_NAME: ${{ secrets.MATCH_KEYCHAIN_NAME }}
          MATCH_KEYCHAIN_PASSWORD: ${{ secrets.MATCH_KEYCHAIN_PASSWORD }}
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
        run: bundle exec fastlane build_app_store build_number:${{ inputs.build-number }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: macos-appstore-package
          path: ${{ github.workspace }}/output

  build-app-package:
    name: App Package
    runs-on: macos-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/lunasea/.github/actions/prepare_for_build@master
        with:
          platform: macos
          appstore-connect-key: ${{ secrets.APPLE_STORE_CONNECT_KEY }}
          match-ssh-private-key: ${{ secrets.MATCH_SSH_PRIVATE_KEY }}

      - name: Build LunaSea
        working-directory: ${{ github.workspace }}/macos
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_ITC_TEAM_ID: ${{ secrets.APPLE_ITC_TEAM_ID }}
          APPLE_STORE_CONNECT_ISSUER_ID: ${{ secrets.APPLE_STORE_CONNECT_ISSUER_ID }}
          APPLE_STORE_CONNECT_KEY_FILEPATH: ${{ github.workspace }}/keys/appstore.p8
          APPLE_STORE_CONNECT_KEY_ID: ${{ secrets.APPLE_STORE_CONNECT_KEY_ID }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          MACOS_INSTALLER_CERT_DIRECT: ${{ secrets.MACOS_INSTALLER_CERT_DIRECT }}
          MATCH_KEYCHAIN_NAME: ${{ secrets.MATCH_KEYCHAIN_NAME }}
          MATCH_KEYCHAIN_PASSWORD: ${{ secrets.MATCH_KEYCHAIN_PASSWORD }}
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
        run: bundle exec fastlane build_app_package build_number:${{ inputs.build-number }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: macos-app-package
          path: ${{ github.workspace }}/output

  build-disk-image:
    name: Disk Image
    runs-on: macos-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/lunasea/.github/actions/prepare_for_build@master
        with:
          platform: macos
          appstore-connect-key: ${{ secrets.APPLE_STORE_CONNECT_KEY }}
          match-ssh-private-key: ${{ secrets.MATCH_SSH_PRIVATE_KEY }}

      - name: Setup Disk Image Creator
        run: |
          brew install graphicsmagick imagemagick
          pip install setuptools
          npm install -g create-dmg

      - name: Build LunaSea
        working-directory: ${{ github.workspace }}/macos
        env:
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_ITC_TEAM_ID: ${{ secrets.APPLE_ITC_TEAM_ID }}
          APPLE_STORE_CONNECT_ISSUER_ID: ${{ secrets.APPLE_STORE_CONNECT_ISSUER_ID }}
          APPLE_STORE_CONNECT_KEY_FILEPATH: ${{ github.workspace }}/keys/appstore.p8
          APPLE_STORE_CONNECT_KEY_ID: ${{ secrets.APPLE_STORE_CONNECT_KEY_ID }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          MACOS_INSTALLER_CERT_DIRECT: ${{ secrets.MACOS_INSTALLER_CERT_DIRECT }}
          MATCH_KEYCHAIN_NAME: ${{ secrets.MATCH_KEYCHAIN_NAME }}
          MATCH_KEYCHAIN_PASSWORD: ${{ secrets.MATCH_KEYCHAIN_PASSWORD }}
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
        run: bundle exec fastlane build_disk_image build_number:${{ inputs.build-number }} build_version:${{ inputs.build-version }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: macos-disk-image
          path: ${{ github.workspace }}/output
