#!/bin/bash

set -e

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

# Resolve SOURCE_DIR relative to this script so it works regardless of cwd.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="$SCRIPT_DIR/../plugin/"
DEST_DIR="$HOME/.claude/plugins/marketplaces/thedotmack/plugin/"

print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

if [ ! -d "$SOURCE_DIR" ]; then
    print_error "Source directory '$SOURCE_DIR' does not exist!"
    exit 1
fi

if [ ! -d "$DEST_DIR" ]; then
    print_warning "Destination directory '$DEST_DIR' does not exist. Creating it..."
    mkdir -p "$DEST_DIR"
fi

print_status "Syncing plugin folder to marketplace..."
print_status "Source: $SOURCE_DIR"
print_status "Destination: $DEST_DIR"

if [ "$1" = "--dry-run" ] || [ "$1" = "-n" ]; then
    print_status "Dry run - showing what would be synced:"
    rsync -av --delete --dry-run "$SOURCE_DIR" "$DEST_DIR"
    exit 0
fi

if rsync -av --delete "$SOURCE_DIR" "$DEST_DIR"; then
    print_status "✅ Plugin folder synced successfully!"
else
    print_error "❌ Sync failed!"
    exit 1
fi

echo ""
print_status "Sync complete. Files are now synchronized."
print_status "You can run '$0 --dry-run' to preview changes before syncing."