# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# ==================================================================================================================== #
# Copyright 2025-2025 The pyTooling Authors                                                                            #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
name: Install Package

on:
  workflow_call:
    inputs:
      jobs:
        description: 'JSON list with environment fields, telling the system and Python versions to run tests with.'
        required: true
        type: string
      wheel:
        description: "Wheel package as input artifact."
        required: true
        type: string
      package_name:
        description: "Name of the Python package."
        required: true
        type: string

jobs:
  PackageInstallation:
    name: ${{ matrix.sysicon }} ${{ matrix.pyicon }} Package installation using Python ${{ matrix.python }}
    runs-on: ${{ matrix.runs-on }}

    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(inputs.jobs) }}

    defaults:
      run:
        shell: ${{ matrix.shell }}

    steps:
      - name: 📥 Download artifacts '${{ inputs.wheel }}' from 'Package' job
        uses: pyTooling/download-artifact@v7
        with:
          name: ${{ inputs.wheel }}
          path: install

      - name: '🟦 Setup MSYS2 for ${{ matrix.runtime }}'
        uses: msys2/setup-msys2@v2
        if: matrix.system == 'msys2'
        with:
          msystem: ${{ matrix.runtime }}
          update: true
          pacboy: >-
            python-pip:p python-wheel:p
            python-lxml:p
            python-markupsafe:p
            python-pyaml:p python-types-pyyaml:p
            python-ruamel-yaml:p python-ruamel.yaml.clib:p
            python-tomli:p

      - name: 🐍 Setup Python ${{ matrix.python }}
        uses: actions/setup-python@v6
        if: matrix.system != 'msys2'
        with:
          python-version: ${{ matrix.python }}

      - name: 🔧 Install wheel and pip dependencies (native)
        if: matrix.system != 'msys2'
        run: |
          python -m pip install --disable-pip-version-check -U wheel

      - name: 🔧 Install wheel from artifact (Ubuntu/macOS)
        if: ( matrix.system != 'windows' && matrix.system != 'windows-arm' )
        run: |
          python -m pip install --disable-pip-version-check -U install/*.whl

      - name: 🔧 Install wheel from artifact (Windows)
        if: ( matrix.system == 'windows' || matrix.system == 'windows-arm' )
        run: |
          python -m pip install -v --disable-pip-version-check (Get-Item .\install\*.whl).FullName

      - name: 📦 Run Package Version Check (Ubuntu/macOS)
        if: ( matrix.system != 'windows' && matrix.system != 'windows-arm' )
        run: |
          set +e

          ANSI_LIGHT_RED=$'\x1b[91m'
          ANSI_LIGHT_GREEN=$'\x1b[92m'
          ANSI_NOCOLOR=$'\x1b[0m'

          printf "Import package and checking package version ...\n  "
          python3 - << EOF | tee ImportTest.log | grep -E "^Package version:\s+[0-9]+\.[0-9]+\.[0-9]+"
          from ${{ inputs.package_name }} import __version__

          print(f"Package version: {__version__}")
          EOF
          if [[ $? -eq 0 ]]; then
            printf "  ${ANSI_LIGHT_GREEN}[PASSED]${ANSI_NOCOLOR}\n"
          else
            printf "  ${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n"
            printf "::error title=%s::%s\n" "InstallPackage" "Couldn't check package version of '${{ inputs.package_name }}'."
            exit 1
          fi

      - name: 📦 Run Package Version Check (Windows)
        if: ( matrix.system == 'windows' || matrix.system == 'windows-arm' )
        run: |
          $result=$(python -c "from ${{ inputs.package_name }} import __version__; print(f""Package version: {__version__}"")")
          Write-Host $result
          if ($result -match "Package version:\s+\d+\.\d+\.\d+") {
            Write-Host -ForegroundColor Green "[PASSED]"
          } else {
            Write-Host -ForegroundColor Red "[FAILED]"
            Write-Host ("::error title={0}::{1}" -f "InstallPackage", "Couldn't check package version of '${{ inputs.package_name }}'.")
            exit 1
          }
