#!/usr/bin/env bash
# Submit Token List Script
# Submits token list to CoinGecko, Uniswap, and other aggregators

set -euo pipefail


# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi
TOKEN_LIST_FILE="${PROJECT_ROOT}/metamask/token-list.json"
TOKEN_LIST_URL="https://raw.githubusercontent.com/Defi-Oracle-Tooling/smom-dbis-138/main/metamask/token-list.json"

# Logging function
log() {
    log_success "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

error() {
    log_error "[ERROR] $1"
    exit 1
}

warn() {
    log_warn "[WARNING] $1"
}

# Validate token list
validate_token_list() {
    log "Validating token list..."
    
    if [ ! -f "$TOKEN_LIST_FILE" ]; then
        error "Token list file not found: $TOKEN_LIST_FILE"
    fi
    
    # Validate JSON syntax
    if ! jq empty "$TOKEN_LIST_FILE" 2>/dev/null; then
        error "Token list JSON syntax is invalid"
    fi
    
    # Validate schema (if ajv is available)
    if command -v ajv &> /dev/null; then
        if ajv validate -s "${PROJECT_ROOT}/metamask/token-list.schema.json" -d "$TOKEN_LIST_FILE"; then
            log "Token list schema validation passed"
        else
            error "Token list schema validation failed"
        fi
    else
        warn "ajv-cli not installed, skipping schema validation"
    fi
    
    log "Token list validation passed"
}

# Submit to CoinGecko
submit_to_coingecko() {
    log "Submitting token list to CoinGecko..."
    
    warn "CoinGecko submission requires manual process:"
    warn "  1. Fork https://github.com/coingecko/token-lists"
    warn "  2. Add token list file to the repository"
    warn "  3. Create PR to coingecko/token-lists"
    warn "  4. Wait for review and merge"
    
    log "CoinGecko submission instructions:"
    log "  - Repository: https://github.com/coingecko/token-lists"
    log "  - Token list URL: $TOKEN_LIST_URL"
    log "  - File location: Add to repository root or appropriate directory"
}

# Submit to Uniswap
submit_to_uniswap() {
    log "Submitting token list to Uniswap..."
    
    warn "Uniswap submission requires manual process:"
    warn "  1. Fork https://github.com/Uniswap/token-lists"
    warn "  2. Add token list file to src/tokens/ directory"
    warn "  3. Update tokens.ts to include the list"
    warn "  4. Create PR to Uniswap/token-lists"
    warn "  5. Wait for review and merge"
    
    log "Uniswap submission instructions:"
    log "  - Repository: https://github.com/Uniswap/token-lists"
    log "  - Token list URL: $TOKEN_LIST_URL"
    log "  - File location: src/tokens/defi-oracle-mainnet.json"
}

# Submit to Token Lists aggregator
submit_to_token_lists() {
    log "Submitting token list to Token Lists aggregator..."
    
    warn "Token Lists aggregator submission requires manual process:"
    warn "  1. Visit https://tokenlists.org"
    warn "  2. Click 'Add Token List'"
    warn "  3. Enter token list URL: $TOKEN_LIST_URL"
    warn "  4. Submit for review"
    
    log "Token Lists aggregator submission instructions:"
    log "  - Website: https://tokenlists.org"
    log "  - Token list URL: $TOKEN_LIST_URL"
}

# Generate submission report
generate_report() {
    log "Generating submission report..."
    
    local report_file="${PROJECT_ROOT}/token-list-submission-report.md"
    
    cat > "$report_file" <<EOF
# Token List Submission Report

Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)

## Token List Information

- **File**: $TOKEN_LIST_FILE
- **URL**: $TOKEN_LIST_URL
- **Chain ID**: 138
- **Network**: DeFi Oracle Meta Mainnet

## Submission Status

### CoinGecko
- **Status**: Pending manual submission
- **Repository**: https://github.com/coingecko/token-lists
- **Instructions**: Fork repository, add token list, create PR

### Uniswap
- **Status**: Pending manual submission
- **Repository**: https://github.com/Uniswap/token-lists
- **Instructions**: Fork repository, add to src/tokens/, create PR

### Token Lists Aggregator
- **Status**: Pending manual submission
- **Website**: https://tokenlists.org
- **Instructions**: Visit website, submit token list URL

## Next Steps

1. Ensure token list is accessible at: $TOKEN_LIST_URL
2. Submit to CoinGecko: Fork repository and create PR
3. Submit to Uniswap: Fork repository and create PR
4. Submit to Token Lists aggregator: Visit website and submit URL
5. Monitor submissions and respond to review comments

## Tracking

- CoinGecko PR: [To be created]
- Uniswap PR: [To be created]
- Token Lists aggregator: [To be submitted]
EOF

    log "Submission report generated: $report_file"
}

# Main function
main() {
    log "Starting token list submission process..."
    
    # Step 1: Validate token list
    validate_token_list
    
    # Step 2: Generate submission report
    generate_report
    
    # Step 3: Provide submission instructions
    log "Token list submission instructions:"
    submit_to_coingecko
    submit_to_uniswap
    submit_to_token_lists
    
    log "Token list submission process completed"
    log "See token-list-submission-report.md for detailed instructions"
    log "Next steps:"
    log "  1. Ensure token list is accessible at: $TOKEN_LIST_URL"
    log "  2. Follow submission instructions for each aggregator"
    log "  3. Track submission status in token-list-submission-report.md"
}

# Run main function
main "$@"

