#!/usr/bin/env bash
# Development environment setup script
# Installs required tools and dependencies for development

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

source "$SCRIPT_DIR/../lib/init.sh"
source "$SCRIPT_DIR/../lib/common/validation.sh"

cd "$PROJECT_ROOT"

log_section "Development Environment Setup"

# Check for required commands
log_subsection "Checking dependencies"

REQUIRED_COMMANDS=(
    "bash"
    "git"
    "docker"
    "python3"
    "node"
    "npm"
)

MISSING_COMMANDS=()

for cmd in "${REQUIRED_COMMANDS[@]}"; do
    if command_exists "$cmd"; then
        log_success "$cmd is installed"
    else
        log_warn "$cmd is not installed"
        MISSING_COMMANDS+=("$cmd")
    fi
done

# Optional tools
log_subsection "Checking optional tools"

OPTIONAL_COMMANDS=(
    "shellcheck:Static analysis for shell scripts"
    "shfmt:Shell script formatter"
    "yamllint:YAML linter"
    "terraform:Infrastructure as code"
    "kubectl:Kubernetes CLI"
    "az:Azure CLI"
)

for cmd_info in "${OPTIONAL_COMMANDS[@]}"; do
    IFS=':' read -r cmd desc <<< "$cmd_info"
    if command_exists "$cmd"; then
        log_success "$cmd is installed"
    else
        log_info "$cmd is not installed (optional: $desc)"
    fi
done

# Install shellcheck if missing
if ! command_exists shellcheck && command_exists apt-get; then
    log_info "Installing shellcheck..."
    sudo apt-get update
    sudo apt-get install -y shellcheck
fi

# Install shfmt if missing
if ! command_exists shfmt && command_exists go; then
    log_info "Installing shfmt..."
    go install mvdan.cc/sh/v3/cmd/shfmt@latest
fi

# Setup pre-commit hooks
log_subsection "Setting up pre-commit hooks"

if [ -d ".git" ]; then
    if [ ! -f ".git/hooks/pre-commit" ]; then
        log_info "Creating pre-commit hook..."
        cat > .git/hooks/pre-commit <<'EOF'
#!/usr/bin/env bash
# Pre-commit hook for code quality checks

set -e

# Run shellcheck on shell scripts
if command -v shellcheck &>/dev/null; then
    git diff --cached --name-only --diff-filter=ACM | grep '\.sh$' | while read -r file; do
        shellcheck "$file" || exit 1
    done
fi

# Run yamllint on YAML files
if command -v yamllint &>/dev/null; then
    git diff --cached --name-only --diff-filter=ACM | grep -E '\.(yml|yaml)$' | while read -r file; do
        yamllint "$file" || exit 1
    done
fi
EOF
        chmod +x .git/hooks/pre-commit
        log_success "Pre-commit hook created"
    else
        log_info "Pre-commit hook already exists"
    fi
fi

# Create .env.example if it doesn't exist
if [ ! -f ".env.example" ]; then
    log_info "Creating .env.example..."
    cat > .env.example <<'EOF'
# Environment Configuration
# Copy this file to .env and update with your values

# RPC Configuration
RPC_URL=http://localhost:8545

# Chain Configuration
CHAIN_ID=138

# Azure Configuration (if using Azure)
AZURE_SUBSCRIPTION_ID=
AZURE_RESOURCE_GROUP=
AZURE_LOCATION=westeurope

# Key Vault Configuration
KEY_VAULT_NAME=

# Deployment Configuration
ENVIRONMENT=development
EOF
    log_success ".env.example created"
fi

log_success "Development environment setup complete"

if [ ${#MISSING_COMMANDS[@]} -gt 0 ]; then
    log_warn "Missing required commands: ${MISSING_COMMANDS[*]}"
    log_info "Please install missing commands before continuing"
fi

