name: CI

on:
  pull_request:
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

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

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

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

      - run: npm ci

      - name: Regenerate types and schema docs
        run: |
          npm run gentypes
          npm run gendocs

      - name: Fail if generated files are stale
        run: |
          paths=(
            app/types/supabase/index.d.ts
            tasks/imported-list-processor/src/supabase.d.ts
            tasks/geo-tagging/src/supabase.d.ts
            docs/schema
          )
          if ! git diff --ignore-space-at-eol --exit-code --quiet -- "${paths[@]}"; then
            echo "::error::Generated files are out of date. Run \`npm run precommit\` locally and commit the result."
            git diff --stat -- "${paths[@]}"
            git diff -- "${paths[@]}"
            exit 1
          fi
