name: Build for Mac

on:
  workflow_call:
    inputs:
      tag:
        description: "tag"
        required: false
        default: ""
        type: string
  workflow_dispatch:

jobs:
  build-mac-app:
    name: Release Mac
    runs-on: macos-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          flutter-version-file: pubspec.yaml

      - name: Set and Extract version
        shell: pwsh
        run: lib/scripts/build.ps1

      - name: Build Mac
        run: flutter build macos --release --dart-define-from-file=pili_release.json

      - name: Prepare Upload
        run: |
          npm install --global create-dmg
          create-dmg build/macos/Build/Products/Release/PiliPlus.app
        continue-on-error: true

      - name: Rename DMG
        run: mv PiliPlus*.dmg PiliPlus_macos_${{ env.version }}.dmg

      - name: Release
        if: ${{ github.event.inputs.tag != '' }}
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.event.inputs.tag }}
          name: ${{ github.event.inputs.tag }}
          files: |
            PiliPlus_macos_*.dmg

      - name: Upload macos release
        uses: actions/upload-artifact@v5
        with:
          name: macOS-release
          path: PiliPlus_macos_*.dmg
