import { apiURL, token } from '@Services/Api';
import { page } from '@Services/Page';
import axios from 'axios';

/**
 * Servicio para enviar mensajes por WhatsApp
 */
const whatsappService = {
    /**
     * Enviar factura por WhatsApp
     * @param {Object} data - Datos de la factura
     * @returns {Promise}
     */
    async sendInvoice({ phone_number, invoice_number, pdf_url, xml_url = null, customer_name }) {
        try {
            const { data } = await axios.post(
                apiURL('whatsapp/send-invoice'),
                {
                    phone_number,
                    invoice_number,
                    pdf_url,
                    xml_url,
                    customer_name
                },
                {
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        'Authorization': `Bearer ${token.value}`
                    }
                }
            );
            return data;
        } catch (error) {
            const errorData = error.response?.data?.data || error.response?.data;
            console.error('WhatsApp Error Detail:', errorData);
            throw errorData;
        }
    },

    /**
     * Enviar mensaje genérico por WhatsApp
     * @param {Object} data - Datos del mensaje
     * @returns {Promise}
     */
    async sendMessage({ phone_number, message }) {
        try {
            const { data } = await axios.post(
                apiURL('whatsapp/send-message'),
                {
                    phone_number,
                    message
                },
                {
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        'Authorization': `Bearer ${token.value}`
                    }
                }
            );
            return data;
        } catch (error) {
            const errorData = error.response?.data?.data || error.response?.data;
            console.error('WhatsApp Error Detail:', errorData);
            throw errorData;
        }
    }
};

export default whatsappService;