name: Contract Testing

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

jobs:
  contract-tests:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '20'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Run API contract validation
      run: npm run test:api:contracts
    
    - name: Generate OpenAPI spec
      run: npm run api:generate-spec
    
    - name: Generate TypeScript types
      run: npm run api:generate-types
    
    - name: Run contract compatibility check
      run: npm run test:contract:compatibility
    
    - name: Verify website type checking
      run: npm run website:type-check
    
    - name: Upload generated types as artifacts
      uses: actions/upload-artifact@v3
      with:
        name: generated-types
        path: apps/website/lib/types/generated/
        retention-days: 7
    
    - name: Comment PR with results
      if: github.event_name == 'pull_request'
      uses: actions/github-script@v6
      with:
        script: |
          const fs = require('fs');
          const path = require('path');
          
          // Read any contract change reports
          const reportPath = path.join(process.cwd(), 'contract-report.json');
          if (fs.existsSync(reportPath)) {
            const report = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
            
            const comment = `
            ## 🔍 Contract Testing Results
            
            ✅ **All contract tests passed!**
            
            ### Changes Summary:
            - Total changes: ${report.totalChanges}
            - Breaking changes: ${report.breakingChanges}
            - Added: ${report.added}
            - Removed: ${report.removed}
            - Modified: ${report.modified}
            
            Generated types are available as artifacts.
            `;
            
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment
            });
          }

  contract-snapshot:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '20'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Generate and snapshot types
      run: |
        npm run api:generate-spec
        npm run api:generate-types
    
    - name: Commit generated types
      run: |
        git config --local user.email "github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git add apps/website/lib/types/generated/
        git diff --staged --quiet || git commit -m "chore: update generated API types [skip ci]"
        git push