name: Update Warehouse Vacancy Index

on:
  schedule:
    - cron: '0 */4 * * *'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    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.10'

      - name: Compile Static Search Index
        env:
          GOOGLE_SHEET_CSV_URL: ${{ secrets.GOOGLE_SHEET_CSV_URL }}
        run: |
          cd cold-storage-simple
          python scripts/update_index.py

      - name: Commit and Push Changes
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git add cold-storage-simple/data/warehouses.json
          if ! git diff-index --quiet HEAD; then
            git commit -m "chore: auto-update vacancy index [skip ci]"
            git push
          else
            echo "No changes in vacancy capacity detected today. Skipping commit."
          fi
