name: Build PicView Avalonia on macOS

on:
  push:
    branches:
      - dev
  pull_request:
    branches:
      - dev

jobs:
  build:
    runs-on: macos-latest

    steps:
      # Step 1: Checkout the code
      - name: Checkout repository
        uses: actions/checkout@v4

      # Step 2: Setup .NET 10 SDK
      - name: Setup .NET 10 SDK
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: '10.x'

      # Step 3: Get version from Directory.Build.props using PowerShell
      - name: Get version from Directory.Build.props
        id: get-version
        run: pwsh -File "${{ github.workspace }}/Build/Get-VersionInfo.ps1"

      # Step 4: Restore dependencies
      - name: Restore dependencies
        run: dotnet restore src/PicView.Avalonia.MacOS/PicView.Avalonia.MacOS.csproj

      # Step 5: Build arm64 version
      - name: Build arm64 version
        run: |
          pwsh -File "${{ github.workspace }}/Build/Build Avalonia.MacOS.ps1" `
            -Platform "arm64" `
            -outputPath "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64" `
            -appVersion "${{steps.get-version.outputs.version}}"
        shell: pwsh
      
      # Add debug step to check the build output directories for arm64
      - name: Debug - List build output directories for arm64
        run: |
          echo "Contents of build output directory:"
          ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64"
          echo "Contents of .app/Contents/MacOS:"
          ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app/Contents/MacOS"
          echo "Finding Magick.Native files in the repository:"
          find "${{ github.workspace }}" -name "Magick.Native*arm64*.dylib" 2>/dev/null || echo "No Magick.Native arm64 dylib found"
      
      # Add step to ensure Magick.Native libs are in the app bundle for arm64
      - name: Copy Magick.Native libraries for arm64
        run: |
          # Find the Magick.Native dylibs
          MAGICK_NATIVE_PATH=$(find "${{ github.workspace }}" -name "Magick.Native*arm64*.dylib" 2>/dev/null || echo "")
          
          if [ ! -z "$MAGICK_NATIVE_PATH" ]; then
            echo "Found Magick.Native dylib at: $MAGICK_NATIVE_PATH"
          
            # Copy to the app's MacOS directory
            cp "$MAGICK_NATIVE_PATH" "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app/Contents/MacOS/"
          
            # Verify the copy
            echo "After copying, MacOS directory contains:"
            ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app/Contents/MacOS/"
          else
            echo "WARNING: Could not find Magick.Native-Q8-arm64.dylib file to copy"
            echo "Checking in the nuget cache directory:"
            find ~/.nuget -name "Magick.Native*arm64*.dylib" 2>/dev/null || echo "Not found in nuget cache"
          fi
      
      # Step 6: Create DMG for arm64
      - name: Create DMG for arm64
        run: |
          hdiutil create -volname "PicView" -srcfolder "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app" -ov -format UDZO "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64.dmg"
      
      # Step 7: Upload arm64 artifacts
      - name: Upload arm64 artifacts
        uses: actions/upload-artifact@v4
        with:
          name: PicView-v${{steps.get-version.outputs.version}}-macOS-arm64
          path: |
            ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64/PicView.app
            ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-arm64.dmg
          retention-days: 14

      # Step 8: Build x64 version
      - name: Build x64 version
        run: |
          pwsh -File "${{ github.workspace }}/Build/Build Avalonia.MacOS.ps1" `
            -Platform "x64" `
            -outputPath "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64" `
            -appVersion "${{steps.get-version.outputs.version}}"
        shell: pwsh
      
      # Add debug step to check the build output directories for x64
      - name: Debug - List build output directories for x64
        run: |
          echo "Contents of build output directory:"
          ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64"
          echo "Contents of .app/Contents/MacOS:"
          ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app/Contents/MacOS"
          echo "Finding Magick.Native files in the repository:"
          find "${{ github.workspace }}" -name "Magick.Native*x64*.dylib" 2>/dev/null || echo "No Magick.Native x64 dylib found"
      
      # Add step to ensure Magick.Native libs are in the app bundle for x64
      - name: Copy Magick.Native libraries for x64
        run: |
          # Find the Magick.Native dylibs
          MAGICK_NATIVE_PATH=$(find "${{ github.workspace }}" -name "Magick.Native*x64*.dylib" 2>/dev/null || echo "")
          
          if [ ! -z "$MAGICK_NATIVE_PATH" ]; then
            echo "Found Magick.Native dylib at: $MAGICK_NATIVE_PATH"
          
            # Copy to the app's MacOS directory
            cp "$MAGICK_NATIVE_PATH" "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app/Contents/MacOS/"
          
            # Verify the copy
            echo "After copying, MacOS directory contains:"
            ls -la "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app/Contents/MacOS/"
          else
            echo "WARNING: Could not find Magick.Native-Q8-x64.dylib file to copy"
            echo "Checking in the nuget cache directory:"
            find ~/.nuget -name "Magick.Native*x64*.dylib" 2>/dev/null || echo "Not found in nuget cache"
          fi
      
      # Step 9: Create DMG for x64
      - name: Create DMG for x64
        run: |
          hdiutil create -volname "PicView" -srcfolder "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app" -ov -format UDZO "${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64.dmg"

      # Step 10: Upload x64 artifacts
      - name: Upload x64 artifacts
        uses: actions/upload-artifact@v4
        with:
          name: PicView-v${{steps.get-version.outputs.version}}-macOS-x64
          path: |
            ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64/PicView.app
            ${{ github.workspace }}/Build/PicView-v${{steps.get-version.outputs.version}}-osx-x64.dmg
          retention-days: 14