name: Storage Cleanup

on:
  schedule:
    # Blob cleanup: run daily at 04:00 UTC
    - cron: '0 4 * * *'
    # S3 archive cleanup: run weekly on Sunday at 05:00 UTC
    - cron: '0 5 * * 0'
  workflow_dispatch:
    inputs:
      target:
        description: 'Cleanup target'
        required: true
        default: 'all'
        type: choice
        options:
          - all
          - blob
          - archive

permissions:
  contents: read

jobs:
  cleanup:
    name: ${{ matrix.name }}
    runs-on: [self-hosted, bazel]
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'all' && '{"include":[{"name":"Blob cleanup","command":"bazel run //src/main/go/net/eagle0/build/blob_cleanup:blob_cleanup -- --min-age=1h"},{"name":"S3 archive cleanup","command":"bazel run //src/main/go/net/eagle0/build/s3_archive_cleanup:s3_archive_cleanup -- --min-age=720h"}]}' || github.event_name == 'workflow_dispatch' && github.event.inputs.target == 'archive' && '{"include":[{"name":"S3 archive cleanup","command":"bazel run //src/main/go/net/eagle0/build/s3_archive_cleanup:s3_archive_cleanup -- --min-age=720h"}]}' || github.event_name == 'schedule' && github.event.schedule == '0 5 * * 0' && '{"include":[{"name":"S3 archive cleanup","command":"bazel run //src/main/go/net/eagle0/build/s3_archive_cleanup:s3_archive_cleanup -- --min-age=720h"}]}' || '{"include":[{"name":"Blob cleanup","command":"bazel run //src/main/go/net/eagle0/build/blob_cleanup:blob_cleanup -- --min-age=1h"}]}') }}

    steps:
      - name: Prepare non-LFS checkout
        env:
          GIT_CONFIG_GLOBAL: ${{ runner.temp }}/gitconfig-no-lfs
          GIT_CONFIG_NOSYSTEM: "1"
        run: |
          git config --global --unset-all filter.lfs.process || true
          git config --global filter.lfs.smudge "cat"
          git config --global filter.lfs.clean "cat"
          git config --global filter.lfs.required false
          if [ -d ".git" ]; then
            git config --local --unset-all filter.lfs.process || true
            git config --local filter.lfs.smudge "cat"
            git config --local filter.lfs.clean "cat"
            git config --local filter.lfs.required false
          fi
          rm -f .git/hooks/post-checkout .git/hooks/post-merge .git/hooks/pre-push

      - uses: actions/checkout@v7
        env:
          GIT_CONFIG_GLOBAL: ${{ runner.temp }}/gitconfig-no-lfs
          GIT_CONFIG_NOSYSTEM: "1"
        with:
          persist-credentials: false
          lfs: false
          clean: false

      - name: Ensure Bazel installed
        uses: ./.github/actions/setup-bazel

      - name: Run blob cleanup
        if: matrix.name == 'Blob cleanup'
        env:
          ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
        run: ${{ matrix.command }}

      - name: Run S3 archive cleanup
        if: matrix.name == 'S3 archive cleanup'
        env:
          DO_SPACES_ACCESS_KEY: ${{ secrets.DO_SPACES_ACCESS_KEY }}
          DO_SPACES_SECRET_KEY: ${{ secrets.DO_SPACES_SECRET_KEY }}
        run: ${{ matrix.command }}
