name: Schema Validation

on:
  pull_request:
    paths:
      - 'supabase/migrations/**'
      - 'src/lib/moderation/**'
      - 'supabase/functions/**'
  push:
    branches:
      - main
      - develop
    paths:
      - 'supabase/migrations/**'
      - 'src/lib/moderation/**'
      - 'supabase/functions/**'
  workflow_dispatch: # Allow manual triggering

jobs:
  validate-schema:
    name: Validate Database Schema
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run schema validation script
        env:
          SUPABASE_SERVICE_ROLE_KEY: ${{ secrets.SUPABASE_SERVICE_ROLE_KEY }}
        run: |
          echo "🔍 Running schema validation checks..."
          npm run validate-schema

      - name: Run Playwright schema validation tests
        env:
          SUPABASE_SERVICE_ROLE_KEY: ${{ secrets.SUPABASE_SERVICE_ROLE_KEY }}
        run: |
          echo "🧪 Running integration tests..."
          npx playwright test schema-validation --reporter=list

      - name: Upload test results
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: schema-validation-results
          path: |
            playwright-report/
            test-results/
          retention-days: 7

      - name: Comment PR with validation results
        if: failure() && github.event_name == 'pull_request'
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## ❌ Schema Validation Failed
              
              The schema validation checks have detected inconsistencies in your database changes.
              
              **Common issues:**
              - Missing fields in submission tables
              - Mismatched data types between tables
              - Missing version metadata fields
              - Invalid column names (e.g., \`ride_type\` in \`rides\` table)
              
              **Next steps:**
              1. Review the failed tests in the Actions log
              2. Check the [Schema Reference documentation](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/docs/submission-pipeline/SCHEMA_REFERENCE.md)
              3. Fix the identified issues
              4. Push your fixes to re-run validation
              
              **Need help?** Consult the [Integration Tests README](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/tests/integration/README.md).`
            })

  migration-safety-check:
    name: Migration Safety Check
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check for breaking changes in migrations
        run: |
          echo "🔍 Checking for potentially breaking migration patterns..."
          
          # Check if any migrations contain DROP COLUMN
          if git diff origin/main...HEAD -- 'supabase/migrations/**' | grep -i "DROP COLUMN"; then
            echo "⚠️  Warning: Migration contains DROP COLUMN"
            echo "::warning::Migration contains DROP COLUMN - ensure data migration plan exists"
          fi
          
          # Check if any migrations alter NOT NULL constraints
          if git diff origin/main...HEAD -- 'supabase/migrations/**' | grep -i "ALTER COLUMN.*NOT NULL"; then
            echo "⚠️  Warning: Migration alters NOT NULL constraints"
            echo "::warning::Migration alters NOT NULL constraints - ensure data backfill is complete"
          fi
          
          # Check if any migrations rename columns
          if git diff origin/main...HEAD -- 'supabase/migrations/**' | grep -i "RENAME COLUMN"; then
            echo "⚠️  Warning: Migration renames columns"
            echo "::warning::Migration renames columns - ensure all code references are updated"
          fi

      - name: Validate migration file naming
        run: |
          echo "🔍 Validating migration file names..."
          
          # Check that all migration files follow the timestamp pattern
          for file in supabase/migrations/*.sql; do
            if [[ ! $(basename "$file") =~ ^[0-9]{14}_ ]]; then
              echo "❌ Invalid migration filename: $(basename "$file")"
              echo "::error::Migration files must start with a 14-digit timestamp (YYYYMMDDHHMMSS)"
              exit 1
            fi
          done
          
          echo "✅ All migration filenames are valid"

  documentation-check:
    name: Documentation Check
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check if schema docs need updating
        run: |
          echo "📚 Checking if schema documentation is up to date..."
          
          # Check if migrations changed but SCHEMA_REFERENCE.md didn't
          MIGRATIONS_CHANGED=$(git diff origin/main...HEAD --name-only | grep -c "supabase/migrations/" || true)
          SCHEMA_DOCS_CHANGED=$(git diff origin/main...HEAD --name-only | grep -c "docs/submission-pipeline/SCHEMA_REFERENCE.md" || true)
          
          if [ "$MIGRATIONS_CHANGED" -gt 0 ] && [ "$SCHEMA_DOCS_CHANGED" -eq 0 ]; then
            echo "⚠️  Warning: Migrations were changed but SCHEMA_REFERENCE.md was not updated"
            echo "::warning::Consider updating docs/submission-pipeline/SCHEMA_REFERENCE.md to reflect schema changes"
          else
            echo "✅ Documentation check passed"
          fi

      - name: Comment PR with documentation reminder
        if: success()
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const migrationsChanged = (await exec.getExecOutput('git', ['diff', 'origin/main...HEAD', '--name-only'])).stdout.includes('supabase/migrations/');
            const docsChanged = (await exec.getExecOutput('git', ['diff', 'origin/main...HEAD', '--name-only'])).stdout.includes('docs/submission-pipeline/SCHEMA_REFERENCE.md');
            
            if (migrationsChanged && !docsChanged) {
              github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: `## 📚 Documentation Reminder
                
                This PR includes database migrations but doesn't update the schema reference documentation.
                
                **If you added/modified fields**, please update:
                - \`docs/submission-pipeline/SCHEMA_REFERENCE.md\`
                
                **If this is a minor change** (e.g., fixing typos, adding indexes), you can ignore this message.`
              })
            }
