name: Verify Deployment

on:
  schedule:
    - cron: '0 */6 * * *' # Every 6 hours
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - 'scripts/deployment/**'
      - '.github/workflows/verify-deployment.yml'

env:
  RPC_URL: ${{ secrets.RPC_URL || 'https://rpc.d-bis.org' }}
  EXPLORER_URL: ${{ secrets.EXPLORER_URL || 'https://explorer.d-bis.org' }}

jobs:
  verify:
    name: Verify Deployment
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y jq curl dnsutils
      
      - name: Make scripts executable
        run: chmod +x scripts/deployment/*.sh
      
      - name: Verify deployment
        run: |
          ./scripts/deployment/verify-deployment.sh
        continue-on-error: true
      
      - name: Upload verification report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: verification-report
          path: deployment-verification-report.md
          retention-days: 30
      
      - name: Create issue on failure
        if: failure()
        uses: actions/github-script@v7
        with:
          script: |
            const title = 'Deployment Verification Failed';
            const body = `Deployment verification failed. See [verification report](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.`;
            
            await github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: title,
              body: body,
              labels: ['deployment', 'verification', 'bug']
            });

