#!/bin/bash
#
# test_api.sh — cURL test suite for Container Management API
#

# ========= CONFIG =========
API_KEY="your-secret-api-key"
CONTAINER_ID="001-002-123e4567-e89b-12d3-a456-426614174000"   # sample UUID
BASE_URL="https://dev.odoo4projects.com"
# ==========================

# --- Functions for each endpoint ---

root_redirect() {
  curl -i "$BASE_URL/"
}

update_container() {
  curl -k -X POST "$BASE_URL/container/update" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{
      "UUID": "'"$CONTAINER_ID"'",
      "email": "user@example.com",
      "expires": "2025-12-31",
      "status": "active",
      "created": "2025-08-16",
      "tags": "N8N, SQLITE",
      "affiliate": "OLIVER",
      "domains": "N8N.local",
      "status":"hot",
      "env": {
        "BRANCH": "release"
      },
      "secret": {
        "psql": "5Sg9gDxYQH44QNSPyOx"
      }
 
    }'
}

start_container() {
  curl -k -X POST "$BASE_URL/container/start" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

stop_container() {
  curl -k -X POST "$BASE_URL/container/stop" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

nuke_container() {
  curl -k -X POST "$BASE_URL/container/nuke" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

info_all() {
  curl -k -X POST "$BASE_URL/container/info" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{}'
}

info_one() {
  curl -k -X POST "$BASE_URL/container/info" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

bump_container() {
  curl -k -X POST "$BASE_URL/container/bump" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

container_quota() {
  curl -k -X POST "$BASE_URL/container/quota" \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $API_KEY" \
    -d '{ "container_id": "'"$CONTAINER_ID"'" }'
}

system_containers() {
  curl -k -X GET "$BASE_URL/system/containers" \
    -H "X-API-Key: $API_KEY"
}

system_images() {
  curl -k -X GET "$BASE_URL/system/images" \
    -H "X-API-Key: $API_KEY"
}

system_info() {
  curl -k -X GET "$BASE_URL/system/info" \
    -H "X-API-Key: $API_KEY"
}

system_pull() {
  curl -k -X POST "$BASE_URL/system/pull" \
    -H "X-API-Key: $API_KEY"
}

# --- Help message ---
show_help() {
  cat <<EOF
Usage: $0 <command>

Available commands:
  root_redirect       - Test root redirect
  update_container    - Create or update container
  start_container     - Start container
  stop_container      - Stop container
  nuke_container      - Nuke container (status must be "nuke")
  info_all            - Get info for all containers
  info_one            - Get info for one container
  bump_container      - Bump container
  container_quota     - Show container quota
  system_containers   - List running containers
  system_images       - List docker images
  system_info         - Show system information
  system_pull         - Pull all container images

Examples:
  $0 update_container
  $0 system_info
EOF
}

# --- Main ---
if [ $# -eq 0 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  show_help
  exit 0
fi

cmd="$1"
shift
if declare -f "$cmd" >/dev/null 2>&1; then
  "$cmd" "$@"
else
  echo "Unknown command: $cmd"
  show_help
  exit 1
fi

