name: Quality Gate

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: write         # Required for repository checkout, access, and badge updates
  security-events: read   # Required for CodeQL security status

jobs:
  quality-gate:
    name: Quality Gate
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      
      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
          source $HOME/.cargo/env
      
      - name: Install dependencies
        run: |
          export PATH="$HOME/.cargo/bin:$PATH"
          uv sync
      
      - name: Run quality gate checks
        run: |
          export PATH="$HOME/.cargo/bin:$PATH"
          ./scripts/run_tests.sh
        env:
          GITHUB_REPOSITORY: ${{ github.repository }}
          PYPI_PACKAGE: mypylogger
      
      - name: Update badges (CI-only)
        if: success() && github.ref == 'refs/heads/main'
        run: |
          export PATH="$HOME/.cargo/bin:$PATH"
          export TESTS_PASSED=true
          export GITHUB_REPOSITORY=${{ github.repository }}
          export PYPI_PACKAGE=mypylogger
          uv run python -m badges --ci-mode
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Quality gate summary
        if: always()
        run: |
          echo "Quality Gate Status: ${{ job.status }}"
          if [ "${{ job.status }}" = "success" ]; then
            echo "✅ All quality checks passed"
          else
            echo "❌ Quality gate failed"
            exit 1
          fi