import { query } from '../config/database';

export const sendWhatsApp = async (
    leadId: number,
    recipient: string,
    message: string,
    sentBy: number
) => {
    // 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 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 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 };
};

export const sendSMS = async (
    leadId: number,
    recipient: string,
    message: string,
    sentBy: number
) => {
    // SMS Gateway stub
    // In production, integrate with Twilio, AWS SNS, or other SMS provider
    const mockExternalId = `SMS-${Date.now()}`;

    const result = await 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 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];
};

export const sendEmail = async (
    leadId: number,
    recipient: string,
    subject: string,
    message: string,
    sentBy: number
) => {
    // Email SMTP stub
    // In production, integrate with SendGrid, AWS SES, or SMTP server
    const mockExternalId = `EMAIL-${Date.now()}`;

    const result = await 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 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];
};

export const getCommunications = async (leadId: number) => {
    const result = await 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;
};
