"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCommunications = exports.sendEmail = exports.sendSMS = exports.sendWhatsApp = void 0;
const database_1 = require("../config/database");
const sendWhatsApp = async (leadId, recipient, message, sentBy) => {
    // WhatsApp Business API stub
    // In production, integrate with WhatsApp Business API or third-party service
    const whatsappLink = `https://wa.me/${recipient.replace(/[^0-9]/g, '')}?text=${encodeURIComponent(message)}`;
    const result = await (0, database_1.query)(`INSERT INTO communications (lead_id, communication_type, direction, recipient, message, sent_by, metadata)
     VALUES ($1, $2, $3, $4, $5, $6, $7)
     RETURNING *`, [leadId, 'whatsapp', 'outbound', recipient, message, sentBy, JSON.stringify({ link: whatsappLink })]);
    // Log activity
    await (0, database_1.query)(`INSERT INTO activities (lead_id, activity_type, title, description, performed_by)
     VALUES ($1, $2, $3, $4, $5)`, [leadId, 'WHATSAPP_SENT', 'WhatsApp Message', `Message sent to ${recipient}`, sentBy]);
    return { ...result.rows[0], whatsappLink };
};
exports.sendWhatsApp = sendWhatsApp;
const sendSMS = async (leadId, recipient, message, sentBy) => {
    // SMS Gateway stub
    // In production, integrate with Twilio, AWS SNS, or other SMS provider
    const mockExternalId = `SMS-${Date.now()}`;
    const result = await (0, database_1.query)(`INSERT INTO communications (lead_id, communication_type, direction, recipient, message, sent_by, external_id, status)
     VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
     RETURNING *`, [leadId, 'sms', 'outbound', recipient, message, sentBy, mockExternalId, 'sent']);
    // Log activity
    await (0, database_1.query)(`INSERT INTO activities (lead_id, activity_type, title, description, performed_by)
     VALUES ($1, $2, $3, $4, $5)`, [leadId, 'SMS_SENT', 'SMS Sent', `SMS sent to ${recipient}`, sentBy]);
    return result.rows[0];
};
exports.sendSMS = sendSMS;
const sendEmail = async (leadId, recipient, subject, message, sentBy) => {
    // Email SMTP stub
    // In production, integrate with SendGrid, AWS SES, or SMTP server
    const mockExternalId = `EMAIL-${Date.now()}`;
    const result = await (0, database_1.query)(`INSERT INTO communications (lead_id, communication_type, direction, recipient, subject, message, sent_by, external_id, status)
     VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
     RETURNING *`, [leadId, 'email', 'outbound', recipient, subject, message, sentBy, mockExternalId, 'sent']);
    // Log activity
    await (0, database_1.query)(`INSERT INTO activities (lead_id, activity_type, title, description, performed_by)
     VALUES ($1, $2, $3, $4, $5)`, [leadId, 'EMAIL_SENT', 'Email Sent', `Email sent to ${recipient}: ${subject}`, sentBy]);
    return result.rows[0];
};
exports.sendEmail = sendEmail;
const getCommunications = async (leadId) => {
    const result = await (0, database_1.query)(`SELECT c.*, u.name as sent_by_name
     FROM communications c
     LEFT JOIN users u ON c.sent_by = u.id
     WHERE c.lead_id = $1
     ORDER BY c.sent_at DESC`, [leadId]);
    return result.rows;
};
exports.getCommunications = getCommunications;
