name: Validate YAML Workflows

on:
  pull_request:
    paths:
      - 'yaml_instance/**/*.yaml'
      - '.github/workflows/**/*.yml'
      - 'tools/validate_all_yamls.py'
      - 'check/**/*.py'
  
  push:
    branches:
      - main
    paths:
      - 'yaml_instance/**/*.yaml'
      - '.github/workflows/**/*.yml'
      - 'tools/validate_all_yamls.py'
      - 'check/**/*.py'
  
  workflow_dispatch:

jobs:
  validate:
    name: Validate YAML Configuration Files
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Set up Python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      
      - name: Install uv
        uses: astral-sh/setup-uv@v4
        with:
          enable-cache: true

      - name: Install system dependencies for pycairo
        run: |
          sudo apt-get update
          sudo apt-get install -y libcairo2-dev pkg-config
      
      - name: Cache uv dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cache/uv
            .venv
          key: ${{ runner.os }}-uv-${{ hashFiles('uv.lock') }}
          restore-keys: |
            ${{ runner.os }}-uv-
      
      - name: Install dependencies
        run: uv sync
      
      - name: Run YAML validation
        run: uv run python tools/validate_all_yamls.py
      
      - name: Report validation results
        if: always()
        run: |
          if [ $? -eq 0 ]; then
            echo "All YAML workflow files passed validation"
          else
            echo "YAML validation failed - check the logs above for details"
            exit 1
          fi
