name: Build and Release

on:
  push:
    branches: [ main, master ]
    paths:
      - 'pyproject.toml'  # Trigger on pyproject.toml changes

jobs:
  check-version:
    runs-on: ubuntu-latest
    outputs:
      version_changed: ${{ steps.check_version.outputs.version_changed }}
      version: ${{ steps.check_version.outputs.version }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2  # We need the previous commit to compare
      
      - name: Check if version changed
        id: check_version
        run: |
          # Install toml parser
          pip install toml
          
          # Extract version from pyproject.toml using Python
          NEW_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
          
          # Get version from the previous commit
          git checkout HEAD~1
          if [ -f pyproject.toml ]; then
            OLD_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
          else
            OLD_VERSION=""
          fi
          git checkout -
          
          # Compare versions
          if [ "$NEW_VERSION" != "$OLD_VERSION" ]; then
            echo "version_changed=true" >> $GITHUB_OUTPUT
            echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
            echo "Version changed from $OLD_VERSION to $NEW_VERSION"
          else
            echo "version_changed=false" >> $GITHUB_OUTPUT
            echo "Version unchanged: $NEW_VERSION"
          fi

  build-and-release:
    needs: check-version
    if: ${{ needs.check-version.outputs.version_changed == 'true' }}
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          
      - name: Install dependencies
        run: |
          # Install uv
          python -m pip install --upgrade pip
          pip install uv
          
          # Install project dependencies with uv
          uv sync
          
          # Install PyInstaller globally with pip to ensure it's in PATH
          pip install pyinstaller
          
      - name: Build with PyInstaller
        run: |
          # Activate the virtual environment created by uv sync
          .\.venv\Scripts\activate
          # Now run PyInstaller
          pyinstaller build.spec
          
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: camera-apps-${{ needs.check-version.outputs.version }}
          path: dist/*
          
  create-release:
    needs: [check-version, build-and-release]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ needs.check-version.outputs.version }}
          name: Release v${{ needs.check-version.outputs.version }}
          draft: false
          prerelease: false
          files: camera-apps-${{ needs.check-version.outputs.version }}/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}