on: 
  push:
      branches: [main, master, workflow_testing]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true


env:
  ERC_JOB_NAME : "⚡ERC report⚡"
  DRC_JOB_NAME : "📟 DRC report 📟"

jobs:
  setup_new_project: # this job only exists for project creation
    runs-on: ubuntu-latest
    name: set up project 
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # reads the project_setting.yaml file allowing you to access it with steps.id.outputs.[key]
      - name: read yaml file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml
      
      - name: check setup
        run: |
          echo ${{steps.yaml.outputs.needs_setup}}

      - name: Setting up Python and chevron to processes failed reports
        # workflows really need some more work done on them
        if: ${{ !cancelled() && steps.yaml.outputs.needs_setup == 'true' && github.repository != 'sirlilpanda/kicad-project-template'}} 
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'
      
      - name: installing requirements  
        if: ${{ !cancelled() && steps.yaml.outputs.needs_setup == 'true' && github.repository != 'sirlilpanda/kicad-project-template'}} 
        run: pip install -r ${{ github.workspace }}/.github/report_processing/requirements.txt
      
      - name: rename project if setup has not been completed
        if: ${{ !cancelled() && steps.yaml.outputs.needs_setup == 'true' && github.repository != 'sirlilpanda/kicad-project-template'}} 
        run: python ${{ github.workspace }}/.github/rename.py ${{github.event.repository.name}} 

      - name: commit production files
        if: ${{ !cancelled() && steps.yaml.outputs.needs_setup == 'true' && github.repository != 'sirlilpanda/kicad-project-template'}} 
        uses: EndBug/add-and-commit@v9
        with:
          default_author: github_actions
          message: 'setup project'
          push: true
          add: "."

  setup_matrixs:
    outputs:
      projects: ${{ steps.projects.outputs.projects }}
    runs-on: ubuntu-latest
    name: matrix setup 🗓️
    needs: setup_new_project
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setting up Python
        # workflows really need some more work done on them
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'

      - name: installing requirements  
        run: pip install pyyaml
      
      - name: creating list start
        run:
          echo -n "projects=[" >> temp.txt

      # this find commad outputs path1,path2,path3 with out the .kicad_pro ext
      # .kicad_pro ext is removed because the erc and drc steps want either a
      # .kicad_sch or .kicad_pcb file it also makes it easier to name files 
      # with the project name
      # this output will look like
      # projects=["path/projet_name", "path/projet_name2", ...]
      - name: get kicad project names
        run:
          find . -type f -name "*.kicad_pro" -exec sh -c 'echo -n "\"${0%.kicad_pro}\","' {} \; | sed 's/.\{1\}$//' >> temp.txt

      - name: creating list end
        run:
          echo "]" >> temp.txt

      - name: list output
        run:
          cat temp.txt
  
      - name: creating list end
        id: projects
        run:
          cat temp.txt >> "$GITHUB_OUTPUT"


