name: 'Install Lemonade (.deb)'
description: 'Downloads and extracts Lemonade .deb package, then verifies installation'
inputs:
  version:
    description: 'Version string for the .deb package (e.g., 0.5.0)'
    required: true
  artifact-name:
    description: 'Name of the artifact to download'
    required: false
    default: 'lemonade-deb'
  extract-path:
    description: 'Path where the .deb should be extracted'
    required: false
    default: './deb-extract'
  download-artifact:
    description: 'Whether to download the artifact (set to false if already downloaded)'
    required: false
    default: 'true'

outputs:
  bin-path:
    description: 'Path to the extracted binaries directory'
    value: ${{ steps.extract.outputs.bin-path }}

runs:
  using: "composite"
  steps:
    - name: Download .deb package
      if: ${{ inputs.download-artifact == 'true' }}
      uses: actions/download-artifact@v7
      with:
        name: ${{ inputs.artifact-name }}
        path: .

    - name: Extract .deb package
      id: extract
      shell: bash
      run: |
        set -e

        VERSION="${{ inputs.version }}"
        EXTRACT_PATH="${{ inputs.extract-path }}"

        # Try to find the .deb file - it may have ~24.04 suffix from dpkg-buildpackage
        DEB_FILE=$(ls lemonade-server_${VERSION}*_amd64.deb 2>/dev/null | head -n1)

        if [ -z "$DEB_FILE" ]; then
            echo "ERROR: .deb file not found matching pattern: lemonade-server_${VERSION}*_amd64.deb"
            ls -la *.deb 2>/dev/null || echo "No .deb files found in current directory"
            exit 1
        fi

        echo "Extracting .deb package: $DEB_FILE"

        # Extract the .deb package
        mkdir -p "$EXTRACT_PATH"
        dpkg-deb -x "$DEB_FILE" "$EXTRACT_PATH"

        # Copy resources to user's local share (path_utils.cpp checks here)
        mkdir -p ~/.local/share/lemonade-server
        cp -r "$EXTRACT_PATH/usr/share/lemonade-server/"* ~/.local/share/lemonade-server/

        # Make llama directory writable for backend downloads
        mkdir -p "$EXTRACT_PATH/usr/share/lemonade-server/llama"
        chmod 777 "$EXTRACT_PATH/usr/share/lemonade-server/llama"

        # Make binaries executable
        chmod +x "$EXTRACT_PATH/usr/bin/lemonade-server"
        chmod +x "$EXTRACT_PATH/usr/bin/lemond"
        chmod +x "$EXTRACT_PATH/usr/bin/lemonade" 2>/dev/null || true

        # Add binaries to PATH for subsequent steps
        BIN_PATH="$(pwd)/$EXTRACT_PATH/usr/bin"
        echo "$BIN_PATH" >> $GITHUB_PATH
        echo "bin-path=$BIN_PATH" >> $GITHUB_OUTPUT

        echo "Package extracted successfully to: $EXTRACT_PATH"
        echo "Binaries available at: $BIN_PATH"

    - name: Verify binaries work
      shell: bash
      run: |
        set -e

        EXTRACT_PATH="${{ inputs.extract-path }}"

        echo "Verifying binaries..."

        # Test version commands
        "$EXTRACT_PATH/usr/bin/lemond" --version
        "$EXTRACT_PATH/usr/bin/lemonade-server" --version

        echo "Binaries verified successfully!"

    - name: Start server and wait for ready
      shell: bash
      run: |
        # The .deb extract doesn't install a systemd service, so start manually.
        # lemond logs to stdout (SinkCout), so redirect to a file for
        # the capture-server-logs action to find.
        EXTRACT_PATH="${{ inputs.extract-path }}"
        SERVER_BIN="$EXTRACT_PATH/usr/bin/lemond"
        CACHE_DIR="./"
        LOG_FILE="/tmp/lemonade-server.log"

        echo "Starting lemonade server with cache dir $CACHE_DIR (logging to $LOG_FILE)..."
        "$SERVER_BIN" "$CACHE_DIR" > "$LOG_FILE" 2>&1 &
        SERVER_PID=$!

        echo "Waiting for server to be ready (PID $SERVER_PID)..."
        for i in $(seq 1 30); do
          if curl -sf http://127.0.0.1:13305/live > /dev/null 2>&1; then
            echo "Server is running and healthy"
            exit 0
          fi
          # Check if process is still alive
          if ! kill -0 $SERVER_PID 2>/dev/null; then
            echo "ERROR: Server process (PID $SERVER_PID) exited prematurely"
            echo "=== Server log ==="
            cat "$LOG_FILE" 2>/dev/null || echo "(no log file)"
            exit 1
          fi
          echo "Waiting for server... ($i/30)"
          sleep 2
        done
        echo "ERROR: Server did not start within 60 seconds"
        echo "=== Server log (last 50 lines) ==="
        tail -50 "$LOG_FILE" 2>/dev/null || echo "(no log file)"
        exit 1
