<?php
// includes/functions/emails/registry.php
if (!defined('ABSPATH')) exit;

if (!isset($GLOBALS['AFB_EMAILS'])) $GLOBALS['AFB_EMAILS'] = [];

/**
 * Register an email “type”.
 * $cfg keys:
 *  - enabled_option (string option key; truthy enables)
 *  - from_name_option, from_email_option, reply_to_option, cc_option, bcc_option (option keys)
 *  - to (array|string|callable($vars))
 *  - subject (string with {{tokens}} or callable($vars):string)
 *  - html (string with {{tokens}} or callable($vars):string)
 *  - text (string with {{tokens}} or callable($vars):string) // currently unused, kept for future
 *  - attachments (array|callable($vars):array)
 */
if (!function_exists('afb_register_email')) {
    function afb_register_email($slug, array $cfg) {
        $GLOBALS['AFB_EMAILS'][$slug] = $cfg;
    }
}

/**
 * Send an email by type/slug.
 * $overrides: to, from_name, from_email, reply_to, cc, bcc, headers, attachments, subject, html
 */
if (!function_exists('afb_email_send')) {
    function afb_email_send($slug, array $vars = [], array $overrides = []) {
        $reg = $GLOBALS['AFB_EMAILS'][$slug] ?? null;
        if (!$reg) return false;

        // Enabled?
        $enabled = true;
        if (!empty($reg['enabled_option'])) {
            $enabled = (string) get_option($reg['enabled_option'], '') === '1';
        }
        if (isset($overrides['enabled'])) $enabled = (bool)$overrides['enabled'];
        if (!$enabled) return false;

        // Recipient(s)
        $to = $overrides['to'] ?? (
            is_callable($reg['to']) ? call_user_func($reg['to'], $vars) : ($reg['to'] ?? [])
        );
        $to = afb_email_normalize_recipients($to);
        if (!$to) return false;

        // Subject
        if (isset($overrides['subject'])) {
            $subject = $overrides['subject'];
        } else {
            $subject = is_callable($reg['subject']) ? call_user_func($reg['subject'], $vars) : (string)($reg['subject'] ?? '');
            $subject = afb_email_token_replace($subject, $vars);
        }

        // HTML
        if (isset($overrides['html'])) {
            $html = $overrides['html'];
        } else {
            $html = is_callable($reg['html']) ? call_user_func($reg['html'], $vars) : (string)($reg['html'] ?? '');
            $html = afb_email_token_replace($html, $vars);
        }

        // From / Reply / CC / BCC (option-backed defaults)
        $from_name  = $overrides['from_name']  ?? ($reg['from_name_option']  ? afb_email_opt($reg['from_name_option'], afb_email_default_from_name()) : afb_email_default_from_name());
        $from_email = $overrides['from_email'] ?? ($reg['from_email_option'] ? afb_email_opt($reg['from_email_option'], afb_email_default_from_email()) : afb_email_default_from_email());
        $reply_to   = $overrides['reply_to']   ?? ($reg['reply_to_option']   ? afb_email_opt($reg['reply_to_option'], '') : '');
        $cc         = $overrides['cc']         ?? ($reg['cc_option']         ? afb_email_opt($reg['cc_option'], '') : '');
        $bcc        = $overrides['bcc']        ?? ($reg['bcc_option']        ? afb_email_opt($reg['bcc_option'], '') : '');

        // Attachments
        $attachments = $overrides['attachments'] ?? (
            (is_callable($reg['attachments']) ? (array)call_user_func($reg['attachments'], $vars) : (array)($reg['attachments'] ?? []))
        );

        return afb_mailer_send($to, $subject, $html, '', [
            'from_name'  => $from_name,
            'from_email' => $from_email,
            'reply_to'   => $reply_to,
            'cc'         => $cc,
            'bcc'        => $bcc,
            'attachments'=> $attachments,
            'headers'    => $overrides['headers'] ?? [],
        ]);
    }
}
