#!/bin/bash
# glitch gate API Helper Script
# Usage: source this file, then use the functions

# Configuration - set these or export before sourcing
GNOMMO_API_URL="${GNOMMO_API_URL:-http://localhost:3001}"
# CONTENT_API_KEY should be set in environment

# Check if API key is set
check_api_key() {
  if [ -z "$CONTENT_API_KEY" ]; then
    echo "Error: CONTENT_API_KEY not set"
    echo "Run: export CONTENT_API_KEY=your-key-here"
    return 1
  fi
}

# List all techs
list_techs() {
  check_api_key || return 1
  curl -s -H "Authorization: Bearer $CONTENT_API_KEY" \
    "$GNOMMO_API_URL/api/content/techs" | jq
}

# List techs without gates (available for linking)
list_available_techs() {
  check_api_key || return 1
  curl -s -H "Authorization: Bearer $CONTENT_API_KEY" \
    "$GNOMMO_API_URL/api/content/techs/available" | jq
}

# List all glitch gates
list_gates() {
  check_api_key || return 1
  curl -s -H "Authorization: Bearer $CONTENT_API_KEY" \
    "$GNOMMO_API_URL/api/content/deglitch-gates" | jq
}

# Get a specific gate by ID
get_gate() {
  check_api_key || return 1
  local gate_id=$1
  if [ -z "$gate_id" ]; then
    echo "Usage: get_gate <gate_id>"
    return 1
  fi
  curl -s -H "Authorization: Bearer $CONTENT_API_KEY" \
    "$GNOMMO_API_URL/api/content/deglitch-gates/$gate_id" | jq
}

# Create a gate from JSON file
create_gate() {
  check_api_key || return 1
  local json_file=$1
  if [ -z "$json_file" ]; then
    echo "Usage: create_gate <json_file>"
    return 1
  fi
  if [ ! -f "$json_file" ]; then
    echo "Error: File not found: $json_file"
    return 1
  fi
  curl -s -X POST \
    -H "Authorization: Bearer $CONTENT_API_KEY" \
    -H "Content-Type: application/json" \
    -d @"$json_file" \
    "$GNOMMO_API_URL/api/content/deglitch-gates" | jq
}

# Create a gate from JSON string
create_gate_json() {
  check_api_key || return 1
  local json_data=$1
  if [ -z "$json_data" ]; then
    echo "Usage: create_gate_json '<json_string>'"
    return 1
  fi
  curl -s -X POST \
    -H "Authorization: Bearer $CONTENT_API_KEY" \
    -H "Content-Type: application/json" \
    -d "$json_data" \
    "$GNOMMO_API_URL/api/content/deglitch-gates" | jq
}

# Update a gate from JSON file
update_gate() {
  check_api_key || return 1
  local gate_id=$1
  local json_file=$2
  if [ -z "$gate_id" ] || [ -z "$json_file" ]; then
    echo "Usage: update_gate <gate_id> <json_file>"
    return 1
  fi
  if [ ! -f "$json_file" ]; then
    echo "Error: File not found: $json_file"
    return 1
  fi
  curl -s -X PUT \
    -H "Authorization: Bearer $CONTENT_API_KEY" \
    -H "Content-Type: application/json" \
    -d @"$json_file" \
    "$GNOMMO_API_URL/api/content/deglitch-gates/$gate_id" | jq
}

# Delete a gate
delete_gate() {
  check_api_key || return 1
  local gate_id=$1
  if [ -z "$gate_id" ]; then
    echo "Usage: delete_gate <gate_id>"
    return 1
  fi
  read -p "Delete gate $gate_id? (y/N) " confirm
  if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
    curl -s -X DELETE \
      -H "Authorization: Bearer $CONTENT_API_KEY" \
      "$GNOMMO_API_URL/api/content/deglitch-gates/$gate_id" | jq
  else
    echo "Cancelled"
  fi
}

# Print available commands
deglitch_help() {
  echo "glitch gate API Commands:"
  echo ""
  echo "  list_techs           - List all techs"
  echo "  list_available_techs - List techs without gates"
  echo "  list_gates           - List all glitch gates"
  echo "  get_gate <id>        - Get gate details"
  echo "  create_gate <file>   - Create gate from JSON file"
  echo "  create_gate_json '<json>' - Create gate from JSON string"
  echo "  update_gate <id> <file>   - Update gate from JSON file"
  echo "  delete_gate <id>     - Delete a gate"
  echo ""
  echo "Configuration:"
  echo "  GNOMMO_API_URL=$GNOMMO_API_URL"
  echo "  CONTENT_API_KEY=$([ -n "$CONTENT_API_KEY" ] && echo "[set]" || echo "[not set]")"
}

echo "DEGLITCH API helper loaded. Run 'deglitch_help' for commands."
