#!/usr/bin/env bash
set -euo pipefail

echo "=== pentestswarm development setup ==="
echo ""

# Check prerequisites
command -v go >/dev/null 2>&1 || { echo "ERROR: Go is not installed. Install from https://go.dev/dl/"; exit 1; }
command -v docker >/dev/null 2>&1 || { echo "ERROR: Docker is not installed. Install from https://docker.com"; exit 1; }

echo "[1/5] Installing Go development tools..."
go install github.com/air-verse/air@latest 2>/dev/null || echo "  air already installed or skipped"
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest 2>/dev/null || echo "  sqlc already installed or skipped"
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest 2>/dev/null || echo "  golangci-lint already installed or skipped"

echo "[2/5] Starting services (PostgreSQL, Redis, Ollama)..."
docker compose -f deploy/docker-compose.dev.yml up -d

echo "[3/5] Waiting for services to be healthy..."
for i in $(seq 1 30); do
    if docker compose -f deploy/docker-compose.dev.yml ps --format json 2>/dev/null | grep -q '"healthy"' || \
       docker compose -f deploy/docker-compose.dev.yml ps 2>/dev/null | grep -q "healthy"; then
        break
    fi
    sleep 1
done

echo "[4/5] Building pentestswarm..."
make build

echo "[5/5] Running health check..."
./bin/pentestswarm --version

echo ""
echo "=== Setup complete ==="
echo ""
echo "Services running:"
echo "  PostgreSQL: localhost:5432 (user: pentestswarm, password: pentestswarm_dev)"
echo "  Redis:      localhost:6379"
echo "  Ollama:     localhost:11434"
echo ""
echo "Quick start:"
echo "  make dev        # Start with hot-reload"
echo "  make test       # Run tests"
echo "  make lint       # Run linter"
echo ""
