#!/bin/bash

# Configuration
REMOTE_HOST="${SSH_HOST:-root@alpha.mintel.me}"
ACTION=$1
ENV=$2

# Help
if [ -z "$ACTION" ] || [ -z "$ENV" ]; then
    echo "Usage: ./scripts/sync-directus.sh [push|pull] [testing|staging|production]"
    echo ""
    echo "Commands:"
    echo "  push   Sync LOCAL data -> REMOTE"
    echo "  pull   Sync REMOTE data -> LOCAL"
    echo ""
    echo "Environments:"
    echo "  testing, staging, production"
    exit 1
fi

# Project Configuration
PRJ_ID="mintel-me"
REMOTE_DIR="/home/deploy/sites/mintel.me"

case $ENV in
  testing)    PROJECT_NAME="${PRJ_ID}-testing"; ENV_FILE=".env.testing" ;;
  staging)    PROJECT_NAME="${PRJ_ID}-staging"; ENV_FILE=".env.staging" ;;
  production) PROJECT_NAME="${PRJ_ID}-prod";    ENV_FILE=".env.prod"    ;;
  *) echo "❌ Invalid environment: $ENV"; exit 1 ;;
esac

# DB Details
DB_USER="directus"
DB_NAME="directus"

echo "🔍 Detecting local database..."
# Check root or apps/web for docker-compose context
if [ -f "docker-compose.yml" ]; then
    COMPOSE_CMD="docker compose"
elif [ -f "../../docker-compose.yml" ]; then
    COMPOSE_CMD="docker compose -f ../../docker-compose.yml"
else
    echo "❌ docker-compose.yml not found."
    exit 1
fi

LOCAL_DB_CONTAINER=$($COMPOSE_CMD ps -q directus-db)
if [ -z "$LOCAL_DB_CONTAINER" ]; then
    echo "❌ Local directus-db container not found. Is it running?"
    exit 1
fi

if [ "$ACTION" == "push" ]; then
    echo "🚀 Pushing LOCAL -> $ENV ($PROJECT_NAME)..."
    
    echo "📦 Dumping local database..."
    docker exec "$LOCAL_DB_CONTAINER" pg_dump -U "$DB_USER" --clean --if-exists --no-owner --no-privileges "$DB_NAME" > dump.sql
    
    echo "📤 Uploading dump to remote server..."
    scp dump.sql "$REMOTE_HOST:$REMOTE_DIR/dump.sql"
    
    echo "🔄 Restoring dump on $ENV..."
    REMOTE_DB_CONTAINER=$(ssh "$REMOTE_HOST" "cd $REMOTE_DIR && docker compose -p $PROJECT_NAME ps -q directus-db")
    
    if [ -z "$REMOTE_DB_CONTAINER" ]; then
        echo "❌ Remote $ENV-db container not found!"
        exit 1
    fi

    echo "🧹 Wiping remote database schema..."
    ssh "$REMOTE_HOST" "docker exec $REMOTE_DB_CONTAINER psql -U $DB_USER $DB_NAME -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'"
    
    echo "⚡ Restoring database..."
    ssh "$REMOTE_HOST" "docker exec -i $REMOTE_DB_CONTAINER psql -U $DB_USER $DB_NAME < $REMOTE_DIR/dump.sql"
    
    echo "📁 Syncing uploads (Local -> $ENV)..."
    rsync -avz --progress ./directus/uploads/ "$REMOTE_HOST:$REMOTE_DIR/directus/uploads/"
    
    rm dump.sql
    ssh "$REMOTE_HOST" "rm $REMOTE_DIR/dump.sql"
    
    echo "🔄 Restarting remote Directus..."
    ssh "$REMOTE_HOST" "cd $REMOTE_DIR && docker compose -p $PROJECT_NAME restart directus"
    
    echo "✨ Push to $ENV complete!"

elif [ "$ACTION" == "pull" ]; then
    echo "📥 Pulling $ENV Data -> LOCAL..."
    
    echo "📦 Dumping remote database ($ENV)..."
    REMOTE_DB_CONTAINER=$(ssh "$REMOTE_HOST" "cd $REMOTE_DIR && docker compose -p $PROJECT_NAME ps -q directus-db")
    
    if [ -z "$REMOTE_DB_CONTAINER" ]; then
        echo "❌ Remote $ENV-db container not found!"
        exit 1
    fi
    
    ssh "$REMOTE_HOST" "docker exec $REMOTE_DB_CONTAINER pg_dump -U $DB_USER --clean --if-exists --no-owner --no-privileges $DB_NAME > $REMOTE_DIR/dump.sql"
    
    echo "📥 Downloading dump..."
    scp "$REMOTE_HOST:$REMOTE_DIR/dump.sql" dump.sql
    
    echo "🧹 Wiping local database schema..."
    docker exec "$LOCAL_DB_CONTAINER" psql -U "$DB_USER" "$DB_NAME" -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'
    
    echo "⚡ Restoring database locally..."
    docker exec -i "$LOCAL_DB_CONTAINER" psql -U "$DB_USER" "$DB_NAME" < dump.sql
    
    echo "📁 Syncing uploads ($ENV -> Local)..."
    rsync -avz --progress "$REMOTE_HOST:$REMOTE_DIR/directus/uploads/" ./directus/uploads/
    
    rm dump.sql
    ssh "$REMOTE_HOST" "rm $REMOTE_DIR/dump.sql"
    
    echo "✨ Pull to Local complete!"
fi
