#!/usr/bin/env bash
# Alert script for tunnel failures
# Sends notifications when tunnels fail

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TUNNELS_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"

# Configuration
ALERT_EMAIL="${ALERT_EMAIL:-}"
ALERT_WEBHOOK="${ALERT_WEBHOOK:-}"
LOG_FILE="${LOG_FILE:-/var/log/cloudflared-alerts.log}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# Usage
if [ $# -lt 2 ]; then
    echo "Usage: $0 <tunnel-name> <failure-type>"
    echo ""
    echo "Failure types: service_down, connectivity_failed, dns_failed"
    exit 1
fi

TUNNEL_NAME="$1"
FAILURE_TYPE="$2"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

declare -A TUNNEL_DOMAINS=(
    ["ml110"]="ml110-01.d-bis.org"
    ["r630-01"]="r630-01.d-bis.org"
    ["r630-02"]="r630-02.d-bis.org"
)

DOMAIN="${TUNNEL_DOMAINS[$TUNNEL_NAME]:-unknown}"

# Create alert message
create_alert_message() {
    local subject="Cloudflare Tunnel Alert: $TUNNEL_NAME"
    local body="
ALERT: Cloudflare Tunnel Failure

Tunnel: $TUNNEL_NAME
Domain: $DOMAIN
Failure Type: $FAILURE_TYPE
Timestamp: $TIMESTAMP

Please check the tunnel status and logs.

To check status:
  systemctl status cloudflared-${TUNNEL_NAME}

To view logs:
  journalctl -u cloudflared-${TUNNEL_NAME} -f
"
    echo "$body"
}

# Send email alert
send_email() {
    if [ -z "$ALERT_EMAIL" ]; then
        return 0
    fi
    
    local subject="Cloudflare Tunnel Alert: $TUNNEL_NAME"
    local body=$(create_alert_message)
    
    if command -v mail &> /dev/null; then
        echo "$body" | mail -s "$subject" "$ALERT_EMAIL" 2>/dev/null || true
        log_info "Email alert sent to $ALERT_EMAIL"
    elif command -v sendmail &> /dev/null; then
        {
            echo "To: $ALERT_EMAIL"
            echo "Subject: $subject"
            echo ""
            echo "$body"
        } | sendmail "$ALERT_EMAIL" 2>/dev/null || true
        log_info "Email alert sent to $ALERT_EMAIL"
    else
        log_warn "Email not configured (mail/sendmail not found)"
    fi
}

# Send webhook alert
send_webhook() {
    if [ -z "$ALERT_WEBHOOK" ]; then
        return 0
    fi
    
    local payload=$(cat <<EOF
{
    "text": "Cloudflare Tunnel Alert",
    "attachments": [
        {
            "color": "danger",
            "fields": [
                {
                    "title": "Tunnel",
                    "value": "$TUNNEL_NAME",
                    "short": true
                },
                {
                    "title": "Domain",
                    "value": "$DOMAIN",
                    "short": true
                },
                {
                    "title": "Failure Type",
                    "value": "$FAILURE_TYPE",
                    "short": true
                },
                {
                    "title": "Timestamp",
                    "value": "$TIMESTAMP",
                    "short": true
                }
            ]
        }
    ]
}
EOF
)
    
    if command -v curl &> /dev/null; then
        curl -s -X POST -H "Content-Type: application/json" \
            -d "$payload" "$ALERT_WEBHOOK" > /dev/null 2>&1 || true
        log_info "Webhook alert sent"
    else
        log_warn "Webhook not sent (curl not found)"
    fi
}

# Log alert
log_alert() {
    local message="[$TIMESTAMP] ALERT: Tunnel $TUNNEL_NAME - $FAILURE_TYPE"
    echo "$message" >> "$LOG_FILE"
    log_error "$message"
}

# Main
main() {
    log_alert
    
    # Send alerts
    send_email
    send_webhook
    
    # Also log to syslog if available
    if command -v logger &> /dev/null; then
        logger -t cloudflared-alert "Tunnel $TUNNEL_NAME failure: $FAILURE_TYPE"
    fi
}

main

