name: Kam Project Initialization
permissions:
  contents: write
  actions: read
on:
  workflow_dispatch:
    inputs:
      template-url:
        description: "Template download URL (single template: .tar.gz; multiple templates: .zip) - leave empty to skip template import"
        required: false
        type: string
        default: https://github.com/MemDeco-WG/Kam/releases/latest/download/templates.zip
      init-command:
        description: Full initialization command (complete command to run after setup/template import)
        required: false
        type: string
        default: kam init . -t kam_template
      enable-cache:
        description: Toggle version-aware caching for kam and Cargo (legacy top-level toggle; set to 'false' to disable all caching)
        required: false
        type: string
        default: "true"
      cache-types:
        description: "Which caches to enable: 'all' (default), 'none', or a comma-separated list containing any of: 'cargo','pip','js','kam'. Example: 'cargo,pip'"
        required: false
        type: string
        default: "all"
jobs:
  build:
    name: Prepare kam (build & cache)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      # Caching is now handled by `setup-kam` (set with `enable-cache: ${{ github.event.inputs.enable-cache }}`).
      # Removed redundant manual cache steps.
      - name: Setup kam (prepare and populate cache)
        uses: MemDeco-WG/setup-kam@main
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          enable-cache: ${{ github.event.inputs.enable-cache }}
          # Per-cache controls: use 'all'|'none' or a comma-separated list (e.g. 'cargo,pip')
          cache-types: ${{ github.event.inputs.cache-types }}
      - name: Verify kam installation
        run: kam --version

  init:
    name: Run initialization (imports template in workspace)
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      # Caching is now handled by `setup-kam` (set with `enable-cache: ${{ github.event.inputs.enable-cache }}`).
      # Removed redundant manual restore steps.
      - name: Setup kam (import template in this workspace)
        uses: MemDeco-WG/setup-kam@main
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          template-url: ${{ github.event.inputs.template-url }}
          enable-cache: ${{ github.event.inputs.enable-cache }}
          # Per-cache controls: use 'all'|'none' or a comma-separated list (e.g. 'cargo,pip')
          cache-types: ${{ github.event.inputs.cache-types }}
      - name: Verify kam installation
        run: kam --version
      - name: Run initialization command
        shell: bash
        run: |
          kam init --help
          echo "Running initialization command: ${{ github.event.inputs.init-command }}"
          ${{ github.event.inputs.init-command }}
          echo "Initialization completed successfully"
        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 }}
