name: Test and Deploy

on:
  push:
    branches: [ main, cloudways-dev ]
  pull_request:
    branches: [ main, cloudways-dev ]
  workflow_dispatch:
    inputs:
      deploy_type:
        description: 'Deployment Type'
        required: true
        default: 'staging'
        type: choice
        options:
          - staging
          - canary
          - production

jobs:
  health-check:
    name: System Health Check
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'
          cache-dependency-path: wordpress-dev/package-lock.json
          
      - name: Install dependencies
        run: |
          cd wordpress-dev
          npm ci
          
      - name: Install Playwright browsers
        run: |
          cd wordpress-dev
          npx playwright install --with-deps
          
      - name: Run health check
        run: |
          cd wordpress-dev
          bash bin/health-check.sh --ci
          
      - name: Verify selectors
        run: |
          cd wordpress-dev
          bash bin/verify-selectors.sh --ci
          
  test:
    name: Run Tests
    needs: health-check
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'
          cache-dependency-path: wordpress-dev/package-lock.json
          
      - name: Install dependencies
        run: |
          cd wordpress-dev
          npm ci
          
      - name: Install Playwright browsers
        run: |
          cd wordpress-dev
          npx playwright install --with-deps
          
      - name: Create screenshots directory
        run: mkdir -p wordpress-dev/screenshots
          
      - name: Run E2E tests
        run: |
          cd wordpress-dev
          npx playwright test --config=playwright.config.ts
          
      - name: Analyze test results
        if: always()
        run: |
          cd wordpress-dev
          bash bin/test-monitor.sh --store --notify --threshold=80
          
      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: |
            wordpress-dev/test-results
            wordpress-dev/screenshots
            wordpress-dev/logs
            
  visual-regression:
    name: Visual Regression Test
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' || github.event.inputs.deploy_type != ''
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          cache: 'npm'
          cache-dependency-path: wordpress-dev/package-lock.json
          
      - name: Install dependencies
        run: |
          cd wordpress-dev
          npm ci
          
      - name: Install Playwright browsers
        run: |
          cd wordpress-dev
          npx playwright install --with-deps
          
      - name: Install ImageMagick
        run: |
          sudo apt-get update
          sudo apt-get install -y imagemagick
          
      - name: Download baseline screenshots
        uses: actions/download-artifact@v3
        with:
          name: visual-regression-baseline
          path: wordpress-dev/tests/e2e/visual-regression/baseline
        continue-on-error: true
          
      - name: Capture current screenshots
        run: |
          cd wordpress-dev
          bash bin/visual-regression.sh capture
          
      - name: Establish baseline if not exists
        run: |
          cd wordpress-dev
          if [ ! -d "tests/e2e/visual-regression/baseline" ] || [ -z "$(ls -A tests/e2e/visual-regression/baseline)" ]; then
            cp -r tests/e2e/visual-regression/current/* tests/e2e/visual-regression/baseline/
            echo "Created baseline from current screenshots"
          fi
          
      - name: Compare screenshots
        run: |
          cd wordpress-dev
          bash bin/visual-regression.sh compare --threshold=5
          
      - name: Generate report
        if: always()
        run: |
          cd wordpress-dev
          bash bin/visual-regression.sh report
          
      - name: Upload visual regression results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: visual-regression-results
          path: wordpress-dev/tests/e2e/visual-regression
          
      - name: Save current screenshots as baseline
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: actions/upload-artifact@v3
        with:
          name: visual-regression-baseline
          path: wordpress-dev/tests/e2e/visual-regression/current
          
  optimize:
    name: Optimize Tests
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          
      - name: Analyze test performance
        run: |
          cd wordpress-dev
          bash bin/optimize-tests.sh analyze
          
      - name: Upload optimization analysis
        uses: actions/upload-artifact@v3
        with:
          name: test-optimization
          path: wordpress-dev/tests/e2e/analysis
          
  deploy-staging:
    name: Deploy to Staging
    needs: [test, visual-regression]
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/cloudways-dev'
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Pre-deployment validation
        run: |
          cd wordpress-dev
          bash bin/pre-deploy-validation.sh --ci
          
      - name: Deploy to staging
        run: |
          cd wordpress-dev
          bash bin/deploy-plugin.sh --staging
          
      - name: Verify deployment
        run: |
          cd wordpress-dev
          bash bin/verify-staging.sh
          
  deploy-production:
    name: Deploy to Production
    needs: [test, visual-regression]
    runs-on: ubuntu-latest
    if: github.event.inputs.deploy_type == 'production'
    
    environment:
      name: production
      url: https://upskill.measurequick.com
      
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        
      - name: Pre-deployment validation
        run: |
          cd wordpress-dev
          bash bin/pre-deploy-validation.sh --ci
          
      - name: Deploy canary
        run: |
          cd wordpress-dev
          bash bin/canary-deploy.sh --percentage=10 --wait=5
          
      - name: Deploy to production
        run: |
          cd wordpress-dev
          bash bin/deploy.sh --config deploy-config.sh
          
      - name: Run smoke tests
        run: |
          cd wordpress-dev
          npx playwright test tests/e2e/smoke-tests.spec.ts --config=playwright.config.ts
          
      - name: Verify deployment
        run: |
          cd wordpress-dev
          bash bin/verify-production.sh
          
  notify:
    name: Send Notifications
    needs: [deploy-staging, deploy-production]
    runs-on: ubuntu-latest
    if: always()
    
    steps:
      - name: Check deployment status
        id: check
        run: |
          if [[ "${{ needs.deploy-staging.result }}" == "success" || "${{ needs.deploy-production.result }}" == "success" ]]; then
            echo "status=success" >> $GITHUB_OUTPUT
          else
            echo "status=failure" >> $GITHUB_OUTPUT
          fi
          
      - name: Send success notification
        if: steps.check.outputs.status == 'success'
        run: |
          echo "Deployment successful! Notification would be sent here."
          
      - name: Send failure notification
        if: steps.check.outputs.status != 'success'
        run: |
          echo "Deployment failed! Notification would be sent here."