<?php

namespace Modules\WhatsApp\Entities\Services;

/**
 * Single source of truth for the automated-notification events. Settings UI,
 * the settings controller and the default-template seeder all iterate this, so
 * adding a new trigger is a one-line change here + a template body + the place
 * that calls WaNotifier.
 *
 * Per event:
 *   toggle   => wa_settings key for the on/off switch  (default '1' unless noted)
 *   template => wa_settings key holding the chosen template name
 *   default  => default WaTemplate name
 *   group    => UI grouping
 *   label    => human label
 *   wired    => true if a trigger actually fires it today (vs. a ready template
 *               awaiting a trigger you can enable on request)
 */
class WaEvents
{
    public static function all(): array
    {
        return [
            // ---- Sales / POS ----
            'invoice' => [
                'toggle' => 'notify_invoice', 'template' => 'invoice_template',
                'default' => 'invoice_created', 'group' => 'Sales', 'label' => 'Invoice on sale finalised',
                'default_on' => true, 'wired' => true,
            ],
            'payment' => [
                'toggle' => 'notify_payment', 'template' => 'payment_template',
                'default' => 'payment_received', 'group' => 'Sales', 'label' => 'Payment received',
                'default_on' => true, 'wired' => true,
            ],
            'sale_return' => [
                'toggle' => 'notify_sale_return', 'template' => 'sale_return_template',
                'default' => 'sale_return', 'group' => 'Sales', 'label' => 'Sale return / refund',
                'default_on' => true, 'wired' => true,
            ],
            'quotation' => [
                'toggle' => 'notify_quotation', 'template' => 'quotation_template',
                'default' => 'quotation_sent', 'group' => 'Sales', 'label' => 'Quotation created',
                'default_on' => true, 'wired' => true,
            ],
            'payment_due' => [
                'toggle' => 'notify_payment_due', 'template' => 'payment_due_template',
                'default' => 'payment_due_reminder', 'group' => 'Sales', 'label' => 'Payment-due reminder (overdue invoices)',
                'default_on' => false, 'wired' => true,
            ],
            'shipment' => [
                'toggle' => 'notify_shipment', 'template' => 'shipment_template',
                'default' => 'shipment_status', 'group' => 'Sales', 'label' => 'Shipment / delivery status changed',
                'default_on' => true, 'wired' => true,
            ],

            // ---- Purchasing (supplier-facing) ----
            'po_sent' => [
                'toggle' => 'notify_po_sent', 'template' => 'po_sent_template',
                'default' => 'po_sent', 'group' => 'Purchasing', 'label' => 'Purchase order sent to supplier',
                'default_on' => false, 'wired' => true,
            ],

            // ---- Lab ----
            'lab_results' => [
                'toggle' => 'notify_lab_results', 'template' => 'lab_results_template',
                'default' => 'lab_results_ready', 'group' => 'Lab', 'label' => 'Lab results ready',
                'default_on' => true, 'wired' => true,
            ],

            // ---- VetClinic appointments ----
            'appointment_booked' => [
                'toggle' => 'notify_appointment_booked', 'template' => 'appointment_booked_template',
                'default' => 'appointment_booked', 'group' => 'Appointments', 'label' => 'Appointment booked',
                'default_on' => true, 'wired' => true,
            ],
            'appointment_reminder' => [
                'toggle' => 'notify_appointment_reminder', 'template' => 'appointment_reminder_template',
                'default' => 'appointment_reminder', 'group' => 'Appointments', 'label' => 'Appointment reminder (day before)',
                'default_on' => true, 'wired' => true,
            ],
            'appointment_cancelled' => [
                'toggle' => 'notify_appointment_cancelled', 'template' => 'appointment_cancelled_template',
                'default' => 'appointment_cancelled', 'group' => 'Appointments', 'label' => 'Appointment cancelled',
                'default_on' => true, 'wired' => true,
            ],
            'appointment_rescheduled' => [
                'toggle' => 'notify_appointment_rescheduled', 'template' => 'appointment_rescheduled_template',
                'default' => 'appointment_rescheduled', 'group' => 'Appointments', 'label' => 'Appointment rescheduled',
                'default_on' => true, 'wired' => true,
            ],
            'appointment_followup' => [
                'toggle' => 'notify_appointment_followup', 'template' => 'appointment_followup_template',
                'default' => 'appointment_followup', 'group' => 'Appointments', 'label' => 'Post-consultation follow-up',
                'default_on' => false, 'wired' => true,
            ],
            'vaccination_due' => [
                'toggle' => 'notify_vaccination_due', 'template' => 'vaccination_due_template',
                'default' => 'vaccination_due', 'group' => 'Appointments', 'label' => 'Vaccination due reminder',
                'default_on' => true, 'wired' => true,
            ],

            // ---- Onboarding / welcome ----
            'contact_welcome' => [
                'toggle' => 'notify_contact_welcome', 'template' => 'contact_welcome_template',
                'default' => 'contact_welcome', 'group' => 'Onboarding', 'label' => 'Welcome new customer',
                // On by default — bulk imports are safe because the queue paces sends
                // (per-channel jitter) and non-WhatsApp numbers are skipped after the
                // first check. Dedupe guarantees one welcome per contact.
                'default_on' => true, 'wired' => true,
            ],
            'pet_owner_welcome' => [
                'toggle' => 'notify_pet_owner_welcome', 'template' => 'pet_owner_welcome_template',
                'default' => 'pet_owner_welcome', 'group' => 'Onboarding', 'label' => 'Welcome new pet owner',
                'default_on' => true, 'wired' => true,
            ],
            'pet_welcome' => [
                'toggle' => 'notify_pet_welcome', 'template' => 'pet_welcome_template',
                'default' => 'pet_welcome', 'group' => 'Onboarding', 'label' => 'Welcome new pet',
                'default_on' => true, 'wired' => true,
            ],

            // ---- Engagement ----
            'pet_birthday' => [
                'toggle' => 'notify_pet_birthday', 'template' => 'pet_birthday_template',
                'default' => 'pet_birthday', 'group' => 'Engagement', 'label' => 'Pet birthday greeting',
                'default_on' => false, 'wired' => true,
            ],
        ];
    }

    /** Settings defaults contributed by every event (toggle + chosen template). */
    public static function settingDefaults(): array
    {
        $defaults = [];
        foreach (self::all() as $event) {
            $defaults[$event['toggle']]   = !empty($event['default_on']) ? '1' : '0';
            $defaults[$event['template']] = $event['default'];
        }
        return $defaults;
    }
}
