// utils/ntfySender.js — ntfy HTTP POST 래퍼
const NTFY_BASE_URL = process.env.NTFY_BASE_URL || 'http://ntfy:80';
const NTFY_PUBLISH_TOKEN = process.env.NTFY_PUBLISH_TOKEN;
const TKFB_BASE_URL = process.env.TKFB_BASE_URL || 'https://tkfb.technicalkorea.net';

async function sendNtfy(userId, { title, body, url }) {
  if (!NTFY_PUBLISH_TOKEN) return;

  const topic = `tkfactory-user-${userId}`;
  const headers = {
    'Authorization': `Bearer ${NTFY_PUBLISH_TOKEN}`,
    'Title': title || '',
    'Tags': 'bell',
  };
  if (url) {
    headers['Click'] = url.startsWith('http') ? url : `${TKFB_BASE_URL}${url}`;
  }

  try {
    const resp = await fetch(`${NTFY_BASE_URL}/${topic}`, {
      method: 'POST',
      headers,
      body: body || '',
    });
    if (!resp.ok) console.warn(`[ntfy] ${userId} 발송 실패: ${resp.status}`);
  } catch (e) {
    console.error(`[ntfy] ${userId} 발송 오류:`, e.message);
  }
}

module.exports = { sendNtfy };
