'use strict'

const WhatsappCore = require('./whatsappService')

const BASE_URL = process.env.APP_URL || 'http://localhost:4001'

/**
 * Todas as notificações WhatsApp relacionadas ao Clube de Benefícios.
 * Cada método aceita tenantId opcional para segregar whatsapp_logs por tenant.
 */
const ClubNotificationService = {

  async notifyWelcome(userCpf, userName, userPhone, clubName, planName, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_welcome', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, planName, link: `${BASE_URL}/app/carteirinha/${userCpf}` },
    }).catch(console.error)
  },

  async notifyPaymentReminder(userCpf, userName, userPhone, clubName, dueDate, pixLink, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_payment_reminder', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, dueDate, link: pixLink || `${BASE_URL}/app/minha-area` },
    }).catch(console.error)
  },

  async notifyPaymentConfirmed(userCpf, userName, userPhone, clubName, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_payment_confirmed', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, link: `${BASE_URL}/app/carteirinha/${userCpf}` },
    }).catch(console.error)
  },

  async notifyPaymentOverdue(userCpf, userName, userPhone, clubName, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_payment_overdue', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, link: `${BASE_URL}/app/minha-area` },
    }).catch(console.error)
  },

  async notifySuspended(userCpf, userName, userPhone, clubName, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_suspended', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, link: `${BASE_URL}/app/minha-area` },
    }).catch(console.error)
  },

  async notifyCanceled(userCpf, userName, userPhone, clubName, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_canceled', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName },
    }).catch(console.error)
  },

  async notifyNewPartner(userCpf, userName, userPhone, clubName, partnerName, specialty, address, tenantId) {
    if (!userPhone || userPhone.length < 10) return
    await WhatsappCore.sendNotification({
      userId: null, phone: userPhone, type: 'club_medico_partner_new', userCpf, tenantId,
      variables: { nome: userName || 'Cliente', clubName, partnerName, specialty: specialty || '', address: address || '' },
    }).catch(console.error)
  },
}

module.exports = ClubNotificationService