# basename is used here to remove the rest of the path from the find
# 
  DRC:
    runs-on: ubuntu-latest
    name: "📟 DRC report 📟"
    needs: setup_matrixs
    strategy:
      matrix:
        project_path: ${{ fromJSON(needs.setup_matrixs.outputs.projects) }}
    steps:


      - name: check matrix
        run: basename ${{ matrix.project_path }}

      - name: setting env
        run:
          echo "PROJECT_NAME=$(basename ${{ matrix.project_path }})" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v4

      - name: read-yaml-file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml

      - name: Setting up Python and chevron to processes reports
        # workflows really need some more work done on them
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'
        
      - name: installing requirements  
        run: pip install -r ${{ github.workspace }}/.github/report_processing/requirements.txt

      - name: Run KiCad DRC
        id: drc
        uses: sparkengineering/kicad-action@v4
        with:
          kicad_pcb: ${{ matrix.project_path }}.kicad_pcb
          pcb_drc: true
          report_format: json
          pcb_drc_file: drc.json
      
      - name: checking files were created
        run:
          ls -R

      - name: creating DRC report in markdown
        if: ${{ always() }}
        run: python
          ${{ github.workspace }}/.github/report_processing/process_json_reports.py 
          ${{ github.workspace }}/$(dirname ${{matrix.project_path}})/drc.json
          ${{ github.workspace }}/${{steps.yaml.outputs.drc_report_template_path}}
          ${{ github.workspace }}/${{ env.PROJECT_NAME }}_drc.md
          ${{ env.PROJECT_NAME }}

      - name: upload report summary
        if: ${{ always() }}
        run: cat ${{ github.workspace }}/${{ env.PROJECT_NAME }}_drc.md >> $GITHUB_STEP_SUMMARY
      
      - name: get summary url
        if: ${{always()}}
        id: exp
        uses: pl-strflt/job-summary-url-action@v1
        with:
          job: "${{env.DRC_JOB_NAME}} (${{matrix.project_path}})"

      - name: create files to upload
        if: ${{always()}}
        run: |
          echo "{\"passing_drc\":\"${{steps.drc.conclusion != 'failure'}}\",\"project_name\":\"${{env.PROJECT_NAME}}\", \"drc_summary_link\":\"${{ steps.exp.outputs.job_summary_url }}\"}" >> ${{env.PROJECT_NAME}}_drc.json
        
      - name: upload data for readme updating
        if: ${{always()}}
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.PROJECT_NAME}}_drc.json
          path: ${{env.PROJECT_NAME}}_drc.json

  ERC:
    runs-on: ubuntu-latest
    name: "⚡ERC report⚡"
    needs: setup_matrixs
    strategy:
      matrix:
        project_path: ${{ fromJSON(needs.setup_matrixs.outputs.projects) }}
    steps:
      - name: check matrix
        run: basename ${{ matrix.project_path }}

      - name: setting env
        run:
          echo "PROJECT_NAME=$(basename ${{ matrix.project_path }})" >> $GITHUB_ENV

      - name: setting env
        run:
          echo "PROJECT_NAME=$(basename ${{ matrix.project_path }})" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v4

      - name: read-yaml-file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml

      - name: Setting up Python and chevron to processes reports
        # workflows really need some more work done on them
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'
        
      - name: installing requirements  
        run: pip install -r ${{ github.workspace }}/.github/report_processing/requirements.txt

      - name: Run KiCad ERC
        id: erc
        uses: sparkengineering/kicad-action@v4
        with:
          kicad_sch: ${{ matrix.project_path }}.kicad_sch
          sch_erc: true
          report_format: json
          sch_erc_file: erc.json
      
      - name: checking files were created
        run:
          ls -R

      - name: creating ERC report in markdown
        if: ${{ always() }}
        run: python
          ${{ github.workspace }}/.github/report_processing/process_json_reports.py 
          ${{ github.workspace }}/$(dirname ${{matrix.project_path}})/erc.json
          ${{ github.workspace }}/${{steps.yaml.outputs.erc_report_template_path}}
          ${{ github.workspace }}/${{ env.PROJECT_NAME }}_erc.md
          ${{ env.PROJECT_NAME }}
      
      - name: upload report summary
        if: ${{ always() }}
        run: cat ${{ github.workspace }}/${{ env.PROJECT_NAME }}_erc.md >> $GITHUB_STEP_SUMMARY
      
      - name: get summary url
        if: ${{always()}}
        id: exp
        uses: pl-strflt/job-summary-url-action@v1
        with:
          job: "${{env.ERC_JOB_NAME}} (${{matrix.project_path}})"
  
      - name: print summary url
        if: ${{always()}}
        run: echo '${{ steps.exp.outputs.job_summary_url }}'
        shell: bash
  
  
      - name: create files to upload
        if: ${{always()}}
        run: |
          echo "{\"passing_erc\":\"${{steps.erc.conclusion != 'failure'}}\",\"project_name\":\"${{env.PROJECT_NAME}}\", \"erc_summary_link\":\"${{ steps.exp.outputs.job_summary_url }}\"}" >> ${{env.PROJECT_NAME}}_erc.json
        
      - name: upload data for readme updating
        if: ${{always()}}
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.PROJECT_NAME}}_erc.json
          path: ${{env.PROJECT_NAME}}_erc.json

  production_job:
    runs-on: ubuntu-latest
    name: creating production files bom sch gerbers 📂 
    needs: setup_matrixs
    strategy:
      matrix:
        project_path: ${{ fromJSON(needs.setup_matrixs.outputs.projects) }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
  
  

      - name: read-yaml-file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml

      - name: setting env
        run: 
          echo "PROJECT_NAME=$(basename ${{ matrix.project_path }})" >> $GITHUB_ENV
      
      - name: setting env
        run: | #                                                                                      | the / here is defined in the project settings
          echo "schematic_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.schematic_output_name}}.pdf" >> $GITHUB_ENV
          echo "bill_of_materials_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.bom_csv_output_name}}.csv" >> $GITHUB_ENV
          echo "production_format_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.production_file_output_name}}.zip" >> $GITHUB_ENV
          echo "bom_report_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.bom_report_output_name}}.md" >> $GITHUB_ENV
  

      - name: checking envs
        run:
          echo "${{env.schematic_file}}"
          echo "${{env.bill_of_materials_file}}"
          echo "${{env.production_format_file}}"
          echo "${{env.bom_report_file}}"
  

      - name: setting env
        run: 
          echo "PROJECT_NAME=$(basename ${{ matrix.project_path }})" >> $GITHUB_ENV
      
      - name: setting env
        run: | #                                                                                      | the / here is defined in the project settings
          echo "schematic_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.schematic_output_name}}.pdf" >> $GITHUB_ENV
          echo "bill_of_materials_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.bom_csv_output_name}}.csv" >> $GITHUB_ENV
          echo "production_format_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.production_file_output_name}}.zip" >> $GITHUB_ENV
          echo "bom_report_file=${{env.PROJECT_NAME}}${{steps.yaml.outputs.bom_report_output_name}}.md" >> $GITHUB_ENV
  

      - name: checking envs
        run:
          echo "${{env.schematic_file}}"
          echo "${{env.bill_of_materials_file}}"
          echo "${{env.production_format_file}}"
          echo "${{env.bom_report_file}}"
  
      - name: Setting up Python and chevron to processes reports
        # workflows really need some more work done on them
        if: ${{ !cancelled() }} 
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'
        
      - name: installing requirements  
        if: ${{ !cancelled() }} 
        run: pip install -r ${{ github.workspace }}/.github/report_processing/requirements.txt

      - name: Export production files
        id: production
        uses: sparkengineering/kicad-action@v4
        if: '!cancelled()'
        with:
          kicad_sch: ${{ matrix.project_path }}.kicad_sch
          sch_pdf: true # Generate PDF
          sch_bom: true # Generate BOM
          kicad_pcb: ${{ matrix.project_path }}.kicad_pcb
          pcb_gerbers: true # Generate Gerbers

      - name: Moving production files to correct location
        if: ${{ !cancelled() && steps.production.conclusion == 'success' }}
        run: |  
            mv ${{ github.workspace }}/$(dirname ${{matrix.project_path}})/sch.pdf ${{ github.workspace }}/${{env.schematic_file}}
            mv ${{ github.workspace }}/$(dirname ${{matrix.project_path}})/bom.csv ${{ github.workspace }}/${{env.bill_of_materials_file}}
            mv ${{ github.workspace }}/$(dirname ${{matrix.project_path}})/gbr.zip ${{ github.workspace }}/${{env.production_format_file}}


      - name: creating BOM report in markdown
        if: ${{ !cancelled() }} 
        run: python 
          ${{ github.workspace }}/.github/report_processing/process_bom_files.py 
          ${{ github.workspace }}/${{env.bill_of_materials_file}}
          ${{ github.workspace }}/${{steps.yaml.outputs.bom_template_path}}
          ${{ github.workspace }}/${{env.bom_report_file}}

      - name: upload report'
        uses: actions/upload-artifact@v4
        with: 
          name: production-files-${{env.PROJECT_NAME}}.zip
          path: |
            ${{ github.workspace }}/${{env.schematic_file}}
            ${{ github.workspace }}/${{env.bill_of_materials_file}}
            ${{ github.workspace }}/${{env.production_format_file}}
            ${{ github.workspace }}/${{env.bom_report_file}}

      - name: data for readme updating
        if: ${{always()}}
        run: |
          echo "{" >> ${{env.PROJECT_NAME}}_project.json
          echo "\"schematic_link\":\"https://github.com/${{ github.repository }}/tree/${{ github.ref_name }}/${{env.schematic_file}}\"," >> ${{env.PROJECT_NAME}}_project.json
          echo "\"bom_csv_link\":\"https://github.com/${{ github.repository }}/tree/${{ github.ref_name }}/${{env.bill_of_materials_file}}\"," >> ${{env.PROJECT_NAME}}_project.json
          echo "\"bom_report_link\":\"https://github.com/${{ github.repository }}/tree/${{ github.ref_name }}/${{env.bom_report_file}}\"," >> ${{env.PROJECT_NAME}}_project.json
          echo "\"gerber_link\":\"https://github.com/${{ github.repository }}/tree/${{ github.ref_name }}/${{env.production_format_file}}\"," >> ${{env.PROJECT_NAME}}_project.json
          echo "\"project_link\":\"https://github.com/${{ github.repository }}/tree/${{ github.ref_name }}/$(dirname ${{matrix.project_path}})\"," >> ${{env.PROJECT_NAME}}_project.json
          echo "\"project_name\":\"${{env.PROJECT_NAME}}\"" >> ${{env.PROJECT_NAME}}_project.json
          echo "}" >> ${{env.PROJECT_NAME}}_project.json

      - name: upload report
        if: ${{always()}}
        uses: actions/upload-artifact@v4
        with: 
          name: ${{env.PROJECT_NAME}}_project.json
          path: ${{env.PROJECT_NAME}}_project.json

  readme_job:
    runs-on: ubuntu-latest
    name: update readme
    if: ${{ always() }}
    needs: [DRC, ERC, production_job]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: read-yaml-file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml

      - name: download ercs files
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        uses: actions/download-artifact@v4
        with:
          pattern: "*_erc.json"
          path: ${{ github.workspace }}
      
      - name: download drcs files
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        uses: actions/download-artifact@v4
        with:
          pattern: "*_drc.json"
          path: ${{ github.workspace }}

      - name: download production json files
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        uses: actions/download-artifact@v4
        with:
          pattern: "*_project.json"
          path: ${{ github.workspace }}

      - name: Setting up Python
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        uses: actions/setup-python@v5 
        with:
            python-version: '3.10'
            cache: 'pip'
      - name: installing requirements  
        if: ${{ !cancelled() && steps.yaml.outputs.dynamic_read_me == 'true' }}
        run: pip install -r ${{ github.workspace }}/.github/report_processing/requirements.txt

      - name: create extra info for readme hash
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        run: |
          echo "{" >> readme_extras.json
          echo "\"badge\" : \"[![.github/workflows/main.yaml](https://github.com/${{github.repository}}/actions/workflows/main.yaml/badge.svg?branch=${{github.ref_name}})](https://github.com/${{github.repository}}/actions/workflows/main.yaml)\"," >> readme_extras.json
          echo "\"lastest_action_run_link\" : \"https://github.com/${{github.repository}}/actions/runs/${{ github.run_id }}\"," >> readme_extras.json
          echo "\"title\" : \"${{steps.yaml.outputs.project_name}}\"" >> readme_extras.json
          echo "}" >> readme_extras.json
      - name: show extras
        run: 
          cat readme_extras.json

      - name: create new readme
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        run: 
          python 
          ${{ github.workspace }}/.github/report_processing/process_output_files.py 
          ${{ github.workspace }}/${{steps.yaml.outputs.readme_template_path}} *.json

      - name: upload data for readme updating
        if: ${{ steps.yaml.outputs.dynamic_read_me == 'true' }}
        uses: actions/upload-artifact@v4
        with:
          name: README.md
          path: README.md

      - name: list everything
        if: ${{always()}}
        run: ls -R
  
  upload_job:
    runs-on: ubuntu-latest
    name: commit production files 
    if: ${{ always() }}
    needs: [readme_job]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: read-yaml-file
        uses: juliojimenez/yamler@v1.1.0
        id: yaml
        with:
          yaml-file: ${{ github.workspace }}/project_settings.yaml

      - name: download production files
        uses: actions/download-artifact@v4
        with:
          pattern: production-files-*
          path: ${{ github.workspace }}

      - name: download readme
        uses: actions/download-artifact@v4
        with:
          name: README.md
          path: ${{ github.workspace }}
  
      - name: check downloads
        run: ls -R

      - name: Moving files to correct location
        run: |  
            mv ${{ github.workspace }}/production-files-*/*${{steps.yaml.outputs.schematic_output_name}}.pdf ${{ github.workspace }}/${{steps.yaml.outputs.schematic_output_path}}
            mv ${{ github.workspace }}/production-files-*/*${{steps.yaml.outputs.bom_csv_output_name}}.csv ${{ github.workspace }}/${{steps.yaml.outputs.bom_csv_output_path}}
            mv ${{ github.workspace }}/production-files-*/*${{steps.yaml.outputs.bom_report_output_name}}.md ${{ github.workspace }}/${{steps.yaml.outputs.bom_report_output_path}}
            mv ${{ github.workspace }}/production-files-*/*${{steps.yaml.outputs.production_file_output_name}}.zip ${{ github.workspace }}/${{steps.yaml.outputs.production_file_output_path}}
            
      - name: commit production files
        uses: EndBug/add-and-commit@v9
        with:
            default_author: github_actions
            message: 'production files'
            push: true
            pull: '--rebase --autostash'
            tag_push: '--force' # just makes life easier
            tag: 'v1.0.0 --force'
            add: |
              ${{ github.workspace }}/*${{steps.yaml.outputs.schematic_output_name}}.pdf
              ${{ github.workspace }}/*${{steps.yaml.outputs.bom_csv_output_name}}.csv
              ${{ github.workspace }}/*${{steps.yaml.outputs.bom_report_output_name}}.md
              ${{ github.workspace }}/*${{steps.yaml.outputs.production_file_output_name}}.zip
              ${{ github.workspace }}/README.md
      
      - name: list everything
        if: ${{always()}}
        run: ls -R
        
        
        
        