#!/usr/bin/env bash
# Complete setup and push script for token lists
# This script configures the remote and pushes all commits and tags

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

cd "$REPO_ROOT"

# Check if remote exists
if git remote get-url origin &>/dev/null; then
    CURRENT_URL=$(git remote get-url origin)
    log_info "Current remote: $CURRENT_URL"
    
    # Test if repository exists
    log_info "Testing repository access..."
    if git ls-remote --heads origin &>/dev/null; then
        log_success "Repository is accessible!"
    else
        log_error "Repository not found or not accessible"
        log_info "Current URL: $CURRENT_URL"
        echo ""
        read -p "Do you want to update the remote URL? (y/N): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            read -p "Enter new repository URL: " NEW_URL
            if [[ -n "$NEW_URL" ]]; then
                git remote set-url origin "$NEW_URL"
                log_success "Remote updated to: $NEW_URL"
                
                # Test again
                if git ls-remote --heads origin &>/dev/null; then
                    log_success "Repository is now accessible!"
                else
                    log_error "Repository still not accessible"
                    log_info "Please ensure:"
                    echo "  1. Repository exists on GitHub"
                    echo "  2. You have push access"
                    echo "  3. SSH key is added to GitHub"
                    exit 1
                fi
            else
                log_error "No URL provided"
                exit 1
            fi
        else
            log_error "Cannot proceed without accessible repository"
            exit 1
        fi
    fi
else
    log_info "No remote configured. Setting up..."
    read -p "Enter repository URL (e.g., git@github.com:user/repo.git): " REPO_URL
    
    if [[ -z "$REPO_URL" ]]; then
        log_error "Repository URL is required"
        exit 1
    fi
    
    git remote add origin "$REPO_URL"
    log_success "Remote added: $REPO_URL"
    
    # Test repository
    log_info "Testing repository access..."
    if git ls-remote --heads origin &>/dev/null; then
        log_success "Repository is accessible!"
    else
        log_error "Repository not found or not accessible"
        log_info "Please ensure:"
        echo "  1. Repository exists on GitHub"
        echo "  2. You have push access"
        echo "  3. SSH key is added to GitHub"
        exit 1
    fi
fi

# Get current branch
CURRENT_BRANCH=$(git branch --show-current)
log_info "Current branch: $CURRENT_BRANCH"

# Show what will be pushed
log_info "Commits ready to push:"
git log --oneline origin/$CURRENT_BRANCH..HEAD 2>/dev/null || git log --oneline -5

echo ""
log_info "Tags ready to push:"
git tag -l "token-list-*"

echo ""
read -p "Ready to push? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    log_info "Push cancelled"
    exit 0
fi

# Push commits
log_info "Pushing commits to origin/$CURRENT_BRANCH..."
if git push -u origin "$CURRENT_BRANCH"; then
    log_success "Commits pushed successfully!"
else
    log_error "Failed to push commits"
    exit 1
fi

# Push tags
log_info "Pushing tags..."
if git push origin --tags; then
    log_success "Tags pushed successfully!"
else
    log_error "Failed to push tags"
    exit 1
fi

# Final summary
echo ""
log_success "✅ Push complete!"
echo ""
log_info "Token lists are now available at:"
REPO_URL=$(git remote get-url origin)
if [[ "$REPO_URL" =~ git@github.com:([^/]+)/(.+)\.git ]]; then
    USER_ORG="${BASH_REMATCH[1]}"
    REPO_NAME="${BASH_REMATCH[2]}"
    BASE_URL="https://raw.githubusercontent.com/$USER_ORG/$REPO_NAME"
    echo "  - ChainID 138: $BASE_URL/$CURRENT_BRANCH/token-lists/lists/dbis-138.tokenlist.json"
    echo "  - Ethereum Mainnet: $BASE_URL/$CURRENT_BRANCH/token-lists/lists/ethereum-mainnet.tokenlist.json"
    echo "  - ALL Mainnet: $BASE_URL/$CURRENT_BRANCH/token-lists/lists/all-mainnet.tokenlist.json"
fi
