name: Validate Token List

on:
  pull_request:
    paths:
      - 'token-lists/**'
      - '.github/workflows/validate-pr.yml'
  push:
    branches:
      - '**'
    paths:
      - 'token-lists/**'
      - '.github/workflows/validate-pr.yml'

jobs:
  validate:
    name: Validate Token List
    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: 'pnpm'
      
      - name: Install dependencies
        run: pnpm install
        working-directory: ${{ github.workspace }}
      
      - name: Validate JSON schema
        run: |
          node token-lists/scripts/validate-token-list.js token-lists/lists/dbis-138.tokenlist.json
        continue-on-error: false
      
      - name: Validate address checksums
        run: |
          node token-lists/scripts/checksum-addresses.js token-lists/lists/dbis-138.tokenlist.json
        continue-on-error: false
      
      - name: Validate logos
        run: |
          node token-lists/scripts/validate-logos.js token-lists/lists/dbis-138.tokenlist.json
        continue-on-error: true
      
      - name: On-chain verification (optional)
        run: |
          node token-lists/scripts/verify-on-chain.js token-lists/lists/dbis-138.tokenlist.json
        continue-on-error: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Comment PR with results
        if: github.event_name == 'pull_request'
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const path = 'token-lists/lists/dbis-138.tokenlist.json';
            
            if (fs.existsSync(path)) {
              const tokenList = JSON.parse(fs.readFileSync(path, 'utf-8'));
              const body = `## Token List Validation Results ✅
              
              **List**: ${tokenList.name}
              **Version**: ${tokenList.version.major}.${tokenList.version.minor}.${tokenList.version.patch}
              **Tokens**: ${tokenList.tokens.length}
              
              All validation checks passed! 🎉`;
              
              github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: body
              });
            }

