#!/bin/bash

# ThrillWiki Development Environment Setup
# Sets up the complete development environment for both backend and frontend

set -e

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

# Script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../" && pwd)"

# Configuration
BACKEND_DIR="$PROJECT_ROOT/backend"
FRONTEND_DIR="$PROJECT_ROOT/frontend"

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

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

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

# Function to check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Function to check system requirements
check_requirements() {
    print_status "Checking system requirements..."

    local missing_deps=()

    # Check Python
    if ! command_exists python3; then
        missing_deps+=("python3")
    else
        local python_version=$(python3 --version | cut -d' ' -f2 | cut -d'.' -f1,2)
        if (( $(echo "$python_version < 3.11" | bc -l) )); then
            print_warning "Python version $python_version detected. Python 3.11+ recommended."
        fi
    fi

    # Check uv
    if ! command_exists uv; then
        missing_deps+=("uv")
    fi

    # Check Node.js
    if ! command_exists node; then
        missing_deps+=("node")
    else
        local node_version=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
        if (( node_version < 18 )); then
            print_warning "Node.js version $node_version detected. Node.js 18+ recommended."
        fi
    fi

    # Check pnpm
    if ! command_exists pnpm; then
        missing_deps+=("pnpm")
    fi

    # Check PostgreSQL (optional)
    if ! command_exists psql; then
        print_warning "PostgreSQL not found. SQLite will be used for development."
    fi

    # Check Redis (optional)
    if ! command_exists redis-server; then
        print_warning "Redis not found. Some features may not work."
    fi

    if [ ${#missing_deps[@]} -ne 0 ]; then
        print_error "Missing required dependencies: ${missing_deps[*]}"
        print_status "Please install the missing dependencies and run this script again."
        print_status "Installation instructions:"
        print_status "  - Python 3.11+: https://www.python.org/downloads/"
        print_status "  - uv: pip install uv"
        print_status "  - Node.js 18+: https://nodejs.org/"
        print_status "  - pnpm: npm install -g pnpm"
        exit 1
    fi

    print_success "All system requirements met!"
}

# Function to setup backend
setup_backend() {
    print_status "Setting up Django backend..."

    cd "$BACKEND_DIR"

    # Install Python dependencies with uv
    print_status "Installing Python dependencies..."
    if [ ! -d ".venv" ]; then
        uv sync
    else
        print_warning "Virtual environment already exists. Updating dependencies..."
        uv sync
    fi

    # Create .env file if it doesn't exist
    if [ ! -f ".env" ]; then
        print_status "Creating backend .env file..."
        cp .env.example .env
        print_warning "Please edit backend/.env with your settings"
    else
        print_warning "Backend .env file already exists"
    fi

    # Run database migrations
    print_status "Running database migrations..."
    uv run manage.py migrate

    # Create superuser (optional)
    print_status "Creating Django superuser..."
    echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(username='admin').exists() or User.objects.create_superuser('admin', 'admin@example.com', 'admin')" | uv run manage.py shell

    print_success "Backend setup completed!"
    cd "$PROJECT_ROOT"
}

# Function to setup frontend
setup_frontend() {
    print_status "Setting up Vue.js frontend..."

    cd "$FRONTEND_DIR"

    # Install Node.js dependencies
    print_status "Installing Node.js dependencies..."
    if [ ! -d "node_modules" ]; then
        pnpm install
    else
        print_warning "node_modules already exists. Updating dependencies..."
        pnpm install
    fi

    # Create environment files if they don't exist
    if [ ! -f ".env.local" ]; then
        print_status "Creating frontend .env.local file..."
        cp .env.development .env.local
        print_warning "Please edit frontend/.env.local with your settings"
    else
        print_warning "Frontend .env.local file already exists"
    fi

    print_success "Frontend setup completed!"
    cd "$PROJECT_ROOT"
}

# Function to setup root environment
setup_root_env() {
    print_status "Setting up root environment..."

    cd "$PROJECT_ROOT"

    # Create root .env file if it doesn't exist
    if [ ! -f ".env" ]; then
        print_status "Creating root .env file..."
        cp .env.example .env
        print_warning "Please edit .env with your settings"
    else
        print_warning "Root .env file already exists"
    fi

    print_success "Root environment setup completed!"
}

# Function to verify setup
verify_setup() {
    print_status "Verifying setup..."

    local issues=()

    # Check backend
    cd "$BACKEND_DIR"
    if [ ! -d ".venv" ]; then
        issues+=("Backend virtual environment not found")
    fi

    if [ ! -f ".env" ]; then
        issues+=("Backend .env file not found")
    fi

    # Check if Django can start
    if ! uv run manage.py check --settings=config.django.local >/dev/null 2>&1; then
        issues+=("Django configuration check failed")
    fi

    cd "$FRONTEND_DIR"

    # Check frontend
    if [ ! -d "node_modules" ]; then
        issues+=("Frontend node_modules not found")
    fi

    if [ ! -f ".env.local" ]; then
        issues+=("Frontend .env.local file not found")
    fi

    # Check if Vue can build
    if ! pnpm run type-check >/dev/null 2>&1; then
        issues+=("Vue.js type check failed")
    fi

    cd "$PROJECT_ROOT"

    if [ ${#issues[@]} -ne 0 ]; then
        print_warning "Setup verification found issues:"
        for issue in "${issues[@]}"; do
            echo -e "  - ${YELLOW}$issue${NC}"
        done
        return 1
    else
        print_success "Setup verification passed!"
        return 0
    fi
}

# Function to show usage
show_usage() {
    cat << EOF
Usage: $0 [OPTIONS]

Set up the complete ThrillWiki development environment.

Options:
    -h, --help          Show this help message
    -b, --backend-only  Setup only the backend
    -f, --frontend-only Setup only the frontend
    -y, --yes           Skip confirmation prompts
    --no-verify         Skip setup verification

Examples:
    $0                  # Setup both backend and frontend
    $0 --backend-only   # Setup only backend
    $0 --frontend-only  # Setup only frontend

Environment Variables:
    SKIP_CONFIRMATION   Set to 'true' to skip confirmation prompts
    SKIP_VERIFICATION   Set to 'true' to skip verification

EOF
}

# Parse command line arguments
BACKEND_ONLY=false
FRONTEND_ONLY=false
SKIP_CONFIRMATION=false
SKIP_VERIFICATION=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_usage
            exit 0
            ;;
        -b|--backend-only)
            BACKEND_ONLY=true
            shift
            ;;
        -f|--frontend-only)
            FRONTEND_ONLY=true
            shift
            ;;
        -y|--yes)
            SKIP_CONFIRMATION=true
            shift
            ;;
        --no-verify)
            SKIP_VERIFICATION=true
            shift
            ;;
        *)
            print_error "Unknown option: $1"
            show_usage
            exit 1
            ;;
    esac
