#!/usr/bin/env bash
# Developer setup script for CleverAgents
# Usage: bash scripts/setup-dev.sh
#
# This script sets up the development environment including:
# - Installing all dependencies (dev, tests, docs extras)
# - Installing pre-commit hooks
# - Verifying tool installations
# - Running an initial quality check

set -euo pipefail

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

info() { echo -e "${BOLD}${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${BOLD}${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${BOLD}${RED}[ERROR]${NC} $*"; }

# Check Python version
info "Checking Python version..."
PYTHON_VERSION=$(python3 --version 2>&1 | grep -oP '\d+\.\d+')
REQUIRED_VERSION="3.13"
if [ "$(printf '%s\n' "$REQUIRED_VERSION" "$PYTHON_VERSION" | sort -V | head -n1)" != "$REQUIRED_VERSION" ]; then
    error "Python >= $REQUIRED_VERSION is required, found $PYTHON_VERSION"
    exit 1
fi
info "Python $PYTHON_VERSION detected"

# Install the project with all development extras
info "Installing project with dev, tests, and docs extras..."
pip install -e ".[dev,tests,docs]" --quiet

# Install pre-commit hooks
info "Installing pre-commit hooks..."
pre-commit install
pre-commit install --hook-type commit-msg
info "Pre-commit hooks installed (pre-commit and commit-msg stages)"

# Verify key tools are available
info "Verifying tool installations..."
TOOLS_OK=true

for tool in ruff pyright bandit vulture radon pre-commit behave; do
    if command -v "$tool" > /dev/null 2>&1; then
        VERSION=$("$tool" --version 2>&1 | head -1)
        info "  $tool: $VERSION"
    else
        warn "  $tool: NOT FOUND (check PATH or installation)"
        TOOLS_OK=false
    fi
done

if [ "$TOOLS_OK" = false ]; then
    warn "Some tools were not found. They may be installed in a location not on your PATH."
    warn "Try: export PATH=\"\$HOME/.local/bin:\$PATH\""
fi

# Run a quick quality check
info "Running initial quality check..."
echo ""
info "--- Ruff Format Check ---"
ruff format --check . 2>&1 || warn "Some files need formatting. Run: ruff format ."

echo ""
info "--- Ruff Lint Check ---"
ruff check . 2>&1 || warn "Some lint issues found. Run: ruff check --fix ."

echo ""
info "--- Pyright Type Check ---"
pyright 2>&1 || warn "Type checking found issues."

echo ""
info "Development environment setup complete."
info ""
info "Available commands:"
info "  nox                      - Run all quality checks and tests"
info "  nox -s unit_tests        - Run Behave unit tests"
info "  nox -s integration_tests - Run Robot integration tests"
info "  nox -s coverage_report   - Run tests with coverage"
info "  nox -s lint              - Run linter"
info "  nox -s format            - Format code"
info "  nox -s typecheck         - Run type checker"
info "  pre-commit run --all-files - Run all pre-commit hooks"
