name: Build and Package Oraset

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  release:
    types: [ created ]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
    - uses: actions/checkout@v3

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

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyinstaller

    - name: Build with PyInstaller
      run: |
        pyinstaller --onefile --name oraset oraset.py

    - name: Create Debian package (Ubuntu only)
      if: matrix.os == 'ubuntu-latest'
      run: |
        mkdir -p oraset_1.0.0_amd64/usr/bin
        cp dist/oraset oraset_1.0.0_amd64/usr/bin/
        mkdir -p oraset_1.0.0_amd64/DEBIAN
        echo 'Package: oraset' > oraset_1.0.0_amd64/DEBIAN/control
        echo 'Version: 1.0.0' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Section: utils' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Priority: optional' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Architecture: amd64' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Depends: python3' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Maintainer: Oraset Team <contact@oraset.org>' >> oraset_1.0.0_amd64/DEBIAN/control
        echo 'Description: Oraset programming language interpreter' >> oraset_1.0.0_amd64/DEBIAN/control
        dpkg-deb --build oraset_1.0.0_amd64

    - name: Create DMG package (macOS only)
      if: matrix.os == 'macos-latest'
      run: |
        mkdir -p Oraset.app/Contents/MacOS
        cp dist/oraset Oraset.app/Contents/MacOS/
        echo '<?xml version="1.0" encoding="UTF-8"?>' > Oraset.app/Contents/Info.plist
        echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> Oraset.app/Contents/Info.plist
        echo '<plist version="1.0">' >> Oraset.app/Contents/Info.plist
        echo '<dict>' >> Oraset.app/Contents/Info.plist
        echo '    <key>CFBundleExecutable</key>' >> Oraset.app/Contents/Info.plist
        echo '    <string>oraset</string>' >> Oraset.app/Contents/Info.plist
        echo '    <key>CFBundleName</key>' >> Oraset.app/Contents/Info.plist
        echo '    <string>Oraset</string>' >> Oraset.app/Contents/Info.plist
        echo '    <key>CFBundleIdentifier</key>' >> Oraset.app/Contents/Info.plist
        echo '    <string>org.oraset.interpreter</string>' >> Oraset.app/Contents/Info.plist
        echo '    <key>CFBundleVersion</key>' >> Oraset.app/Contents/Info.plist
        echo '    <string>1.0.0</string>' >> Oraset.app/Contents/Info.plist
        echo '</dict>' >> Oraset.app/Contents/Info.plist
        echo '</plist>' >> Oraset.app/Contents/Info.plist
        hdiutil create -srcfolder Oraset.app -volname Oraset -ov -format UDZO oraset.dmg

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: oraset-${{ matrix.os }}
        path: |
          ${{ matrix.os == 'windows-latest' && 'dist/oraset.exe' || '' }}
          ${{ matrix.os == 'ubuntu-latest' && 'dist/oraset' || '' }}
          ${{ matrix.os == 'ubuntu-latest' && 'oraset_1.0.0_amd64.deb' || '' }}
          ${{ matrix.os == 'macos-latest' && 'dist/oraset' || '' }}
          ${{ matrix.os == 'macos-latest' && 'oraset.dmg' || '' }}

  release:
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    steps:
    - uses: actions/checkout@v3

    - name: Download all artifacts
      uses: actions/download-artifact@v4
      with:
        path: artifacts

    - name: Upload release assets
      uses: softprops/action-gh-release@v2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        files: |
          artifacts/oraset-windows-latest/oraset.exe
          artifacts/oraset-ubuntu-latest/oraset
          artifacts/oraset-ubuntu-latest/oraset_1.0.0_amd64.deb
          artifacts/oraset-macos-latest/oraset
          artifacts/oraset-macos-latest/oraset.dmg