name: Seed reset smoke

on:
  pull_request:
    paths:
      - 'supabase/migrations/**'
      - 'supabase/seeds/**'
      - 'supabase/seed.manifest'
      - 'supabase/seed.sql'
      - 'supabase/config.toml'
      - 'supabase/combine-seeds.sh'
      - '.github/workflows/seeds-smoke.yml'
  push:
    branches:
      - main
      - development
    paths:
      - 'supabase/migrations/**'
      - 'supabase/seeds/**'
      - 'supabase/seed.manifest'
      - 'supabase/seed.sql'
      - 'supabase/config.toml'
      - 'supabase/combine-seeds.sh'

jobs:
  db-reset:
    name: supabase db reset against committed seed.sql
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0

      - name: Setup workspace
        uses: ./.github/actions/setup-workspace

      - uses: supabase/setup-cli@v2
        with:
          version: latest
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Start Supabase
        run: supabase start

      - name: Reset database (applies migrations + seed.sql)
        # `supabase db reset` finishes by polling Studio/Kong while containers
        # are still warming up; the first poll occasionally returns 502 even
        # though migrations succeeded. Retry once after a short cool-down.
        run: |
          for attempt in 1 2; do
            if supabase db reset --local --yes; then
              exit 0
            fi
            echo "db reset attempt $attempt failed; restarting Supabase and retrying"
            supabase stop --no-backup || true
            sleep 5
            supabase start
          done
          exit 1

      - name: Stop Supabase
        if: always()
        run: supabase stop --no-backup
