# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# ==================================================================================================================== #
# Copyright 2020-2026 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: Extract Configuration

on:
  workflow_call:
    inputs:
      ubuntu_image_version:
        description: 'Ubuntu image version.'
        required: false
        default: '24.04'
        type: string
      python_version:
        description: 'Python version.'
        required: false
        default: '3.14'
        type: string
      coverage_config:
        description: 'Path to the .coveragerc file. Use pyproject.toml by default.'
        required: false
        default: 'pyproject.toml'
        type: string

    outputs:
      unittest_report_xml:
        description: ""
        value: ${{ jobs.Extract.outputs.unittest_report_xml }}
      unittest_merged_report_xml:
        description: ""
        value: ${{ jobs.Extract.outputs.unittest_merged_report_xml }}
      coverage_report_html:
        description: ""
        value: ${{ jobs.Extract.outputs.coverage_report_html }}
      coverage_report_xml:
        description: ""
        value: ${{ jobs.Extract.outputs.coverage_report_xml }}
      coverage_report_json:
        description: ""
        value: ${{ jobs.Extract.outputs.coverage_report_json }}
      typing_report_cobertura:
        description: ""
        value: ${{ jobs.Extract.outputs.typing_report_cobertura }}
      typing_report_junit:
        description: ""
        value: ${{ jobs.Extract.outputs.typing_report_junit }}
      typing_report_html:
        description: ""
        value: ${{ jobs.Extract.outputs.typing_report_html }}

