#!/bin/bash

# Setup Remote Development Environment
# This script connects to a remote server and sets up the development environment

set -e

# Configuration
REMOTE_HOST="ben@192.168.10.249"
REPO_URL="https://forgejo.local/ben/upskill-event-manager.git"  # Adjust if different
TARGET_DIR="~/dev/upskill-event-manager"

echo "🚀 Setting up remote development environment..."
echo "Remote host: $REMOTE_HOST"
echo "Repository: $REPO_URL"
echo "Target directory: $TARGET_DIR"

# SSH to remote server and execute commands
ssh "$REMOTE_HOST" << 'EOF'
echo "📋 Connected to remote server successfully"

# Create dev directory if it doesn't exist
echo "📁 Creating development directory..."
mkdir -p ~/dev

# Set git credentials for local Forgejo instance
echo "🔧 Configuring git credentials for local Forgejo..."
git config --global user.name "Ben"
git config --global user.email "ben@forgejo.local"

# Set up git to use the local Forgejo instance
# Add forgejo.local to known hosts if needed
echo "🌐 Setting up Forgejo connection..."

# Remove existing repository if it exists
if [ -d "~/dev/upskill-event-manager" ]; then
    echo "🗑️  Removing existing repository..."
    rm -rf ~/dev/upskill-event-manager
fi

# Clone the repository
echo "📥 Cloning repository..."
cd ~/dev
git clone https://forgejo.local/ben/upskill-event-manager.git

# Navigate to the repository
cd upskill-event-manager

echo "✅ Repository cloned successfully!"
echo "📍 Location: $(pwd)"
echo "📊 Repository status:"
git status --short || echo "Repository is clean"

echo "🎉 Remote development environment setup complete!"
echo "You can now access the repository at: ~/dev/upskill-event-manager"
EOF

echo "🏁 Script execution completed!"