#!/bin/bash
################################################################################
# Script Name: dokploy-exporter.sh
# Version: 1.0
# Description: Prometheus exporter for Dokploy PaaS providing operational
#              metrics via the Dokploy API — project counts, application status,
#              database breakdown by type, compose services, server info,
#              and API health
#
# Author: Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
#
# Prerequisites:
#   - Dokploy instance running with API enabled
#   - Dokploy API key (generate in Settings → API)
#   - curl for API calls
#   - jq for JSON parsing
#   - netcat (nc) for HTTP mode
#
# Usage:
#   # Output to stdout
#   ./dokploy-exporter.sh
#
#   # HTTP server mode
#   ./dokploy-exporter.sh --http -p 9197
#
#   # Textfile collector mode
#   ./dokploy-exporter.sh --textfile
#
#   # Custom API token and URL
#   ./dokploy-exporter.sh --api-url http://dokploy.local:3000 --api-token mytoken
#
# Metrics Exported:
#   - dokploy_up - API reachability (1=up, 0=down)
#   - dokploy_info{version} - Dokploy version info
#   - dokploy_projects_total - Total project count
#   - dokploy_applications_total - Total applications across all projects
#   - dokploy_applications_by_status{status} - Applications by status
#   - dokploy_compose_services_total - Total Docker Compose services
#   - dokploy_databases_total - Total managed databases
#   - dokploy_databases_by_type{type} - Databases by type
#   - dokploy_servers_total - Total servers managed
#   - dokploy_exporter_duration_seconds - Script execution time
#   - dokploy_exporter_last_run_timestamp - Last run timestamp
#
# Configuration:
#   Default HTTP port: 9197
#   Default API URL: http://localhost:3000
#   Textfile directory: /var/lib/node_exporter
#
################################################################################

set -euo pipefail

# ============================================================================
# CONFIGURATION VARIABLES
# ============================================================================

TEXTFILE_DIR="/var/lib/node_exporter"
OUTPUT_FILE=""
HTTP_MODE=false
HTTP_PORT=9197
API_URL="http://localhost:3000"
API_TOKEN=""

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

show_usage() {
    cat <<EOF
Usage: $0 [OPTIONS]

Export Dokploy PaaS statistics as Prometheus metrics via the Dokploy API.

MODES:
    --textfile      Write to node_exporter textfile collector
    --http          Run HTTP server on port $HTTP_PORT

OPTIONS:
    -p, --port        HTTP port (default: 9197)
    -o, --output      Output file path
    --api-url         Dokploy API URL (default: http://localhost:3000)
    --api-token       API key (required)

EXAMPLES:
    $0 --textfile --api-token mytoken              # Write to textfile collector
    $0 --http --port 9197 --api-token mytoken      # Run HTTP server
    $0 --api-token mytoken                         # Output to stdout
    $0 -o /tmp/dokploy.prom --api-token mytoken    # Write to custom file

EOF
    exit 0
}

parse_args() {
    while [[ $# -gt 0 ]]; do
        case $1 in
            -h|--help) show_usage ;;
            --textfile) OUTPUT_FILE="$TEXTFILE_DIR/dokploy.prom"; shift ;;
            --http) HTTP_MODE=true; shift ;;
            -p|--port) HTTP_PORT="$2"; shift 2 ;;
            -o|--output) OUTPUT_FILE="$2"; shift 2 ;;
            --api-url) API_URL="$2"; shift 2 ;;
            --api-token) API_TOKEN="$2"; shift 2 ;;
            *) echo "Unknown option: $1" >&2; exit 1 ;;
        esac
    done
}

# Check prerequisites
# Returns: 0 if OK, 1 if error
check_prerequisites() {
    if ! command -v curl >/dev/null 2>&1; then
        echo "ERROR: curl not found" >&2
        return 1
    fi

    if ! command -v jq >/dev/null 2>&1; then
        echo "ERROR: jq not found (required for JSON parsing)" >&2
        return 1
    fi

    if [ -z "$API_TOKEN" ]; then
        echo "ERROR: --api-token is required" >&2
        return 1
    fi

    return 0
}

# Escape special characters in Prometheus label values
# Args: $1 - string to escape
# Returns: escaped string safe for Prometheus labels
prom_escape() {
    local val="$1"
    val="${val//\\/\\\\}"
    val="${val//\"/\\\"}"
    val="${val//$'\n'/}"
    echo "$val"
}

# Make an authenticated API call
# Args: $1 - API endpoint path (e.g., /api/project.all)
# Returns: JSON response on stdout
api_call() {
    local endpoint="$1"
    curl -s -X GET \
        -H "x-api-key: ${API_TOKEN}" \
        -H "Accept: application/json" \
        "${API_URL}${endpoint}" 2>/dev/null
}

# ============================================================================
# METRIC GENERATION
# ============================================================================

