#!/usr/bin/env bash
#
# stack-list.sh - List all Portainer stacks
#
# Usage: stack-list.sh [-e endpoint_id] [-f format] [-q]
#
# Environment variables:
#   PORTAINER_URL     - Portainer instance URL (e.g., https://portainer.example.com:9443)
#   PORTAINER_API_KEY - API access token
#
# Options:
#   -e endpoint_id    Filter by endpoint/environment ID
#   -f format         Output format: table (default), json, names
#   -q                Quiet mode - only output stack names (shortcut for -f names)
#   -h                Show this help

set -euo pipefail

# Default values
ENDPOINT_FILTER=""
FORMAT="table"
QUIET=false

# Parse arguments
while getopts "e:f:qh" opt; do
    case $opt in
        e) ENDPOINT_FILTER="$OPTARG" ;;
        f) FORMAT="$OPTARG" ;;
        q) QUIET=true; FORMAT="names" ;;
        h)
            head -20 "$0" | grep "^#" | sed 's/^# \?//'
            exit 0
            ;;
        *)
            echo "Usage: $0 [-e endpoint_id] [-f format] [-q]" >&2
            exit 1
            ;;
    esac
done

# Validate environment
if [[ -z "${PORTAINER_URL:-}" ]]; then
    echo "Error: PORTAINER_URL environment variable not set" >&2
    exit 1
fi

if [[ -z "${PORTAINER_API_KEY:-}" ]]; then
    echo "Error: PORTAINER_API_KEY environment variable not set" >&2
    exit 1
fi

# Remove trailing slash from URL
PORTAINER_URL="${PORTAINER_URL%/}"

# TLS options
CURL_OPTS=()
if [ "${PORTAINER_INSECURE:-0}" = "1" ]; then
    CURL_OPTS+=(-k)
fi

# Fetch stacks
response=$(curl -s "${CURL_OPTS[@]}" -w "\n%{http_code}" \
    -H "X-API-Key: ${PORTAINER_API_KEY}" \
    "${PORTAINER_URL}/api/stacks")

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [[ "$http_code" != "200" ]]; then
    echo "Error: API request failed with status $http_code" >&2
    echo "$body" >&2
    exit 1
fi

# Filter by endpoint if specified
if [[ -n "$ENDPOINT_FILTER" ]]; then
    body=$(echo "$body" | jq --arg eid "$ENDPOINT_FILTER" '[.[] | select(.EndpointId == ($eid | tonumber))]')
fi

# Output based on format
case "$FORMAT" in
    json)
        echo "$body" | jq '.'
        ;;
    names)
        echo "$body" | jq -r '.[].Name'
        ;;
    table)
        echo "ID    NAME                          STATUS    TYPE      ENDPOINT  CREATED"
        echo "----  ----------------------------  --------  --------  --------  -------"
        echo "$body" | jq -r '.[] | [
            .Id,
            .Name,
            (if .Status == 1 then "active" elif .Status == 2 then "inactive" else "unknown" end),
            (if .Type == 1 then "swarm" elif .Type == 2 then "compose" elif .Type == 3 then "k8s" else "unknown" end),
            .EndpointId,
            (.CreationDate | split("T")[0] // "N/A")
        ] | @tsv' | while IFS=$'\t' read -r id name status type endpoint created; do
            printf "%-4s  %-28s  %-8s  %-8s  %-8s  %s\n" "$id" "$name" "$status" "$type" "$endpoint" "$created"
        done
        ;;
    *)
        echo "Error: Unknown format '$FORMAT'. Use: table, json, names" >&2
        exit 1
        ;;
esac
