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

# Script Name: api-lab
# Description: Quick launcher for API testing vulnerable apps (crAPI and vAPI)
# Usage: api-lab start|stop|status|logs

VERSION="1.0.0"

# Colors
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly CYAN='\033[0;36m'
readonly BOLD='\033[1m'
readonly NC='\033[0m'

LAB_DIR="$HOME/api-lab"
CRAPI_DIR="$LAB_DIR/crapi"
VAPI_DIR="$LAB_DIR/vapi"

show_help() {
  echo -e "${BOLD}api-lab${NC} - API Testing Lab Manager v${VERSION}"
  echo
  echo -e "${BOLD}USAGE:${NC}"
  echo "  api-lab <command> [app]"
  echo
  echo -e "${BOLD}COMMANDS:${NC}"
  echo -e "  ${CYAN}start [app]${NC}    Start lab containers (crapi, vapi, or both)"
  echo -e "  ${CYAN}stop [app]${NC}     Stop lab containers"
  echo -e "  ${CYAN}status${NC}         Show running containers"
  echo -e "  ${CYAN}logs [app]${NC}     Show container logs"
  echo -e "  ${CYAN}setup${NC}          Initial lab setup (clone repos)"
  echo
  echo -e "${BOLD}APPS:${NC}"
  echo -e "  ${YELLOW}crapi${NC}   - Completely Ridiculous API (OWASP)"
  echo -e "  ${YELLOW}vapi${NC}    - Vulnerable API"
  echo -e "  ${YELLOW}both${NC}    - Start/stop both apps (default)"
  echo
  echo -e "${BOLD}EXAMPLES:${NC}"
  echo "  api-lab start         # Start both labs"
  echo "  api-lab start crapi   # Start only crAPI"
  echo "  api-lab stop          # Stop both labs"
  echo "  api-lab status        # Check what's running"
  echo "  api-lab logs vapi     # View vAPI logs"
  echo
  echo -e "${BOLD}URLS:${NC}"
  echo "  crAPI:   http://127.0.0.1:8888"
  echo "  Mailhog: http://127.0.0.1:8025"
  echo "  vAPI:    http://127.0.0.1/vapi"
}

# Check if docker is available
check_docker() {
  if ! command -v docker &>/dev/null; then
    echo -e "${RED}Error:${NC} Docker not installed"
    echo "Install: sudo apt install docker.io docker-compose"
    exit 1
  fi
}

# Setup lab directories
setup_lab() {
  echo -e "${CYAN}[*]${NC} Setting up API testing lab..."

  mkdir -p "$LAB_DIR"

  # crAPI
  if [[ ! -d "$CRAPI_DIR" ]]; then
    echo -e "${YELLOW}[*]${NC} Setting up crAPI..."
    mkdir -p "$CRAPI_DIR"
    cd "$CRAPI_DIR"
    curl -o docker-compose.yml https://raw.githubusercontent.com/OWASP/crAPI/main/deploy/docker/docker-compose.yml
    sudo docker-compose pull
    echo -e "${GREEN}✓${NC} crAPI setup complete"
  else
    echo -e "${GREEN}✓${NC} crAPI already exists"
  fi

  # vAPI
  if [[ ! -d "$VAPI_DIR" ]]; then
    echo -e "${YELLOW}[*]${NC} Setting up vAPI..."
    cd "$LAB_DIR"
    git clone https://github.com/roottusk/vapi.git
    cd "$VAPI_DIR"
    sudo docker-compose pull
    echo -e "${GREEN}✓${NC} vAPI setup complete"
  else
    echo -e "${GREEN}✓${NC} vAPI already exists"
  fi

  echo
  echo -e "${GREEN}✓${NC} Lab setup complete!"
  echo -e "${CYAN}[*]${NC} Run: ${BOLD}api-lab start${NC} to launch"
}

# Start containers
start_lab() {
  local app="${1:-both}"

  case "$app" in
    crapi)
      if [[ ! -d "$CRAPI_DIR" ]]; then
        echo -e "${RED}Error:${NC} crAPI not set up. Run: api-lab setup"
        exit 1
      fi
      echo -e "${CYAN}[*]${NC} Starting crAPI..."
      cd "$CRAPI_DIR"
      sudo docker-compose -f docker-compose.yml --compatibility up -d
      echo -e "${GREEN}✓${NC} crAPI started"
      echo -e "${CYAN}[*]${NC} Access at: ${BOLD}http://127.0.0.1:8888${NC}"
      echo -e "${CYAN}[*]${NC} Mailhog at: ${BOLD}http://127.0.0.1:8025${NC}"
      ;;

    vapi)
      if [[ ! -d "$VAPI_DIR" ]]; then
        echo -e "${RED}Error:${NC} vAPI not set up. Run: api-lab setup"
        exit 1
      fi
      echo -e "${CYAN}[*]${NC} Starting vAPI..."
      cd "$VAPI_DIR"
      sudo docker-compose up -d
      echo -e "${GREEN}✓${NC} vAPI started"
      echo -e "${CYAN}[*]${NC} Access at: ${BOLD}http://127.0.0.1/vapi${NC}"
      ;;

    both|all)
      start_lab crapi
      echo
      start_lab vapi
      ;;

    *)
      echo -e "${RED}Error:${NC} Unknown app: $app"
      echo "Valid options: crapi, vapi, both"
      exit 1
      ;;
  esac
}

# Stop containers
stop_lab() {
  local app="${1:-both}"

  case "$app" in
    crapi)
      if [[ -d "$CRAPI_DIR" ]]; then
        echo -e "${CYAN}[*]${NC} Stopping crAPI..."
        cd "$CRAPI_DIR"
        sudo docker-compose stop
        echo -e "${GREEN}✓${NC} crAPI stopped"
      fi
      ;;

    vapi)
      if [[ -d "$VAPI_DIR" ]]; then
        echo -e "${CYAN}[*]${NC} Stopping vAPI..."
        cd "$VAPI_DIR"
        sudo docker-compose stop
        echo -e "${GREEN}✓${NC} vAPI stopped"
      fi
      ;;

    both|all)
      stop_lab crapi
      stop_lab vapi
      ;;

    *)
      echo -e "${RED}Error:${NC} Unknown app: $app"
      exit 1
      ;;
  esac
}

# Show status
show_status() {
  echo -e "${BOLD}${CYAN}API Lab Status:${NC}"
  echo
  sudo docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "(NAMES|crapi|vapi)" || echo "No lab containers running"
}

# Show logs
show_logs() {
  local app="${1:-}"

  if [[ -z "$app" ]]; then
    echo -e "${RED}Error:${NC} Specify app: crapi or vapi"
    exit 1
  fi

  case "$app" in
    crapi)
      cd "$CRAPI_DIR"
      sudo docker-compose logs -f
      ;;
    vapi)
      cd "$VAPI_DIR"
      sudo docker-compose logs -f
      ;;
    *)
      echo -e "${RED}Error:${NC} Unknown app: $app"
      exit 1
      ;;
  esac
}

# Main
check_docker

if [[ $# -eq 0 ]] || [[ "$1" =~ ^(-h|--help|help)$ ]]; then
  show_help
  exit 0
fi

command="$1"
shift

case "$command" in
  setup|install)
    setup_lab
    ;;
  start|up)
    start_lab "${1:-both}"
    ;;
  stop|down)
    stop_lab "${1:-both}"
    ;;
  status|ps)
    show_status
    ;;
  logs)
    show_logs "${1:-}"
    ;;
  *)
    echo -e "${RED}Error:${NC} Unknown command: $command"
    echo "Run 'api-lab --help' for usage"
    exit 1
    ;;
esac