# Generate all Prometheus metrics
# Returns: Prometheus text format metrics on stdout
generate_metrics() {
    local script_start
    script_start=$(date +%s)

    # Check prerequisites
    if ! check_prerequisites; then
        cat <<EOF
# HELP dokploy_up Dokploy API reachability (1=up, 0=down)
# TYPE dokploy_up gauge
dokploy_up 0
EOF
        return
    fi

    # Verify API is responding using server.publicIp as health check
    local health_response
    health_response=$(api_call "/api/server.publicIp" 2>/dev/null)

    if [ -z "$health_response" ]; then
        cat <<EOF
# HELP dokploy_up Dokploy API reachability (1=up, 0=down)
# TYPE dokploy_up gauge
dokploy_up 0
EOF
        return
    fi

    # Check for error responses (invalid API key, etc.)
    local is_error
    is_error=$(echo "$health_response" | jq -r 'if type == "object" and .error then "yes" else "no" end' 2>/dev/null)

    if [ "$is_error" = "yes" ]; then
        cat <<EOF
# HELP dokploy_up Dokploy API reachability (1=up, 0=down)
# TYPE dokploy_up gauge
dokploy_up 0
EOF
        return
    fi

    cat <<EOF
# HELP dokploy_up Dokploy API reachability (1=up, 0=down)
# TYPE dokploy_up gauge
dokploy_up 1
EOF

    echo ""

    # ========================================================================
    # VERSION INFO
    # ========================================================================

    local dokploy_version
    dokploy_version=$(echo "$health_response" | jq -r 'if type == "string" then . else "unknown" end' 2>/dev/null)
    dokploy_version="${dokploy_version:-unknown}"

    cat <<EOF
# HELP dokploy_info Dokploy version information
# TYPE dokploy_info gauge
dokploy_info{version="$(prom_escape "$dokploy_version")"} 1
EOF

    echo ""

    # ========================================================================
    # PROJECT AND APPLICATION METRICS
    # ========================================================================

    local projects_response
    projects_response=$(api_call "/api/project.all")

    local total_projects=0
    local total_apps=0
    local done_apps=0
    local idle_apps=0
    local running_apps=0
    local error_apps=0
    local total_compose=0
    local total_databases=0
    local pg_count=0
    local mysql_count=0
    local mariadb_count=0
    local mongo_count=0
    local redis_count=0

    if [ -n "$projects_response" ] && [ "$projects_response" != "null" ]; then
        total_projects=$(echo "$projects_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        total_projects=${total_projects:-0}

        # Count applications across all projects
        total_apps=$(echo "$projects_response" | jq '[.[] | (.applications // []) | length] | add // 0' 2>/dev/null)
        total_apps=${total_apps:-0}

        # Count applications by status
        done_apps=$(echo "$projects_response" | jq '[.[] | (.applications // [])[] | select(.applicationStatus == "done")] | length' 2>/dev/null)
        idle_apps=$(echo "$projects_response" | jq '[.[] | (.applications // [])[] | select(.applicationStatus == "idle")] | length' 2>/dev/null)
        running_apps=$(echo "$projects_response" | jq '[.[] | (.applications // [])[] | select(.applicationStatus == "running")] | length' 2>/dev/null)
        error_apps=$(echo "$projects_response" | jq '[.[] | (.applications // [])[] | select(.applicationStatus == "error")] | length' 2>/dev/null)
        done_apps=${done_apps:-0}
        idle_apps=${idle_apps:-0}
        running_apps=${running_apps:-0}
        error_apps=${error_apps:-0}

        # Count compose services across all projects
        total_compose=$(echo "$projects_response" | jq '[.[] | (.compose // []) | length] | add // 0' 2>/dev/null)
        total_compose=${total_compose:-0}

        # Count databases by type across all projects
        pg_count=$(echo "$projects_response" | jq '[.[] | (.postgres // []) | length] | add // 0' 2>/dev/null)
        mysql_count=$(echo "$projects_response" | jq '[.[] | (.mysql // []) | length] | add // 0' 2>/dev/null)
        mariadb_count=$(echo "$projects_response" | jq '[.[] | (.mariadb // []) | length] | add // 0' 2>/dev/null)
        mongo_count=$(echo "$projects_response" | jq '[.[] | (.mongo // []) | length] | add // 0' 2>/dev/null)
        redis_count=$(echo "$projects_response" | jq '[.[] | (.redis // []) | length] | add // 0' 2>/dev/null)
        pg_count=${pg_count:-0}
        mysql_count=${mysql_count:-0}
        mariadb_count=${mariadb_count:-0}
        mongo_count=${mongo_count:-0}
        redis_count=${redis_count:-0}

        total_databases=$((pg_count + mysql_count + mariadb_count + mongo_count + redis_count))
    fi

    cat <<EOF
# HELP dokploy_projects_total Total number of projects
# TYPE dokploy_projects_total gauge
dokploy_projects_total $total_projects

# HELP dokploy_applications_total Total number of applications
# TYPE dokploy_applications_total gauge
dokploy_applications_total $total_apps

# HELP dokploy_applications_by_status Applications grouped by status
# TYPE dokploy_applications_by_status gauge
dokploy_applications_by_status{status="done"} $done_apps
dokploy_applications_by_status{status="idle"} $idle_apps
dokploy_applications_by_status{status="running"} $running_apps
dokploy_applications_by_status{status="error"} $error_apps
EOF

    echo ""

    # ========================================================================
    # COMPOSE SERVICE METRICS
    # ========================================================================

    cat <<EOF
# HELP dokploy_compose_services_total Total Docker Compose services
# TYPE dokploy_compose_services_total gauge
dokploy_compose_services_total $total_compose
EOF

    echo ""

    # ========================================================================
    # DATABASE METRICS
    # ========================================================================

    cat <<EOF
# HELP dokploy_databases_total Total managed databases
# TYPE dokploy_databases_total gauge
dokploy_databases_total $total_databases

# HELP dokploy_databases_by_type Databases grouped by type
# TYPE dokploy_databases_by_type gauge
dokploy_databases_by_type{type="postgres"} $pg_count
dokploy_databases_by_type{type="mysql"} $mysql_count
dokploy_databases_by_type{type="mariadb"} $mariadb_count
dokploy_databases_by_type{type="mongo"} $mongo_count
dokploy_databases_by_type{type="redis"} $redis_count
EOF

    echo ""

    # ========================================================================
    # SERVER METRICS
    # ========================================================================

    local servers_response
    servers_response=$(api_call "/api/server.all")

    local total_servers=0

    if [ -n "$servers_response" ] && [ "$servers_response" != "null" ]; then
        total_servers=$(echo "$servers_response" | jq 'if type == "array" then length else 0 end' 2>/dev/null)
        total_servers=${total_servers:-0}
    fi

    cat <<EOF
# HELP dokploy_servers_total Total servers managed
# TYPE dokploy_servers_total gauge
dokploy_servers_total $total_servers
EOF

    echo ""

    # ========================================================================
    # EXPORTER RUNTIME
    # ========================================================================

    local script_end script_duration
    script_end=$(date +%s)
    script_duration=$((script_end - script_start))

    cat <<EOF
# HELP dokploy_exporter_duration_seconds Time to generate all metrics
# TYPE dokploy_exporter_duration_seconds gauge
dokploy_exporter_duration_seconds $script_duration

# HELP dokploy_exporter_last_run_timestamp Unix timestamp of last successful run
# TYPE dokploy_exporter_last_run_timestamp gauge
dokploy_exporter_last_run_timestamp $script_end
EOF

    echo ""
}

# ============================================================================
# HTTP SERVER MODE
# ============================================================================

# Run simple HTTP server using netcat
# Serves metrics on /metrics endpoint
run_http_server() {
    echo "Starting Dokploy exporter on port $HTTP_PORT..." >&2

    if ! command -v nc >/dev/null 2>&1; then
        echo "ERROR: netcat (nc) required for HTTP mode" >&2
        exit 1
    fi

    # Infinite loop accepting HTTP requests
    while true; do
        {
            read -r request
            # Check if request is for /metrics endpoint
            if [[ "$request" =~ ^GET\ /metrics ]]; then
                echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain; version=0.0.4\r\n\r"
                generate_metrics
            else  # Serve HTML landing page for other requests
                echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r"
                cat <<EOF
<!DOCTYPE html>
<html>
<head><title>Dokploy Exporter v1.0</title></head>
<body>
<h1>Dokploy Prometheus Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<p>Operational metrics from the Dokploy API.</p>
</body>
</html>
EOF
            fi
        } | nc -l -p "$HTTP_PORT" -q 1 2>/dev/null
    done
}

# ============================================================================
# MAIN EXECUTION
# ============================================================================

# Main entry point - routes to appropriate output mode
main() {
    parse_args "$@"

    if [ "$HTTP_MODE" = true ]; then
        # Run HTTP server (blocks until killed)
        run_http_server
    elif [ -n "$OUTPUT_FILE" ]; then
        # Textfile collector mode: write atomically using temp file
        local output_dir
        output_dir="$(dirname "$OUTPUT_FILE")"
        mkdir -p "$output_dir"

        # Create temp file in SAME directory for atomic rename (same filesystem)
        local temp_file
        temp_file=$(mktemp "${output_dir}/.dokploy_metrics.XXXXXX")

        # Generate metrics to temp file
        if ! generate_metrics > "$temp_file" 2>/dev/null; then
            rm -f "$temp_file"
            echo "ERROR: Failed to generate metrics" >&2
            exit 1
        fi

        # Validate: file must exist, have content
        local file_lines
        file_lines=$(wc -l < "$temp_file" 2>/dev/null || echo 0)

        if [ "$file_lines" -lt 10 ]; then
            rm -f "$temp_file"
            echo "ERROR: Metrics file too small ($file_lines lines), keeping previous" >&2
            exit 1
        fi

        # Set permissions before move
        chmod 644 "$temp_file"

        # Atomic rename - no gap where file is missing
        mv -f "$temp_file" "$OUTPUT_FILE"

        echo "Metrics written to $OUTPUT_FILE ($file_lines lines)" >&2
    else
        # Default: output to stdout
        generate_metrics
    fi
}

# Execute main function with all script arguments
main "$@"
