#!/usr/bin/env bash
# Configure git remote for token lists push
# This script helps set up the git remote and push configuration

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 current git config
log_info "Current git configuration:"
echo "  User: $(git config user.name)"
echo "  Email: $(git config user.email)"
echo ""

# Check if remote exists
if git remote get-url origin &>/dev/null; then
    log_success "Remote 'origin' already configured:"
    git remote -v
    echo ""
    read -p "Do you want to change it? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        log_info "Keeping existing remote"
        exit 0
    fi
    git remote remove origin
fi

# Get repository URL
log_info "Please provide your GitHub repository URL"
log_info "Examples:"
echo "  HTTPS: https://github.com/username/repo-name.git"
echo "  SSH:   git@github.com:username/repo-name.git"
echo ""

read -p "Repository URL: " REPO_URL

if [[ -z "$REPO_URL" ]]; then
    log_error "Repository URL is required"
    exit 1
fi

# Add remote
log_info "Adding remote 'origin'..."
git remote add origin "$REPO_URL"

log_success "Remote configured:"
git remote -v
echo ""

# Check SSH authentication (if using SSH)
if [[ "$REPO_URL" =~ ^git@ ]]; then
    log_info "Testing SSH connection to GitHub..."
    if ssh -T git@github.com 2>&1 | grep -q "successfully authenticated"; then
        log_success "SSH authentication working"
    else
        log_warn "SSH authentication may not be configured"
        log_info "To set up SSH:"
        echo "  1. Generate key: ssh-keygen -t ed25519 -C 'your_email@example.com'"
        echo "  2. Add to GitHub: https://github.com/settings/keys"
        echo "  3. Test: ssh -T git@github.com"
    fi
fi

# Set up push configuration
log_info "Configuring git push settings..."
git config push.default simple
git config push.autoSetupRemote true

log_success "Git configuration complete!"
echo ""

# Check 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 "Pushing commits..."
    git push -u origin "$CURRENT_BRANCH"
    
    log_info "Pushing tags..."
    git push origin --tags
    
    log_success "✅ Push complete!"
else
    log_info "Push cancelled. You can push later with:"
    echo "  git push -u origin $CURRENT_BRANCH"
    echo "  git push origin --tags"
fi
