name: Load Parameters
description: |
  This composite action loads build parameters from a `MACHINES.json` file.
  It parses the JSON file to create two matrices:
  - `build_matrix`: A complete matrix containing 'deviceTree', 'linuxFirmware', 'lavaDeviceName' and 'hexDSPBinary'
  It expects the `MACHINES.json` file to be located at `ci/MACHINES.json` relative to the workspace root.

outputs:
  build_matrix:
    description: Build matrix
    value: ${{ steps.set-matrix.outputs.build_matrix }}

runs:
  using: "composite"
  steps:
    - name: Set Build Matrix
      id: set-matrix
      uses: actions/github-script@v7
      with:
        script: |
          const fs = require('fs');
          const path = require('path');
          const filePath = path.join(process.env.GITHUB_WORKSPACE, 'ci', 'MACHINES.json');
          let fileData;
          try {
            if (!fs.existsSync(filePath)) {
              core.setFailed(`Error: MACHINES.json not found at ${filePath}`);
              return;
            }
            fileData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
          } catch (err) {
            core.setFailed(`Error loading or parsing MACHINES.json: ${err.message}`);
            return;
          }

          // fileData is an object, not an array
          const matrix = Object.values(fileData)
            .filter(item =>
              typeof item === 'object' && item !== null &&
              'deviceTree' in item && 'linuxFirmware' in item && 'lavaDeviceName' in item && 'hexDSPBinary' in item
            )
            .map(({ deviceTree, linuxFirmware, lavaDeviceName, hexDSPBinary }) => ({
              deviceTree,
              linuxFirmware,
              lavaDeviceName,
              hexDSPBinary
            }));

          core.setOutput('build_matrix', JSON.stringify(matrix));
          console.log("Generated build_matrix:");
          console.log(matrix);
