name: Update generated docs

# Regenerates Supabase types and docs/schema/*.md against the migrated DB,
# then commits the result back to the same branch if anything changed.
#
# Uses the default GITHUB_TOKEN, which intentionally does NOT trigger further
# workflows — so this can't loop. If staging/production are protected branches
# that disallow direct pushes from Actions, switch this job to open a PR
# instead (gh pr create) and configure auto-merge.

on:
  push:
    branches:
      - staging
      - production
    paths-ignore:
      - "docs/schema/**"
      - "app/types/supabase/**"
      - "tasks/imported-list-processor/src/supabase.d.ts"
      - "tasks/geo-tagging/src/supabase.d.ts"

jobs:
  regenerate:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      - uses: supabase/setup-cli@v1
        with:
          version: latest

      - run: npm ci

      - name: Create storage bucket dir (referenced by supabase/config.toml)
        run: mkdir -p supabase/storage/list-uploads

      - name: Start local Supabase DB (applies migrations)
        run: supabase db start

      - run: npm run gentypes
      - run: npm run gendocs

      - name: Commit if changed
        run: |
          paths=(
            docs/schema
            app/types/supabase
            tasks/imported-list-processor/src/supabase.d.ts
            tasks/geo-tagging/src/supabase.d.ts
          )
          if [[ -n "$(git status --porcelain -- "${paths[@]}")" ]]; then
            git config user.name "github-actions[bot]"
            git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
            git add -- "${paths[@]}"
            git commit -m "chore(docs): regenerate types and schema reference"
            git push
          else
            echo "No changes to commit."
          fi
