#!/bin/bash

# Load environment variables
if [ ! -f ../.env ]; then
    echo "Error: .env file not found!"
    exit 1
fi

source ../.env

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

# Function to check if a command was successful
check_status() {
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ $1${NC}"
        return 0
    else
        echo -e "${RED}✗ $1${NC}"
        return 1
    fi
}

# Function to show usage
show_usage() {
    echo "Usage: $0 [options]"
    echo ""
    echo "Options:"
    echo "  -f, --force       Skip all confirmations"
    echo "  -k, --keep-data   Keep existing database data"
    echo "  -t, --test-data   Generate test data after reset (basic|extended|stress)"
    echo "  -h, --help        Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0                           # Interactive reset"
    echo "  $0 -f                        # Force reset without confirmation"
    echo "  $0 -t basic                  # Reset and generate basic test data"
    echo "  $0 -f -t extended            # Force reset and generate extended test data"
    echo "  $0 -k -t stress              # Keep data and add stress test data"
}

# Default values
FORCE=false
KEEP_DATA=false
TEST_DATA=""

# Parse command line options
while [[ $# -gt 0 ]]; do
    case "$1" in
        -f|--force)
            FORCE=true
            shift
            ;;
        -k|--keep-data)
            KEEP_DATA=true
            shift
            ;;
        -t|--test-data)
            TEST_DATA="$2"
            shift 2
            ;;
        -h|--help)
            show_usage
            exit 0
            ;;
        *)
            echo "Error: Unknown option '$1'"
            show_usage
            exit 1
            ;;
    esac
done

# Confirm reset if not forced
if [ "$FORCE" = false ]; then
    echo -e "${YELLOW}Warning: This will reset your development environment.${NC}"
    if [ "$KEEP_DATA" = false ]; then
        echo -e "${RED}ALL DATA WILL BE LOST!${NC}"
    fi
    read -p "Continue? [y/N] " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Operation cancelled."
        exit 1
    fi
fi

echo -e "${BLUE}Starting environment reset...${NC}"

# Stop containers
echo "Stopping containers..."
docker-compose down
check_status "Stop containers"

# Remove volumes if not keeping data
if [ "$KEEP_DATA" = false ]; then
    echo "Removing volumes..."
    docker-compose down -v
    check_status "Remove volumes"
fi

# Start containers
echo "Starting containers..."
docker-compose up -d
check_status "Start containers"

# Wait for containers to be ready
echo "Waiting for containers to be ready..."
sleep 10

# Reset database if not keeping data
if [ "$KEEP_DATA" = false ]; then
    echo "Resetting database..."
    ./manage-db.sh reset -y
    check_status "Database reset"
    
    # Install WordPress
    echo "Installing WordPress..."
    docker-compose exec wordpress wp core install \
        --url=http://localhost:8080 \
        --title="HVAC Training Events" \
        --admin_user=admin \
        --admin_password=admin \
        --admin_email=admin@example.com \
        --allow-root
    check_status "WordPress installation"
    
    # Install and activate required plugins
    echo "Installing required plugins..."
    REQUIRED_PLUGINS=(
        "the-events-calendar"
        "events-calendar-pro"
        "event-tickets"
        "event-tickets-plus"
        "tribe-community-events"
    )
    
    for plugin in "${REQUIRED_PLUGINS[@]}"; do
        echo "Installing plugin: $plugin"
        docker-compose exec wordpress wp plugin install "$plugin" --allow-root
        docker-compose exec wordpress wp plugin activate "$plugin" --allow-root
        check_status "Plugin $plugin installation"
    done
fi

# Generate test data if requested
if [ -n "$TEST_DATA" ]; then
    echo "Generating test data..."
    ./manage-db.sh generate-test-data "$TEST_DATA" -y
    check_status "Test data generation"
fi

# Verify environment
echo "Verifying environment..."
./verify-dev.sh
check_status "Environment verification"

echo -e "${GREEN}Environment reset completed!${NC}" 