done

# Override from environment variables
if [ "$SKIP_CONFIRMATION" = "true" ] || [ "$SKIP_CONFIRMATION_ENV" = "true" ]; then
    SKIP_CONFIRMATION=true
fi

if [ "$SKIP_VERIFICATION" = "true" ] || [ "$SKIP_VERIFICATION_ENV" = "true" ]; then
    SKIP_VERIFICATION=true
fi

# Print banner
echo -e "${GREEN}"
echo "=========================================="
echo "    ThrillWiki Development Setup"
echo "=========================================="
echo -e "${NC}"

print_status "Project root: $PROJECT_ROOT"

# Confirmation prompt
if [ "$SKIP_CONFIRMATION" = false ]; then
    echo ""
    read -p "This will set up the development environment. Continue? (y/N): " -n 1 -r
    echo ""
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        print_status "Setup cancelled."
        exit 0
    fi
fi

# Check requirements
check_requirements

# Setup components based on options
if [ "$BACKEND_ONLY" = true ]; then
    print_status "Setting up backend only..."
    setup_backend
    setup_root_env
elif [ "$FRONTEND_ONLY" = true ]; then
    print_status "Setting up frontend only..."
    setup_frontend
    setup_root_env
else
    print_status "Setting up both backend and frontend..."
    setup_backend
    setup_frontend
    setup_root_env
fi

# Verify setup
if [ "$SKIP_VERIFICATION" = false ]; then
    echo ""
    if verify_setup; then
        print_success "Development environment setup completed successfully!"
        echo ""
        print_status "Next steps:"
        echo "  1. Edit .env files with your configuration"
        echo "  2. Start development servers: ./shared/scripts/dev/start-all.sh"
        echo "  3. Visit http://localhost:5174 for the frontend"
        echo "  4. Visit http://localhost:8000 for the backend API"
        echo ""
        print_status "Happy coding! 🚀"
    else
        print_warning "Setup completed with issues. Please review the warnings above."
        exit 1
    fi
else
    print_success "Development environment setup completed!"
    print_status "Skipped verification as requested."
fi