name: Kam Exec
permissions:
  contents: write
  actions: read
on:
  workflow_dispatch:
    inputs:
      build-command:
        description: Optional build command to run in the build job (leave empty to skip)
        required: false
        type: string
        default: kam build -a -s -r
      kam-command:
        description: Command to run in the exec job (after build)
        required: false
        type: string
        default: kam version patch
jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      installed-version: ${{ steps.set-build-outputs.outputs.installed_version }}
      kam-present: ${{ steps.set-build-outputs.outputs.kam_present }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      # Caching is now handled by `setup-kam` (set with `enable-cache: "true"`).
      # Removed redundant manual cache steps.
      - name: Setup kam
        uses: MemDeco-WG/setup-kam@main
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          enable-cache: "true"
          private-key: ${{ secrets.KAM_PRIVATE_KEY }}
      - name: Verify kam installation
        run: kam --version
      - name: Run build command
        if: ${{ github.event.inputs.build-command != '' }}
        shell: bash
        run: |
          echo "Running build command: ${{ github.event.inputs.build-command }}"
          ${{ github.event.inputs.build-command }}
        env:
          GH_TOKEN: ${{ github.token }}
      - name: Auto-commit build artifacts
        if: ${{ github.event.inputs.build-command != '' }}
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: |
            chore: build artifacts - Build: ${{ github.event.inputs.build-command || 'No build' }}
          file_pattern: |
            *
          push_options: --force-with-lease
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Export build outputs
        id: set-build-outputs
        run: |
          set -euo pipefail
          if command -v kam >/dev/null 2>&1; then
            INSTALLED=$(kam --version 2>/dev/null | awk '{print $2}' | sed -E 's/^[vV]//' | tr -d '\r\n') || true
            echo "installed_version=$INSTALLED" >> $GITHUB_OUTPUT
            echo "kam_present=true" >> $GITHUB_OUTPUT
          else
            echo "installed_version=" >> $GITHUB_OUTPUT
            echo "kam_present=false" >> $GITHUB_OUTPUT
          fi

  exec:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Setup kam
        uses: MemDeco-WG/setup-kam@main
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          enable-cache: "true"
          private-key: ${{ secrets.KAM_PRIVATE_KEY }}
      - name: Update working tree to include build job commits
        run: |
          set -eu
          git fetch origin || true
          BRANCH=$(git rev-parse --abbrev-ref HEAD)
          git reset --hard origin/$BRANCH || true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Verify kam installation
        run: kam --version
      - name: Run kam command
        shell: bash
        run: |
          kam build --help
          rm -rf dist
          chmod +x hooks/**/* || echo "hooks not found! "
          chmod +x src/**/* || echo "src not found!"
          echo "Executing: ${{ github.event.inputs.kam-command }}"
          ${{ github.event.inputs.kam-command }}
        env:
          GH_TOKEN: ${{ github.token }}
      - name: Auto-commit changes
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: |
            chore: apply config - Template: ${{ github.event.inputs.template-url || 'No template' }} - Command: ${{ github.event.inputs.init-command || github.event.inputs.kam-command }}
          file_pattern: |
            *
          push_options: --force-with-lease
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
