#!/bin/bash
#
# MarkiTect Quick Installer
#
# This script provides a simple way to install MarkiTect.
# It's a wrapper around the Python installer script.
#
# Usage:
#   ./install.sh [options]
#   curl -sSL https://raw.githubusercontent.com/example/markitect/main/install.sh | bash
#
# Options:
#   --system          Install system-wide (requires sudo)
#   --dev             Install in development mode
#   --check           Check installation status
#   --uninstall       Uninstall MarkiTect
#   --help            Show help

set -e

# Default options
SYSTEM=""
DEV=""
CHECK=""
UNINSTALL=""
HELP=""

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

# Function to print colored output
print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --system)
            SYSTEM="--system"
            shift
            ;;
        --dev)
            DEV="--dev"
            shift
            ;;
        --check)
            CHECK="--check"
            shift
            ;;
        --uninstall)
            UNINSTALL="--uninstall"
            shift
            ;;
        --help|-h)
            HELP="--help"
            shift
            ;;
        *)
            print_error "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Show help if requested
if [[ -n "$HELP" ]]; then
    cat << EOF
MarkiTect Quick Installer

Usage: $0 [options]

Options:
  --system      Install system-wide (requires sudo)
  --dev         Install in development mode with test dependencies
  --check       Check current installation status
  --uninstall   Uninstall MarkiTect
  --help        Show this help message

Examples:
  $0                    # Install for current user
  $0 --system          # Install system-wide
  $0 --dev             # Install in development mode
  $0 --check           # Check installation status
  $0 --uninstall       # Uninstall MarkiTect

For more advanced options, use the Python installer directly:
  python install.py --help
EOF
    exit 0
fi

# Check if Python is available
if ! command -v python3 &> /dev/null; then
    print_error "Python 3 is required but not found"
    print_info "Please install Python 3.8 or higher and try again"
    exit 1
fi

# Check Python version
python_version=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
required_version="3.8"

if ! python3 -c "import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)"; then
    print_error "Python $required_version or higher is required (found: $python_version)"
    exit 1
fi

print_success "Python $python_version found"

# Determine script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALLER_SCRIPT="$SCRIPT_DIR/install.py"

# Check if installer script exists
if [[ ! -f "$INSTALLER_SCRIPT" ]]; then
    print_error "Installer script not found: $INSTALLER_SCRIPT"
    print_info "Make sure you're running this from the MarkiTect project directory"
    exit 1
fi

# Build command
cmd="python3 $INSTALLER_SCRIPT"

if [[ -n "$SYSTEM" ]]; then
    cmd="$cmd $SYSTEM"
    print_warning "System installation requires sudo privileges"
fi

if [[ -n "$DEV" ]]; then
    cmd="$cmd $DEV"
fi

if [[ -n "$CHECK" ]]; then
    cmd="$cmd $CHECK"
fi

if [[ -n "$UNINSTALL" ]]; then
    cmd="$cmd $UNINSTALL"
fi

# Run the installer
print_info "Running: $cmd"
exec $cmd