/**
 * Zalo OA ZNS template configuration.
 *
 * Maps internal notification template keys to ZNS template IDs registered in
 * the Zalo OA Manager console. Template IDs must be configured via environment
 * variables (ZALO_ZNS_TEMPLATE_*).
 *
 * Template parameters must match the ZNS template definitions exactly —
 * see the Zalo OA Manager for the approved parameter names.
 */

export interface ZaloZnsTemplateConfig {
  /** ZNS template ID (registered in Zalo OA Manager) */
  templateId: string;
  /** Map our internal template data keys to ZNS parameter names */
  mapParams: (data: Record<string, unknown>) => Record<string, string>;
}

/**
 * Returns the ZNS template configurations, reading template IDs from environment.
 * Returns only templates where the env var is configured.
 */
export function getZaloZnsTemplates(): Record<string, ZaloZnsTemplateConfig> {
  const templates: Record<string, ZaloZnsTemplateConfig> = {};

  // Inquiry received — notify property owner/agent about a new inquiry
  const inquiryTplId = process.env['ZALO_ZNS_TEMPLATE_INQUIRY'] ?? '';
  if (inquiryTplId) {
    templates['inquiry.received'] = {
      templateId: inquiryTplId,
      mapParams: (data) => ({
        customer_name: String(data['senderName'] ?? ''),
        property_name: String(data['listingTitle'] ?? ''),
        message: String(data['message'] ?? ''),
      }),
    };
  }

  // Payment confirmed — notify buyer about successful payment
  const paymentTplId = process.env['ZALO_ZNS_TEMPLATE_PAYMENT'] ?? '';
  if (paymentTplId) {
    templates['payment.confirmed'] = {
      templateId: paymentTplId,
      mapParams: (data) => ({
        payment_id: String(data['paymentId'] ?? ''),
        amount: String(data['amountVND'] ?? ''),
        payment_method: String(data['provider'] ?? ''),
      }),
    };
  }

  // Listing approved — notify owner that listing is live
  const listingApprovedTplId = process.env['ZALO_ZNS_TEMPLATE_LISTING_APPROVED'] ?? '';
  if (listingApprovedTplId) {
    templates['listing.approved'] = {
      templateId: listingApprovedTplId,
      mapParams: (data) => ({
        listing_title: String(data['listingTitle'] ?? ''),
      }),
    };
  }

  // Listing rejected — notify owner that listing was rejected
  const listingRejectedTplId = process.env['ZALO_ZNS_TEMPLATE_LISTING_REJECTED'] ?? '';
  if (listingRejectedTplId) {
    templates['listing.rejected'] = {
      templateId: listingRejectedTplId,
      mapParams: (data) => ({
        listing_title: String(data['listingTitle'] ?? ''),
        reason: String(data['reason'] ?? ''),
      }),
    };
  }

  // Listing sold — notify owner that listing is sold
  const listingSoldTplId = process.env['ZALO_ZNS_TEMPLATE_LISTING_SOLD'] ?? '';
  if (listingSoldTplId) {
    templates['listing.sold'] = {
      templateId: listingSoldTplId,
      mapParams: (data) => ({
        listing_title: String(data['listingTitle'] ?? ''),
      }),
    };
  }

  // R8.2 — Inquiry reply: notify the original inquirer that the agent/owner replied
  const inquiryReplyTplId = process.env['ZALO_ZNS_TEMPLATE_INQUIRY_REPLY'] ?? '';
  if (inquiryReplyTplId) {
    templates['inquiry.reply'] = {
      templateId: inquiryReplyTplId,
      mapParams: (data) => ({
        agent_name: String(data['agentName'] ?? ''),
        property_name: String(data['listingTitle'] ?? ''),
        message: String(data['message'] ?? ''),
      }),
    };
  }

  // R8.2 — Listing price drop: notify watchers/saved-search subscribers
  const priceDropTplId = process.env['ZALO_ZNS_TEMPLATE_PRICE_DROP'] ?? '';
  if (priceDropTplId) {
    templates['listing.price_drop'] = {
      templateId: priceDropTplId,
      mapParams: (data) => ({
        listing_title: String(data['listingTitle'] ?? ''),
        old_price: String(data['oldPriceVND'] ?? ''),
        new_price: String(data['newPriceVND'] ?? ''),
      }),
    };
  }

  // R8.2 — Subscription renewal reminder (pre-renewal heads-up)
  const subscriptionRenewalTplId = process.env['ZALO_ZNS_TEMPLATE_SUBSCRIPTION_RENEWAL'] ?? '';
  if (subscriptionRenewalTplId) {
    templates['subscription.renewal'] = {
      templateId: subscriptionRenewalTplId,
      mapParams: (data) => ({
        plan_tier: String(data['planTier'] ?? ''),
        renewal_date: String(data['renewalDate'] ?? ''),
        amount: String(data['amountVND'] ?? ''),
      }),
    };
  }

  // R8.2 — Subscription renewed (post-renewal confirmation; matches SubscriptionRenewedListener)
  const subscriptionRenewedTplId = process.env['ZALO_ZNS_TEMPLATE_SUBSCRIPTION_RENEWED'] ?? '';
  if (subscriptionRenewedTplId) {
    templates['subscription.renewed'] = {
      templateId: subscriptionRenewedTplId,
      mapParams: (data) => ({
        plan_tier: String(data['planTier'] ?? ''),
        period_end: String(data['periodEnd'] ?? ''),
      }),
    };
  }

  return templates;
}
