"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WhatsappService = void 0;
const logger_1 = require("../config/logger");
class WhatsappService {
    static phoneNumberId = process.env.META_WHATSAPP_PHONE_NUMBER_ID || '105574302636254';
    static accessToken = process.env.META_WHATSAPP_ACCESS_TOKEN || 'EAA...';
    /**
     * Send Template Message via Meta WhatsApp Cloud API
     */
    static async sendTemplateMessage(recipientPhone, templateName, parameters) {
        const formattedPhone = recipientPhone.replace(/\D/g, ''); // Ensure digits only (e.g. 919876543210)
        const isMock = this.accessToken === 'EAA...' || !process.env.META_WHATSAPP_ACCESS_TOKEN;
        if (isMock) {
            logger_1.logger.info(`[WhatsAppService] [MOCK SEND] Sent template '${templateName}' to ${formattedPhone} with parameters:`, parameters);
            return true;
        }
        try {
            const response = await fetch(`https://graph.facebook.com/v17.0/${this.phoneNumberId}/messages`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${this.accessToken}`,
                },
                body: JSON.stringify({
                    messaging_product: 'whatsapp',
                    to: formattedPhone,
                    type: 'template',
                    template: {
                        name: templateName,
                        language: { code: 'en_US' },
                        components: [
                            {
                                type: 'body',
                                parameters: parameters,
                            },
                        ],
                    },
                }),
            });
            const data = await response.json();
            if (!response.ok) {
                throw new Error(data.error?.message || 'Meta API request failed');
            }
            logger_1.logger.info(`[WhatsAppService] Message successfully dispatched. ID: ${data.messages?.[0]?.id}`);
            return true;
        }
        catch (err) {
            logger_1.logger.error(`[WhatsAppService] Meta API Error: ${err.message}`);
            return false;
        }
    }
    // Predefined triggers
    static async sendOrderConfirmation(phone, customerName, orderNumber) {
        return this.sendTemplateMessage(phone, 'order_confirmation', [
            { type: 'text', text: customerName },
            { type: 'text', text: orderNumber },
        ]);
    }
    static async sendShippingUpdate(phone, orderNumber, awb, courier) {
        return this.sendTemplateMessage(phone, 'shipping_update', [
            { type: 'text', text: orderNumber },
            { type: 'text', text: awb },
            { type: 'text', text: courier },
        ]);
    }
    static async sendOtp(phone, code) {
        return this.sendTemplateMessage(phone, 'otp_notification', [
            { type: 'text', text: code },
        ]);
    }
}
exports.WhatsappService = WhatsappService;
//# sourceMappingURL=whatsapp.service.js.map