name: Build and Release

on:
  push:
    branches:
      - main

jobs:
  create-release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      
      - name: Get version
        id: get_version
        run: |
          VERSION=$(cat version.txt)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ env.VERSION }}
          name: Release v${{ env.VERSION }}
          draft: false
          prerelease: false

  build-windows:
    needs: [create-release]
    runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'
          
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pyinstaller
          pip install -r requirements.txt
          
      - name: Build executable
        run: |
          pyinstaller macropad.spec
          
      - name: Upload Windows artifact
        uses: actions/upload-artifact@v3
        with:
          name: macropad-windows
          path: dist/macropad.exe

  build-linux:
    needs: [create-release]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y python3-tk python3-dev
          # Pystray requirements
          sudo apt-get install -y libgtk-3-dev python3-gi python3-gi-cairo gir1.2-gtk-3.0
          # Direct dependencies for system tray functionality
          sudo apt-get install -y gir1.2-appindicator3-0.1
          # Additional libraries
          sudo apt-get install -y libcairo2-dev libgirepository1.0-dev
          # For QR code display
          sudo apt-get install -y python3-pil.imagetk

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pyinstaller
          pip install -r requirements.txt
      - name: Build executable
        run: |
          pyinstaller macropad_linux.spec

      - name: Upload Linux artifact
        uses: actions/upload-artifact@v3
        with:
          name: macropad-linux
          path: dist/macropad

# MacOS build is temporarily disabled
# Uncomment this section when macOS build environment becomes available
#
#  build-macos:
#    needs: [create-release]
#    runs-on: macos-latest
#    steps:
#      - name: Checkout code
#        uses: actions/checkout@v3
#
#      - name: Set up Python
#        uses: actions/setup-python@v4
#        with:
#          python-version: '3.11'
#
#      - name: Install dependencies
#        run: |
#          python -m pip install --upgrade pip
#          pip install pyinstaller
#          pip install -r requirements.txt
#
#      - name: Build executable
#        run: |
#          pyinstaller macropad_macos.spec
#
#      - name: Upload macOS artifact
#        uses: actions/upload-artifact@v3
#        with:
#          name: macropad-macos
#          path: dist/macropad.app

  attach-to-release:
    needs: [create-release, build-windows, build-linux]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Get version
        id: get_version
        run: |
          VERSION=$(cat version.txt)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Download all artifacts
        uses: actions/download-artifact@v3
        
      - name: Attach executables to release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ env.VERSION }}
          files: |
            macropad-windows/macropad.exe
            macropad-linux/macropad
#            macropad-macos/macropad.app/**/*
