name: Release Token List

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version tag (e.g., v1.2.0)'
        required: true
        type: string

jobs:
  release:
    name: Release Token List
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - 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 token list
        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 (required)
        run: |
          node token-lists/scripts/verify-on-chain.js token-lists/lists/dbis-138.tokenlist.json --required
        continue-on-error: false
      
      - name: Determine version
        id: version
        run: |
          if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
            VERSION="${{ github.event.inputs.version }}"
            # Remove 'v' prefix if present
            VERSION=${VERSION#v}
          else
            # Extract version from tag
            VERSION=${GITHUB_REF#refs/tags/v}
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "tag=v$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"
      
      - name: Generate checksums
        id: checksums
        run: |
          cd token-lists/lists
          sha256sum dbis-138.tokenlist.json > SHA256SUMS
          echo "checksums_file=token-lists/lists/SHA256SUMS" >> $GITHUB_OUTPUT
          cat SHA256SUMS
      
      - name: Sign token list
        id: sign
        run: |
          cd token-lists
          chmod +x scripts/sign-list.sh
          export MINISIGN_PRIVATE_KEY="${{ secrets.MINISIGN_PRIVATE_KEY }}"
          ./scripts/sign-list.sh sign
        continue-on-error: true
      
      - name: Create release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            token-lists/lists/dbis-138.tokenlist.json
            token-lists/lists/dbis-138.tokenlist.json.sig
            token-lists/lists/SHA256SUMS
          name: Release ${{ steps.version.outputs.tag }}
          tag_name: ${{ steps.version.outputs.tag }}
          body_path: token-lists/docs/CHANGELOG.md
          generate_release_notes: true
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