jobs:
  Extract:
    name: 🔬 Extract configurations from pyproject.toml
    runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}"
    outputs:
      unittest_report_xml:        ${{ steps.getVariables.outputs.unittest_report_xml }}
      unittest_merged_report_xml: ${{ steps.getVariables.outputs.unittest_merged_report_xml }}
      coverage_report_html:       ${{ steps.getVariables.outputs.coverage_report_html }}
      coverage_report_xml:        ${{ steps.getVariables.outputs.coverage_report_xml }}
      coverage_report_json:       ${{ steps.getVariables.outputs.coverage_report_json }}
      typing_report_cobertura:    ${{ steps.getVariables.outputs.typing_report_cobertura }}
      typing_report_junit:        ${{ steps.getVariables.outputs.typing_report_junit }}
      typing_report_html:         ${{ steps.getVariables.outputs.typing_report_html }}

    steps:
      - name: ⏬ Checkout repository
        uses: actions/checkout@v6

      - name: 🐍 Setup Python ${{ inputs.python_version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ inputs.python_version }}

      - name: 🔧 Install wheel and pip dependencies (native)
        run: |
          python -m pip install --disable-pip-version-check -U wheel

      - name: 🔁 Extract configurations from pyproject.toml
        id: getVariables
        shell: python
        run: |
          from json     import dumps as json_dumps
          from os       import getenv
          from pathlib  import Path
          from sys      import version
          from textwrap import dedent

          print(f"Python: {version} (of default installation)")

          from tomllib import load as toml_load

          unittestXMLFile =       Path("./unittest.xml")
          coverageHTMLDirectory = Path("htmlcov")
          coverageXMLFile =       Path("./coverage.xml")
          coverageJSONFile =      Path("./coverage.json")
          coverageRC =            "${{ inputs.coverage_config }}".strip()
          typingCoberturaFile =   Path("report/typing/cobertura.xml")
          typingJUnitFile =       Path("report/typing/StaticTypingSummary.xml")
          typingHTMLDirectory =   Path("report/typing/html")

          # Read output paths from 'pyproject.toml' file
          if coverageRC == "pyproject.toml":
            pyProjectFile =  Path("pyproject.toml")
            if pyProjectFile.exists():
              with pyProjectFile.open("rb") as file:
                pyProjectSettings = toml_load(file)

              toolSection = pyProjectSettings["tool"]
              if "pytest" in toolSection:
                section = toolSection["pytest"]
                if "junit_xml" in section:
                  unittestXMLFile = Path(section["junit_xml"])

              if "pyedaa-reports" in toolSection:
                section = toolSection["pyedaa-reports"]
                if "junit_xml" in section:
                  mergedUnittestXMLFile = Path(section["junit_xml"])

              if "coverage" in toolSection:
                section = toolSection["coverage"]
                if "html" in section:
                  coverageHTMLDirectory = Path(section["html"]["directory"])
                if "xml" in section:
                  coverageXMLFile =       Path(section["xml"]["output"])
                if "json" in section:
                  coverageJSONFile=       Path(section["json"]["output"])

              if "mypy" in toolSection:
                section = toolSection["mypy"]
                if "cobertura_xml_report" in section:
                  typingCoberturaFile =   Path(section["cobertura_xml_report"]) / "cobertura.xml"
                if "junit_xml" in section:
                  typingJUnitFile =       Path(section["junit_xml"])
                if "html_report" in section:
                  typingHTMLDirectory =   Path(section["html_report"])
            else:
              print(f"File '{pyProjectFile}' not found.")
              print(f"::error title=FileNotFoundError::File '{pyProjectFile}' not found.")
              exit(1)

          # Read output paths from '.coveragerc' file
          elif len(coverageRC) > 0:
            print(f"::warning title=Deprecated::Using '{coverageRCFile}' is deprecated. Please use 'pyproject.toml'.")

            coverageRCFile = Path(coverageRC)
            if coverageRCFile.exists():
              with coverageRCFile.open("rb") as file:
                coverageRCSettings = toml_load(file)

              coverageHTMLDirectory = Path(coverageRCSettings["html"]["directory"])
              coverageXMLFile =       Path(coverageRCSettings["xml"]["output"])
              coverageJSONFile =      Path(coverageRCSettings["json"]["output"])
            else:
              print(f"File '{coverageRCFile}' not found.")
              print(f"::error title=FileNotFoundError::File '{coverageRCFile}' not found.")
              exit(1)

          unittest_report_xml = {
            "fullpath":  unittestXMLFile.as_posix(),
            "directory": unittestXMLFile.parent.as_posix(),
            "filename":  unittestXMLFile.name
          }
          unittest_merged_report_xml = {
            "fullpath":  mergedUnittestXMLFile.as_posix(),
            "directory": mergedUnittestXMLFile.parent.as_posix(),
            "filename":  mergedUnittestXMLFile.name
          }
          coverage_report_html = {
            "fullpath":  coverageHTMLDirectory.as_posix(),
            "directory": coverageHTMLDirectory.as_posix()
          }
          coverage_report_xml = {
            "fullpath":  coverageXMLFile.as_posix(),
            "directory": coverageXMLFile.parent.as_posix(),
            "filename":  coverageXMLFile.name
          }
          coverage_report_json = {
            "fullpath":  coverageJSONFile.as_posix(),
            "directory": coverageJSONFile.parent.as_posix(),
            "filename":  coverageJSONFile.name
          }
          typing_report_cobertura = {
            "fullpath":  typingCoberturaFile.as_posix(),
            "directory": typingCoberturaFile.parent.as_posix(),
            "filename":  typingCoberturaFile.name
          }
          typing_report_junit = {
            "fullpath":  typingJUnitFile.as_posix(),
            "directory": typingJUnitFile.parent.as_posix(),
            "filename":  typingJUnitFile.name
          }
          typing_report_html = {
            "fullpath":  typingHTMLDirectory.as_posix(),
            "directory": typingHTMLDirectory.as_posix()
          }

          # Write jobs to special file
          github_output = Path(getenv("GITHUB_OUTPUT"))
          print(f"GITHUB_OUTPUT: {github_output}")
          with github_output.open("a+", encoding="utf-8") as f:
            f.write(dedent(f"""\
              unittest_report_xml={json_dumps(unittest_report_xml)}
              unittest_merged_report_xml={json_dumps(unittest_merged_report_xml)}
              coverage_report_html={json_dumps(coverage_report_html)}
              coverage_report_xml={json_dumps(coverage_report_xml)}
              coverage_report_json={json_dumps(coverage_report_json)}
              typing_report_cobertura={json_dumps(typing_report_cobertura)}
              typing_report_junit={json_dumps(typing_report_junit)}
              typing_report_html={json_dumps(typing_report_html)}
            """))

          print(dedent(f"""\
            DEBUG:
              unittest xml:        {unittestXMLFile}
              merged unittest xml: {mergedUnittestXMLFile}
              coverage html:       {coverageHTMLDirectory}
              coverage xml:        {coverageXMLFile}
              coverage json:       {coverageJSONFile}
              typing cobertura:    {typingCoberturaFile}
              typing junit:        {typingJUnitFile}
              typing html:         {typingHTMLDirectory}
          """))

      - name: Debug JSON objects
        run: |
          printf "unittest_report_xml: JSON=%s\n" "${{ steps.getVariables.outputs.unittest_report_xml }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_report_xml).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_report_xml).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_report_xml).filename }}"

          printf "unittest_merged_report_xml: JSON=%s\n" "${{ steps.getVariables.outputs.unittest_merged_report_xml }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_merged_report_xml).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_merged_report_xml).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.unittest_merged_report_xml).filename }}"

          printf "coverage_report_html: JSON=%s\n" "${{ steps.getVariables.outputs.coverage_report_html }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_html).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_html).directory }}"

          printf "coverage_report_xml: JSON=%s\n" "${{ steps.getVariables.outputs.coverage_report_xml }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_xml).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_xml).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_xml).filename }}"

          printf "coverage_report_json: JSON=%s\n" "${{ steps.getVariables.outputs.coverage_report_json }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_json).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_json).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.coverage_report_json).filename }}"

          printf "typing_report_cobertura: JSON=%s\n" "${{ steps.getVariables.outputs.typing_report_cobertura }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_cobertura).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_cobertura).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_cobertura).filename }}"

          printf "typing_report_junit: JSON=%s\n" "${{ steps.getVariables.outputs.typing_report_junit }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_junit).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_junit).directory }}"
          printf "  filename:  %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_junit).filename }}"

          printf "typing_report_html: JSON=%s\n" "${{ steps.getVariables.outputs.typing_report_html }}"
          printf "  fullpath:  %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_html).fullpath }}"
          printf "  directory: %s\n" "${{ fromJSON(steps.getVariables.outputs.typing_report_html).directory }}"
