name: Build and Release IFLAPP

on:
  push:
    branches:
      - main
    paths:
      - 'details.xml'
  workflow_dispatch:

jobs:
  parse-version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.extract.outputs.version }}
      app_name: ${{ steps.extract.outputs.app_name }}
      publisher: ${{ steps.extract.outputs.publisher }}
      
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Extract metadata from details.xml
        id: extract
        run: |
          if [ -f details.xml ]; then
            VERSION=$(grep -oP '<version>\K[^<]+' details.xml | head -1)
            APP_NAME=$(grep -oP '<app>\K[^<]+' details.xml | head -1)
            PUBLISHER=$(grep -oP '<publisher>\K[^<]+' details.xml | head -1)
            if [ -z "$PUBLISHER" ]; then
              PUBLISHER=$(grep -oP '<empresa>\K[^<]+' details.xml | head -1)
            fi
            echo "version=${VERSION}" >> $GITHUB_OUTPUT
            echo "app_name=${APP_NAME}" >> $GITHUB_OUTPUT
            echo "publisher=${PUBLISHER}" >> $GITHUB_OUTPUT
            echo "✅ Metadata extracted: $PUBLISHER.$APP_NAME v$VERSION"
          else
            echo "❌ details.xml not found"
            exit 1
          fi

  build:
    needs: parse-version
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        include:
          - os: ubuntu-latest
            os_name: linux
            target_platform: Linux
          - os: windows-latest
            os_name: windows
            target_platform: Windows
    
    runs-on: ${{ matrix.os }}
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      
      - name: Install dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y binutils
      
      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pyinstaller requests
        shell: bash
      
      - name: Compile with PackageMaker CLI (Headless)
        run: |
          python packagemaker.py \
            --compile-project . \
            --headless \
            --output ./dist \
            --platform ${{ matrix.target_platform }}
        shell: bash
      
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: iflapp-${{ matrix.os_name }}
          path: dist/*.iflapp

  release:
    needs: [parse-version, build]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./artifacts
      
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ needs.parse-version.outputs.version }}
          name: "Release ${{ needs.parse-version.outputs.version }}"
          files: artifacts/**/*.iflapp
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
