#!/usr/bin/env bash
#
# stack-redeploy.sh - Redeploy a Portainer stack
#
# For git-based stacks, this pulls the latest from the repository and redeploys.
# For file-based stacks, this redeploys with the current stack file.
#
# Usage: stack-redeploy.sh -n <stack-name> [-p] [-e endpoint_id]
#
# Environment variables:
#   PORTAINER_URL     - Portainer instance URL (e.g., https://portainer.example.com:9443)
#   PORTAINER_API_KEY - API access token
#
# Options:
#   -n name           Stack name (required)
#   -i id             Stack ID (alternative to -n)
#   -p                Pull latest images before redeploying
#   -e endpoint_id    Endpoint/environment ID (auto-detected from stack if not provided)
#   -h                Show this help

set -euo pipefail

# Default values
STACK_NAME=""
STACK_ID=""
PULL_IMAGE=false
ENDPOINT_ID=""

# Parse arguments
while getopts "n:i:pe:h" opt; do
    case $opt in
        n) STACK_NAME="$OPTARG" ;;
        i) STACK_ID="$OPTARG" ;;
        p) PULL_IMAGE=true ;;
        e) ENDPOINT_ID="$OPTARG" ;;
        h)
            head -22 "$0" | grep "^#" | sed 's/^# \?//'
            exit 0
            ;;
        *)
            echo "Usage: $0 -n <stack-name> [-p] [-e endpoint_id]" >&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

if [[ -z "$STACK_NAME" && -z "$STACK_ID" ]]; then
    echo "Error: Either -n <stack-name> or -i <stack-id> is required" >&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

# Function to make API requests
api_request() {
    local method="$1"
    local endpoint="$2"
    local data="${3:-}"

    local args=(-s "${CURL_OPTS[@]}" -w "\n%{http_code}" -X "$method" -H "X-API-Key: ${PORTAINER_API_KEY}")

    if [[ -n "$data" ]]; then
        args+=(-H "Content-Type: application/json" -d "$data")
    fi

    curl "${args[@]}" "${PORTAINER_URL}${endpoint}"
}

# Get stack info by name or ID
if [[ -n "$STACK_NAME" ]]; then
    echo "Looking up stack '$STACK_NAME'..."
    response=$(api_request GET "/api/stacks")
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | sed '$d')

    if [[ "$http_code" != "200" ]]; then
        echo "Error: Failed to list stacks (HTTP $http_code)" >&2
        exit 1
    fi

    stack_info=$(echo "$body" | jq --arg name "$STACK_NAME" '.[] | select(.Name == $name)')

    if [[ -z "$stack_info" || "$stack_info" == "null" ]]; then
        echo "Error: Stack '$STACK_NAME' not found" >&2
        exit 1
    fi

    STACK_ID=$(echo "$stack_info" | jq -r '.Id')
    ENDPOINT_ID_FROM_STACK=$(echo "$stack_info" | jq -r '.EndpointId')
else
    # Get stack info by ID
    response=$(api_request GET "/api/stacks/${STACK_ID}")
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | sed '$d')

    if [[ "$http_code" != "200" ]]; then
        echo "Error: Failed to get stack (HTTP $http_code)" >&2
        exit 1
    fi

    stack_info="$body"
    STACK_NAME=$(echo "$stack_info" | jq -r '.Name')
    ENDPOINT_ID_FROM_STACK=$(echo "$stack_info" | jq -r '.EndpointId')
fi

# Use endpoint ID from stack if not provided
if [[ -z "$ENDPOINT_ID" ]]; then
    ENDPOINT_ID="$ENDPOINT_ID_FROM_STACK"
fi

# Check if this is a git-based stack
git_config=$(echo "$stack_info" | jq -r '.GitConfig // empty')

if [[ -n "$git_config" && "$git_config" != "null" ]]; then
    echo "Stack '$STACK_NAME' (ID: $STACK_ID) is git-based"
    echo "Triggering git pull and redeploy..."

    # Git-based stack redeploy
    # The git redeploy endpoint pulls from the repository and redeploys
    request_body=$(jq -n \
        --argjson pullImage "$PULL_IMAGE" \
        '{
            "pullImage": $pullImage,
            "prune": false,
            "repositoryReferenceName": "",
            "repositoryAuthentication": false
        }')

    response=$(api_request PUT "/api/stacks/${STACK_ID}/git/redeploy?endpointId=${ENDPOINT_ID}" "$request_body")
else
    echo "Stack '$STACK_NAME' (ID: $STACK_ID) is file-based"

    # Get the current stack file content
    echo "Fetching current stack file..."
    response=$(api_request GET "/api/stacks/${STACK_ID}/file")
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | sed '$d')

    if [[ "$http_code" != "200" ]]; then
        echo "Error: Failed to get stack file (HTTP $http_code)" >&2
        exit 1
    fi

    stack_file_content=$(echo "$body" | jq -r '.StackFileContent')

    echo "Redeploying..."
    request_body=$(jq -n \
        --argjson pullImage "$PULL_IMAGE" \
        --arg stackFile "$stack_file_content" \
        '{
            "pullImage": $pullImage,
            "prune": false,
            "stackFileContent": $stackFile
        }')

    response=$(api_request PUT "/api/stacks/${STACK_ID}?endpointId=${ENDPOINT_ID}" "$request_body")
fi

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

if [[ "$http_code" == "200" ]]; then
    echo "Successfully redeployed stack '$STACK_NAME'"
    if [[ "$PULL_IMAGE" == "true" ]]; then
        echo "  - Pulled latest images"
    fi
else
    echo "Error: Redeploy failed (HTTP $http_code)" >&2
    echo "$body" | jq '.' 2>/dev/null || echo "$body" >&2
    exit 1
fi
