# ============================================================================
# BlackRoad OS - Pull Request Checks
# Copyright (c) 2025 BlackRoad OS, Inc. / Alexa Louise Amundson
# All Rights Reserved.
# ============================================================================
#
# Additional checks for pull requests.
# ============================================================================

name: PR Checks

on:
  pull_request:
    types: [opened, edited, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write

jobs:
  # ─────────────────────────────────────────────────────────────────────────
  # Validate PR Title (Conventional Commits)
  # ─────────────────────────────────────────────────────────────────────────
  pr-title:
    name: Validate PR Title
    runs-on: ubuntu-latest
    steps:
      - name: Check PR title format
        uses: amannn/action-semantic-pull-request@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          types: |
            feat
            fix
            docs
            style
            refactor
            perf
            test
            build
            ci
            chore
            revert
          scopes: |
            operator
            quantum
            teacher
            cli
            web
            agents
            infra
            deps
          requireScope: false
          subjectPattern: ^[A-Z].+$
          subjectPatternError: |
            The PR title "{subject}" must start with an uppercase letter.

  # ─────────────────────────────────────────────────────────────────────────
  # Check for Breaking Changes
  # ─────────────────────────────────────────────────────────────────────────
  breaking-changes:
    name: Breaking Change Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check for breaking changes
        run: |
          # Check if PR title or body mentions breaking changes
          TITLE="${{ github.event.pull_request.title }}"
          BODY="${{ github.event.pull_request.body }}"

          if echo "$TITLE" | grep -qi "breaking\|BREAKING"; then
            echo "::warning::This PR indicates breaking changes in the title"
            echo "## Breaking Change Detected" >> $GITHUB_STEP_SUMMARY
            echo "The PR title mentions breaking changes. Ensure:" >> $GITHUB_STEP_SUMMARY
            echo "- [ ] CHANGELOG is updated" >> $GITHUB_STEP_SUMMARY
            echo "- [ ] Migration guide is provided" >> $GITHUB_STEP_SUMMARY
            echo "- [ ] Version bump is major" >> $GITHUB_STEP_SUMMARY
          fi

          if echo "$BODY" | grep -qi "breaking change"; then
            echo "::warning::This PR body mentions breaking changes"
          fi

  # ─────────────────────────────────────────────────────────────────────────
  # Check File Sizes
  # ─────────────────────────────────────────────────────────────────────────
  file-size:
    name: Check File Sizes
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Check for large files
        run: |
          echo "## Large Files Check" >> $GITHUB_STEP_SUMMARY

          # Find files larger than 1MB
          LARGE_FILES=$(find . -type f -size +1M -not -path './.git/*' 2>/dev/null || true)

          if [ -n "$LARGE_FILES" ]; then
            echo "::warning::Large files detected (>1MB)"
            echo "### Large files found:" >> $GITHUB_STEP_SUMMARY
            echo '```' >> $GITHUB_STEP_SUMMARY
            echo "$LARGE_FILES" | while read f; do
              SIZE=$(du -h "$f" | cut -f1)
              echo "$f ($SIZE)"
            done >> $GITHUB_STEP_SUMMARY
            echo '```' >> $GITHUB_STEP_SUMMARY
          else
            echo "No large files detected" >> $GITHUB_STEP_SUMMARY
          fi

  # ─────────────────────────────────────────────────────────────────────────
  # License Header Check
  # ─────────────────────────────────────────────────────────────────────────
  license-check:
    name: License Header Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Check for license headers
        run: |
          echo "## License Header Check" >> $GITHUB_STEP_SUMMARY

          MISSING=0

          # Check Python files
          for f in $(find . -name "*.py" -type f -not -path './.git/*' | head -50); do
            if ! head -10 "$f" | grep -q "Copyright"; then
              echo "Missing header: $f"
              MISSING=$((MISSING + 1))
            fi
          done

          # Check Shell scripts
          for f in $(find . -name "*.sh" -type f -not -path './.git/*' | head -50); do
            if ! head -10 "$f" | grep -q "Copyright"; then
              echo "Missing header: $f"
              MISSING=$((MISSING + 1))
            fi
          done

          if [ $MISSING -gt 0 ]; then
            echo "::notice::$MISSING files missing copyright headers"
            echo "$MISSING files missing copyright headers" >> $GITHUB_STEP_SUMMARY
          else
            echo "All checked files have copyright headers" >> $GITHUB_STEP_SUMMARY
          fi
        continue-on-error: true

  # ─────────────────────────────────────────────────────────────────────────
  # Commit Message Check
  # ─────────────────────────────────────────────────────────────────────────
  commit-check:
    name: Commit Message Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check commit messages
        run: |
          echo "## Commit Message Analysis" >> $GITHUB_STEP_SUMMARY

          # Get commits in this PR
          BASE="${{ github.event.pull_request.base.sha }}"
          HEAD="${{ github.event.pull_request.head.sha }}"

          COMMITS=$(git log --oneline $BASE..$HEAD 2>/dev/null || git log --oneline -10)

          echo "### Commits in this PR:" >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
          echo "$COMMITS" >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY

          # Check for WIP commits
          if echo "$COMMITS" | grep -qi "wip\|work in progress\|fixup\|squash"; then
            echo "::warning::PR contains WIP/fixup commits - consider squashing before merge"
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "**Warning:** Contains WIP/fixup commits" >> $GITHUB_STEP_SUMMARY
          fi
