# ==============================================================================
# Gitea Actions CI/CD Workflow Template
# ==============================================================================
# Place this file in '.gitea/workflows/ci.yml' or use it directly in Gitea's CI.
# It automatically validates shell scripts and checks Python formatting/syntax.
# ==============================================================================
name: Multi-Agent Mux CI

on:
  push:
    branches: [ main, dev ]
  pull_request:
    branches: [ main ]

jobs:
  lint-shell:
    name: Lint Shell Scripts
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Install ShellCheck
        run: |
          sudo apt-get update
          sudo apt-get install -y shellcheck

      - name: Run ShellCheck
        run: |
          echo "🔍 Linting shell scripts..."
          shellcheck .agents/skills/lib.sh
          shellcheck .agents/skills/multi-agent-mux-create/scripts/create_session.sh
          shellcheck .agents/skills/multi-agent-mux-stop/scripts/stop_session.sh
          shellcheck .agents/skills/multi-agent-mux-monitor/scripts/reconcile.sh
          shellcheck .agents/skills/multi-agent-mux-loop/scripts/run_loop.sh
          shellcheck deploy/install.sh
          shellcheck deploy/install_mam.sh
          shellcheck deploy/generate-env.sh
          shellcheck deploy/update.sh
          shellcheck deploy/remove.sh
          echo "✅ ShellCheck completed successfully."

  lint-python:
    name: Lint Python Backplane
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'
          cache: 'pip'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pylint
          if [ -f .agents/skills/multi-agent-mux-delegate-job/requirements.txt ]; then
            pip install -r .agents/skills/multi-agent-mux-delegate-job/requirements.txt
          fi

      - name: Run Flake8 (Syntax/Error Check)
        run: |
          echo "🔍 Checking Python syntax with flake8..."
          flake8 .agents/skills/multi-agent-mux-delegate-job/scripts/ --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          flake8 .agents/skills/multi-agent-mux-delegate-job/scripts/ --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

      - name: Run Python Syntax Check (Compile test)
        run: |
          echo "🔍 Verifying Python file compilation..."
          python -m py_compile .agents/skills/multi-agent-mux-delegate-job/scripts/*.py
          echo "✅ All Python files compiled successfully."
