#!/bin/bash
#
# DSS - Docker Discovery
# Container status, images, networks, volumes
#

set -e

PROJECT_PATH="${1:-.}"

# Check if Docker is available
if ! command -v docker &> /dev/null; then
  cat <<EOF
{
  "scan_type": "docker",
  "available": false,
  "message": "Docker not installed or not in PATH"
}
EOF
  exit 0
fi

# Check if Docker daemon is running
if ! docker info &> /dev/null; then
  cat <<EOF
{
  "scan_type": "docker",
  "available": true,
  "daemon_running": false,
  "message": "Docker daemon not running or no permissions"
}
EOF
  exit 0
fi

# Get running containers
get_containers() {
  local containers=()

  while IFS= read -r line; do
    if [[ -n "$line" ]]; then
      local id=$(echo "$line" | cut -d'|' -f1)
      local name=$(echo "$line" | cut -d'|' -f2)
      local image=$(echo "$line" | cut -d'|' -f3)
      local status=$(echo "$line" | cut -d'|' -f4)
      local ports=$(echo "$line" | cut -d'|' -f5 | sed 's/"/\\"/g')

      containers+=("{\"id\":\"$id\",\"name\":\"$name\",\"image\":\"$image\",\"status\":\"$status\",\"ports\":\"$ports\"}")
    fi
  done < <(docker ps --format '{{.ID}}|{{.Names}}|{{.Image}}|{{.Status}}|{{.Ports}}' 2>/dev/null)

  echo "${containers[@]}"
}

# Get images
get_images() {
  local images=()

  while IFS= read -r line; do
    if [[ -n "$line" ]]; then
      local repo=$(echo "$line" | cut -d'|' -f1)
      local tag=$(echo "$line" | cut -d'|' -f2)
      local size=$(echo "$line" | cut -d'|' -f3)

      images+=("{\"repository\":\"$repo\",\"tag\":\"$tag\",\"size\":\"$size\"}")
    fi
  done < <(docker images --format '{{.Repository}}|{{.Tag}}|{{.Size}}' 2>/dev/null | head -20)

  echo "${images[@]}"
}

# Check for docker-compose files
get_compose_info() {
  local compose_files=()

  for file in "docker-compose.yml" "docker-compose.yaml" "compose.yml" "compose.yaml"; do
    if [[ -f "$PROJECT_PATH/$file" ]]; then
      local services=$(grep -E "^  [a-zA-Z]" "$PROJECT_PATH/$file" 2>/dev/null | sed 's/://g' | tr -d ' ' | head -10)
      compose_files+=("{\"file\":\"$file\",\"services\":$(echo "$services" | jq -R -s 'split("\n") | map(select(. != ""))')}")
    fi
  done

  echo "${compose_files[@]}"
}

# Get resource usage
get_stats() {
  local stats=$(docker stats --no-stream --format '{"name":"{{.Name}}","cpu":"{{.CPUPerc}}","memory":"{{.MemUsage}}"}' 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
  echo "[$stats]"
}

# Build output
containers=$(get_containers)
images=$(get_images)
compose=$(get_compose_info)
stats=$(get_stats)

containers_json=$(IFS=,; echo "${containers[*]}")
images_json=$(IFS=,; echo "${images[*]}")
compose_json=$(IFS=,; echo "${compose[*]}")

cat <<EOF
{
  "scan_type": "docker",
  "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "available": true,
  "daemon_running": true,
  "docker_version": "$(docker --version | cut -d' ' -f3 | tr -d ',')",
  "containers": {
    "running": $(docker ps -q 2>/dev/null | wc -l),
    "total": $(docker ps -aq 2>/dev/null | wc -l),
    "list": [${containers_json:-}]
  },
  "images": {
    "total": $(docker images -q 2>/dev/null | wc -l),
    "list": [${images_json:-}]
  },
  "compose_files": [${compose_json:-}],
  "resource_usage": ${stats:-[]}
}
EOF
