frappe.ui.form.on('Lead', {
    refresh(frm) {
        // Email Campaign Button
        if (!frm.doc.__islocal && frm.doc.email_id) {
            frm.add_custom_button(__('Add to Email Campaign'), () => {
                open_email_campaign_dialog(frm);
            }, __('Action'));

            // Email Group Button
            frm.add_custom_button(__('Add to Email Newsletter'), () => {
                open_email_group_dialog(frm);
            }, __('Action'));
        } 
    }
});

function open_email_campaign_dialog(frm) {
    frappe.prompt([
        {
            fieldname: 'campaign_name',
            label: 'Campaign',
            fieldtype: 'Link',
            options: 'Campaign',
            reqd: 1
        },
        {
            fieldname: 'start_date',
            label: 'Start Date',
            fieldtype: 'Date',
            default: frappe.datetime.get_today(),
            reqd: 1
        }
    ],
    (values) => {
        frappe.call({
            method: 'rangeldigital.utilities.lead.lead_api.add_lead_to_campaign',
            args: {
                lead_name: frm.doc.name,
                campaign_name: values.campaign_name,
                start_date: values.start_date
            },
            freeze: true,
            freeze_message: __('Creating Email Campaign...'),
            callback: (r) => {
                if (r.message && r.message.status === 'success') {
                    frappe.show_alert({
                        message: __('Email Campaign created successfully'),
                        indicator: 'green'
                    });
                } else {
                    frappe.msgprint(__('Something went wrong. Please try again.'));
                }
            },
            error: (err) => {
                if (err && err.message) {
                    frappe.msgprint(err.message);
                } else {
                    frappe.msgprint(__('Unexpected error. Check the console.'));
                    console.error(err);
                }
            }
        });
    },
    __('New Email Campaign'),
    __('Create')
    );
}

function open_email_group_dialog(frm) {
    frappe.prompt([
        {
            fieldname: 'email_group',
            label: 'Email Group',
            fieldtype: 'Link',
            options: 'Email Group',
            reqd: 1
        }
    ],
    (values) => {
        frappe.call({
            method: 'rangeldigital.utilities.lead.lead_api.add_lead_to_email_group',
            args: {
                lead_name: frm.doc.name,
                email_group: values.email_group
            },
            freeze: true,
            freeze_message: __('Adding to Email Group...'),
            callback: (r) => {
                if (r.message && r.message.status === 'success') {
                    frappe.show_alert({
                        message: __('Lead added to Email Group successfully'),
                        indicator: 'green'
                    });
                } else {
                    frappe.msgprint(__('Something went wrong. Please try again.'));
                }
            },
            error: (err) => {
                frappe.msgprint(__('Unexpected error. Check the console.'));
                console.error(err);
            }
        });
    },
    __('Add to Email Group'),
    __('Add')
    );
}
