#!/usr/bin/env python3
"""
Shared ntfy.sh push-alert sender.

Single place for the topic + POST logic so alert call-sites (verify_scan.py,
fetch_prices.py, generate_retrospective.py) can't drift out of sync on how a
push notification actually gets sent -- check_positions.sh sends its own
batched alert directly in bash and is intentionally not routed through here.
"""
import urllib.request

NTFY_TOPIC = 'swingtrader-599966045f81'


def send_alert(message, title='SwingTrader Alert', priority='high', tags='warning'):
    """POST a push notification to the shared ntfy.sh topic. Returns True on success."""
    req = urllib.request.Request(
        f'https://ntfy.sh/{NTFY_TOPIC}',
        data=message.encode(),
        headers={'Title': title, 'Priority': priority, 'Tags': tags},
        method='POST',
    )
    try:
        with urllib.request.urlopen(req, timeout=10) as resp:
            return resp.status == 200
    except Exception:
        return False
