name: Bazel Cache Maintenance

on:
  workflow_dispatch:
    inputs:
      target:
        description: 'Maintenance target'
        required: true
        default: 'parity'
        type: choice
        options:
          - parity
          - clean
  schedule:
    - cron: '17 11 * * *'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

permissions:
  contents: read

jobs:
  warm-cache:
    if: github.event_name == 'schedule' || github.event.inputs.target == 'parity'
    runs-on: [self-hosted, bazel]

    steps:
      - name: Clean workspace
        run: |
          git sparse-checkout disable 2>/dev/null || true
          git config --local core.sparseCheckout false 2>/dev/null || true
          git config --local --unset extensions.worktreeConfig 2>/dev/null || true
          rm -f .git/info/sparse-checkout .git/config.worktree 2>/dev/null || true
      - name: Ensure Git LFS available for checkout
        run: |
          COMMON_PATHS=(/opt/homebrew/bin /usr/local/bin)
          for path in "${COMMON_PATHS[@]}"; do
            if [ -d "$path" ]; then
              echo "$path" >> "$GITHUB_PATH"
              export PATH="$path:$PATH"
            fi
          done

          if command -v git-lfs >/dev/null 2>&1; then
            git-lfs version
            exit 0
          fi

          brew install git-lfs
          git-lfs version
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          persist-credentials: false
          lfs: false
      - name: Ensure Bazel installed
        uses: ./.github/actions/setup-bazel
      - name: Warm remote cache
        run: ./scripts/check_bazel_remote_cache_parity.sh warm

  verify-cache:
    needs: warm-cache
    if: github.event_name == 'schedule' || github.event.inputs.target == 'parity'
    runs-on: [self-hosted, bazel]
    strategy:
      fail-fast: false
      matrix:
        probe: [a, b]

    steps:
      - name: Clean workspace
        run: |
          git sparse-checkout disable 2>/dev/null || true
          git config --local core.sparseCheckout false 2>/dev/null || true
          git config --local --unset extensions.worktreeConfig 2>/dev/null || true
          rm -f .git/info/sparse-checkout .git/config.worktree 2>/dev/null || true
      - name: Ensure Git LFS available for checkout
        run: |
          COMMON_PATHS=(/opt/homebrew/bin /usr/local/bin)
          for path in "${COMMON_PATHS[@]}"; do
            if [ -d "$path" ]; then
              echo "$path" >> "$GITHUB_PATH"
              export PATH="$path:$PATH"
            fi
          done

          if command -v git-lfs >/dev/null 2>&1; then
            git-lfs version
            exit 0
          fi

          brew install git-lfs
          git-lfs version
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          persist-credentials: false
          lfs: false
      - name: Ensure Bazel installed
        uses: ./.github/actions/setup-bazel
      - name: Verify remote cache parity
        run: ./scripts/check_bazel_remote_cache_parity.sh verify

  clean-cache:
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'clean'
    runs-on: [self-hosted, bazel]

    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
          lfs: false

      - name: Show disk usage before cleanup
        run: |
          echo "=== Disk usage before cleanup ==="
          BAZEL_USER_ROOT="/private/var/tmp/_bazel_$(whoami)"
          echo "Bazel user root: $BAZEL_USER_ROOT"
          du -sh "$BAZEL_USER_ROOT" 2>/dev/null || echo "Not found"
          df -h .

      - name: Run bazel clean
        run: |
          echo "=== Running bazel clean ==="
          bazel clean
          echo "Clean complete"

      - name: Show disk usage after cleanup
        run: |
          echo "=== Disk usage after cleanup ==="
          BAZEL_USER_ROOT="/private/var/tmp/_bazel_$(whoami)"
          du -sh "$BAZEL_USER_ROOT" 2>/dev/null || echo "Not found"
          df -h .
