#!/usr/bin/env bash
# Daily reminder: secret rotation pending (Linear KAR-12).
# Stops nagging once /root/aria/state/secret-rotation-done exists.
# Disable: touch /root/aria/state/secret-rotation-done
set -euo pipefail

DONE_FLAG="/root/aria/state/secret-rotation-done"
ENV_FILE="/root/.claude/channels/telegram/.env"
CHAT_ID="1164395546"
PARENT_URL="https://linear.app/karia/issue/KAR-12"
START_DATE="2026-05-06"

[[ -f "$DONE_FLAG" ]] && exit 0
[[ -f "$ENV_FILE" ]] || { echo "telegram .env missing" >&2; exit 1; }

# shellcheck disable=SC1090
source "$ENV_FILE"
[[ -n "${TELEGRAM_BOT_TOKEN:-}" ]] || { echo "TELEGRAM_BOT_TOKEN unset" >&2; exit 1; }

day=$(( ( $(date -u +%s) - $(date -u -d "$START_DATE" +%s) ) / 86400 ))

read -r -d '' TEXT <<EOF || true
🔐 Secret-Rotation Erinnerung — Tag ${day}

Offene Tasks: $PARENT_URL (KAR-12 Parent + KAR-13..19 Sub-Issues)

Sobald alle 7 rotiert sind:
  touch $DONE_FLAG
→ stoppt diese Erinnerung.
EOF

curl -fsS --max-time 15 \
  -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
  --data-urlencode "chat_id=${CHAT_ID}" \
  --data-urlencode "text=${TEXT}" \
  > /dev/null
