<?php
/**
 * 247ChatMate — Client Mail Delivery Options
 * ------------------------------------------
 * Lightweight end-user mail routing controls for client sites.
 *
 * Scope for CLIENTMAILDELIVERYOPTIONS1:
 * - Keep the default route as the site's WordPress mail stack, including SMTP plugins.
 * - Allow advanced clients to store their own SMTP route locally on their own site.
 * - Keep credentials masked in admin; blank password field preserves the saved password.
 * - Store failed sends through the existing fallback queue rather than losing leads.
 * - Do not expose agency proxy/provider internals on normal client setup.
 */

defined('ABSPATH') || exit;

if (!defined('MATEWA_CLIENT_MAIL_DELIVERY_OPTION')) {
    define('MATEWA_CLIENT_MAIL_DELIVERY_OPTION', 'matewa_client_mail_delivery_options');
}

if (!defined('MATEWA_CLIENT_MAIL_DELIVERY_TEST_OPTION')) {
    define('MATEWA_CLIENT_MAIL_DELIVERY_TEST_OPTION', 'matewa_client_mail_delivery_last_test');
}

if (!function_exists('matewa_client_mail_delivery_default_options')) {
    function matewa_client_mail_delivery_default_options() {
        return array(
            'route'               => 'site_mail',
            'provider'            => 'other',
            'sender_name'         => '',
            'sender_email'        => '',
            'reply_to_email'      => '',
            'smtp_host'           => '',
            'smtp_port'           => '',
            'smtp_encryption'     => 'tls',
            'smtp_username'       => '',
            'smtp_password_enc'   => '',
            'relay_endpoint'      => '',
            'relay_token_enc'     => '',
            'relay_token_hash'    => '',
            'allow_site_fallback' => '1',
            'updated_at'          => '',
        );
    }
}

if (!function_exists('matewa_client_mail_delivery_provider_presets')) {
    function matewa_client_mail_delivery_provider_presets() {
        return array(
            'google_workspace' => array(
                'label'      => 'Google Workspace / Gmail',
                'host'       => 'smtp.gmail.com',
                'port'       => '587',
                'encryption' => 'tls',
                'note'       => 'Google is best handled by a WordPress SMTP plugin with OAuth, but SMTP/app-password routes can still be recorded here when the client uses them.',
            ),
            'microsoft_365' => array(
                'label'      => 'Microsoft 365 / Outlook',
                'host'       => 'smtp.office365.com',
                'port'       => '587',
                'encryption' => 'tls',
                'note'       => 'Use the client mailbox or authorised SMTP account. Modern Microsoft tenants may require authenticated SMTP to be enabled.',
            ),
            'hostinger' => array(
                'label'      => 'Hostinger email',
                'host'       => 'smtp.hostinger.com',
                'port'       => '465',
                'encryption' => 'ssl',
                'note'       => 'Use the full Hostinger mailbox address and mailbox password.',
            ),
            'other' => array(
                'label'      => 'Other SMTP provider',
                'host'       => '',
                'port'       => '587',
                'encryption' => 'tls',
                'note'       => 'Use the provider details supplied by the client or their IT/email provider.',
            ),
        );
    }
}

if (!function_exists('matewa_client_mail_delivery_allowed_routes')) {
    function matewa_client_mail_delivery_allowed_routes() {
        return array('site_mail', 'managed_relay', 'client_smtp');
    }
}

if (!function_exists('matewa_client_mail_delivery_normalise_route')) {
    function matewa_client_mail_delivery_normalise_route($route) {
        $route = sanitize_key((string) $route);
        return in_array($route, matewa_client_mail_delivery_allowed_routes(), true) ? $route : 'site_mail';
    }
}

if (!function_exists('matewa_client_mail_delivery_normalise_provider')) {
    function matewa_client_mail_delivery_normalise_provider($provider) {
        $provider = sanitize_key((string) $provider);
        $presets = matewa_client_mail_delivery_provider_presets();
        return isset($presets[$provider]) ? $provider : 'other';
    }
}

if (!function_exists('matewa_client_mail_delivery_normalise_encryption')) {
    function matewa_client_mail_delivery_normalise_encryption($encryption) {
        $encryption = sanitize_key((string) $encryption);
        return in_array($encryption, array('tls', 'ssl', 'none'), true) ? $encryption : 'tls';
    }
}

if (!function_exists('matewa_client_mail_delivery_secret_key')) {
    function matewa_client_mail_delivery_secret_key() {
        $seed = '';
        foreach (array('AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY') as $constant) {
            if (defined($constant)) {
                $seed .= constant($constant);
            }
        }

        $seed .= wp_salt('auth') . site_url('/');
        return hash('sha256', $seed, true);
    }
}

if (!function_exists('matewa_client_mail_delivery_encrypt_password')) {
    function matewa_client_mail_delivery_encrypt_password($plain) {
        $plain = (string) $plain;
        if ('' === $plain || !function_exists('openssl_encrypt') || !defined('OPENSSL_RAW_DATA')) {
            return '';
        }

        try {
            $iv_length = openssl_cipher_iv_length('aes-256-cbc');
            if (false === $iv_length || $iv_length < 1 || !function_exists('random_bytes')) {
                return '';
            }

            $iv = random_bytes($iv_length);
            $encrypted = openssl_encrypt($plain, 'aes-256-cbc', matewa_client_mail_delivery_secret_key(), OPENSSL_RAW_DATA, $iv);
            if (false === $encrypted) {
                return '';
            }

            return bin2hex($iv) . ':' . bin2hex($encrypted);
        } catch (Exception $exception) {
            return '';
        }
    }
}

if (!function_exists('matewa_client_mail_delivery_decrypt_password')) {
    function matewa_client_mail_delivery_decrypt_password($stored) {
        $stored = (string) $stored;
        if ('' === $stored || false === strpos($stored, ':') || !function_exists('openssl_decrypt') || !defined('OPENSSL_RAW_DATA')) {
            return '';
        }

        $parts = explode(':', $stored, 2);
        if (2 !== count($parts) || !ctype_xdigit($parts[0]) || !ctype_xdigit($parts[1])) {
            return '';
        }

        $iv = hex2bin($parts[0]);
        $encrypted = hex2bin($parts[1]);
        if (false === $iv || false === $encrypted) {
            return '';
        }

        $plain = openssl_decrypt($encrypted, 'aes-256-cbc', matewa_client_mail_delivery_secret_key(), OPENSSL_RAW_DATA, $iv);
        return false === $plain ? '' : (string) $plain;
    }
}

if (!function_exists('matewa_client_mail_delivery_get_options')) {
    function matewa_client_mail_delivery_get_options() {
        $stored = get_option(MATEWA_CLIENT_MAIL_DELIVERY_OPTION, array());
        $stored = is_array($stored) ? $stored : array();
        $options = array_merge(matewa_client_mail_delivery_default_options(), $stored);

        $options['route'] = matewa_client_mail_delivery_normalise_route($options['route'] ?? 'site_mail');
        $options['provider'] = matewa_client_mail_delivery_normalise_provider($options['provider'] ?? 'other');
        $options['smtp_encryption'] = matewa_client_mail_delivery_normalise_encryption($options['smtp_encryption'] ?? 'tls');
        $options['sender_email'] = sanitize_email((string) ($options['sender_email'] ?? ''));
        $options['reply_to_email'] = sanitize_email((string) ($options['reply_to_email'] ?? ''));
        $options['smtp_username'] = sanitize_text_field((string) ($options['smtp_username'] ?? ''));
        $options['smtp_host'] = sanitize_text_field((string) ($options['smtp_host'] ?? ''));
        $smtp_port_value = absint($options['smtp_port'] ?? 0);
        $options['smtp_port'] = 0 < $smtp_port_value ? (string) $smtp_port_value : '';
        $options['smtp_password_set'] = '' !== (string) ($options['smtp_password_enc'] ?? '');
        $options['relay_endpoint'] = esc_url_raw((string) ($options['relay_endpoint'] ?? ''));
        $options['relay_token_set'] = '' !== (string) ($options['relay_token_enc'] ?? '');
        $options['allow_site_fallback'] = !empty($options['allow_site_fallback']) ? '1' : '0';

        return $options;
    }
}

if (!function_exists('matewa_client_mail_delivery_get_route')) {
    function matewa_client_mail_delivery_get_route() {
        $options = matewa_client_mail_delivery_get_options();
        return $options['route'];
    }
}

if (!function_exists('matewa_client_mail_delivery_allow_site_fallback')) {
    function matewa_client_mail_delivery_allow_site_fallback() {
        $options = matewa_client_mail_delivery_get_options();
        return '1' === (string) ($options['allow_site_fallback'] ?? '1');
    }
}

if (!function_exists('matewa_client_mail_delivery_managed_relay_is_eligible')) {
    function matewa_client_mail_delivery_managed_relay_is_eligible() {
        if (function_exists('matewa_agency_client_entitlement_is_active') && matewa_agency_client_entitlement_is_active()) {
            return true;
        }

        if (function_exists('matewa_get_entitlement_state')) {
            $state = matewa_get_entitlement_state();
            $allowed = array();
            foreach (array('MATEWA_MODE_PRO', 'MATEWA_MODE_MASTER', 'MATEWA_MODE_TRIAL') as $constant) {
                if (defined($constant)) {
                    $allowed[] = constant($constant);
                }
            }
            $allowed = array_merge($allowed, array('pro', 'master', 'trial'));
            if (in_array($state, array_unique($allowed), true)) {
                return true;
            }
        }

        if (function_exists('matewa_founder_beta_is_active') && matewa_founder_beta_is_active()) {
            return true;
        }

        if (function_exists('matewa_trial_active') && matewa_trial_active()) {
            return true;
        }

        return false;
    }
}


if (!function_exists('matewa_client_mail_delivery_sanitise_for_save')) {
    function matewa_client_mail_delivery_sanitise_for_save($raw, $previous = null) {
        $raw = is_array($raw) ? $raw : array();
        $previous = is_array($previous) ? $previous : matewa_client_mail_delivery_get_options();
        $presets = matewa_client_mail_delivery_provider_presets();

        $provider = matewa_client_mail_delivery_normalise_provider($raw['provider'] ?? ($previous['provider'] ?? 'other'));
        $preset = $presets[$provider] ?? $presets['other'];

        $route = matewa_client_mail_delivery_normalise_route($raw['route'] ?? ($previous['route'] ?? 'site_mail'));
        $sender_name = sanitize_text_field((string) ($raw['sender_name'] ?? ''));
        $sender_email = sanitize_email((string) ($raw['sender_email'] ?? ''));
        $reply_to_email = sanitize_email((string) ($raw['reply_to_email'] ?? ''));
        $smtp_host = sanitize_text_field((string) ($raw['smtp_host'] ?? ''));
        $smtp_port = absint($raw['smtp_port'] ?? 0);
        $smtp_encryption = matewa_client_mail_delivery_normalise_encryption($raw['smtp_encryption'] ?? '');
        $smtp_username = sanitize_text_field((string) ($raw['smtp_username'] ?? ''));
        $smtp_password = isset($raw['smtp_password']) ? trim((string) $raw['smtp_password']) : '';
        $relay_endpoint = isset($raw['relay_endpoint']) ? esc_url_raw(trim((string) $raw['relay_endpoint'])) : '';
        $relay_token = isset($raw['relay_token']) ? trim((string) $raw['relay_token']) : '';
        $clear_password = !empty($raw['clear_password']);
        $clear_relay_token = !empty($raw['clear_relay_token']);

        if ('client_smtp' === $route && '' === $smtp_host && '' !== (string) ($preset['host'] ?? '')) {
            $smtp_host = sanitize_text_field((string) $preset['host']);
        }
        if ('client_smtp' === $route && 0 === $smtp_port && '' !== (string) ($preset['port'] ?? '')) {
            $smtp_port = absint($preset['port']);
        }
        if ('client_smtp' === $route && empty($raw['smtp_encryption']) && '' !== (string) ($preset['encryption'] ?? '')) {
            $smtp_encryption = matewa_client_mail_delivery_normalise_encryption($preset['encryption']);
        }

        if ('managed_relay' === $route && '' === $relay_endpoint && defined('MATEWA_MANAGED_MAIL_RELAY_DEFAULT_ENDPOINT')) {
            $relay_endpoint = esc_url_raw(MATEWA_MANAGED_MAIL_RELAY_DEFAULT_ENDPOINT);
        }
        if ('' !== $relay_endpoint) {
            $relay_parts = wp_parse_url($relay_endpoint);
            if (!is_array($relay_parts) || empty($relay_parts['host']) || strtolower((string) ($relay_parts['scheme'] ?? '')) !== 'https') {
                $relay_endpoint = '';
            }
        }

        $password_enc = (string) ($previous['smtp_password_enc'] ?? '');
        if ($clear_password) {
            $password_enc = '';
        } elseif ('' !== $smtp_password) {
            $next_enc = matewa_client_mail_delivery_encrypt_password($smtp_password);
            if ('' !== $next_enc) {
                $password_enc = $next_enc;
            }
        }

        $relay_token_enc = (string) ($previous['relay_token_enc'] ?? '');
        $relay_token_hash = (string) ($previous['relay_token_hash'] ?? '');
        if ($clear_relay_token) {
            $relay_token_enc = '';
            $relay_token_hash = '';
        } elseif ('' !== $relay_token) {
            $next_relay_enc = matewa_client_mail_delivery_encrypt_password($relay_token);
            if ('' !== $next_relay_enc) {
                $relay_token_enc = $next_relay_enc;
                $relay_token_hash = hash('sha256', $relay_token);
            }
        }

        return array(
            'route'               => $route,
            'provider'            => $provider,
            'sender_name'         => $sender_name,
            'sender_email'        => is_email($sender_email) ? $sender_email : '',
            'reply_to_email'      => is_email($reply_to_email) ? $reply_to_email : '',
            'smtp_host'           => $smtp_host,
            'smtp_port'           => 0 < $smtp_port ? (string) $smtp_port : '',
            'smtp_encryption'     => $smtp_encryption,
            'smtp_username'       => $smtp_username,
            'smtp_password_enc'   => $password_enc,
            'relay_endpoint'      => $relay_endpoint,
            'relay_token_enc'     => $relay_token_enc,
            'relay_token_hash'    => sanitize_text_field($relay_token_hash),
            'allow_site_fallback' => !empty($raw['allow_site_fallback']) ? '1' : '0',
            'updated_at'          => current_time('mysql'),
        );
    }
}

if (!function_exists('matewa_client_mail_delivery_save_from_post')) {
    function matewa_client_mail_delivery_save_from_post($raw) {
        $options = matewa_client_mail_delivery_sanitise_for_save($raw, matewa_client_mail_delivery_get_options());
        update_option(MATEWA_CLIENT_MAIL_DELIVERY_OPTION, $options, false);
        return $options;
    }
}

if (!function_exists('matewa_client_mail_delivery_has_complete_smtp')) {
    function matewa_client_mail_delivery_has_complete_smtp($options = null) {
        $options = is_array($options) ? $options : matewa_client_mail_delivery_get_options();
        return (
            'client_smtp' === (string) ($options['route'] ?? '')
            && '' !== (string) ($options['smtp_host'] ?? '')
            && 0 < absint($options['smtp_port'] ?? 0)
            && '' !== (string) ($options['smtp_username'] ?? '')
            && !empty($options['smtp_password_set'])
        );
    }
}

if (!function_exists('matewa_client_mail_delivery_header_email')) {
    function matewa_client_mail_delivery_header_email($headers, $header_name) {
        $headers = is_array($headers) ? $headers : array();
        $header_name = strtolower((string) $header_name);

        foreach ($headers as $header) {
            $header = (string) $header;
            if (false === strpos($header, ':')) {
                continue;
            }

            list($name, $value) = explode(':', $header, 2);
            if (strtolower(trim($name)) !== $header_name) {
                continue;
            }

            if (preg_match('/<([^>]+)>/', $value, $match)) {
                $email = sanitize_email($match[1]);
            } else {
                $email = sanitize_email(trim($value));
            }

            if (is_email($email)) {
                return $email;
            }
        }

        return '';
    }
}

if (!function_exists('matewa_client_mail_delivery_load_phpmailer')) {
    function matewa_client_mail_delivery_load_phpmailer() {
        if (class_exists('PHPMailer\\PHPMailer\\PHPMailer')) {
            return true;
        }

        $base = trailingslashit(ABSPATH . WPINC) . 'PHPMailer/';
        $files = array('Exception.php', 'PHPMailer.php', 'SMTP.php');
        foreach ($files as $file) {
            $path = $base . $file;
            if (is_readable($path)) {
                require_once $path;
            }
        }

        return class_exists('PHPMailer\\PHPMailer\\PHPMailer');
    }
}

if (!function_exists('matewa_client_mail_delivery_send_via_smtp')) {
    function matewa_client_mail_delivery_send_via_smtp(array $payload) {
        $options = matewa_client_mail_delivery_get_options();
        if (!matewa_client_mail_delivery_has_complete_smtp($options)) {
            return array(
                'sent'   => false,
                'method' => 'client_smtp_incomplete',
                'error'  => 'Client SMTP route is selected but the SMTP details are incomplete.',
            );
        }

        if (!matewa_client_mail_delivery_load_phpmailer()) {
            return array(
                'sent'   => false,
                'method' => 'client_smtp_phpmailer_missing',
                'error'  => 'WordPress PHPMailer could not be loaded.',
            );
        }

        $password = matewa_client_mail_delivery_decrypt_password((string) ($options['smtp_password_enc'] ?? ''));
        if ('' === $password) {
            return array(
                'sent'   => false,
                'method' => 'client_smtp_password_missing',
                'error'  => 'Saved SMTP password could not be read. Re-enter the password and save again.',
            );
        }

        try {
            $mailer_class = 'PHPMailer\\PHPMailer\\PHPMailer';
            $mailer = new $mailer_class(true);
            $mailer->CharSet = get_bloginfo('charset') ? get_bloginfo('charset') : 'UTF-8';
            $mailer->isSMTP();
            $mailer->Host = (string) $options['smtp_host'];
            $mailer->SMTPAuth = true;
            $mailer->Username = (string) $options['smtp_username'];
            $mailer->Password = $password;
            $mailer->Port = absint($options['smtp_port']);

            if ('ssl' === (string) $options['smtp_encryption']) {
                $mailer->SMTPSecure = 'ssl';
            } elseif ('tls' === (string) $options['smtp_encryption']) {
                $mailer->SMTPSecure = 'tls';
            }

            $from_email = is_email((string) ($options['sender_email'] ?? '')) ? (string) $options['sender_email'] : sanitize_email((string) $options['smtp_username']);
            if (!is_email($from_email)) {
                return array(
                    'sent'   => false,
                    'method' => 'client_smtp_invalid_sender',
                    'error'  => 'Client SMTP sender email is invalid.',
                );
            }

            $from_name = sanitize_text_field((string) ($options['sender_name'] ?? ''));
            if ('' === $from_name) {
                $from_name = sanitize_text_field((string) get_bloginfo('name'));
            }
            if ('' === $from_name) {
                $from_name = 'Website Leads';
            }

            $mailer->setFrom($from_email, $from_name, false);
            $mailer->addAddress(sanitize_email((string) ($payload['to'] ?? '')));

            $reply_to = sanitize_email((string) ($options['reply_to_email'] ?? ''));
            if (!is_email($reply_to)) {
                $reply_to = matewa_client_mail_delivery_header_email($payload['headers'] ?? array(), 'Reply-To');
            }
            if (is_email($reply_to)) {
                $mailer->addReplyTo($reply_to);
            }

            $mailer->isHTML(true);
            $mailer->Subject = wp_specialchars_decode((string) ($payload['subject'] ?? ''), ENT_QUOTES);
            $mailer->Body = (string) ($payload['message'] ?? '');
            $mailer->AltBody = wp_strip_all_tags((string) ($payload['message'] ?? ''));
            $mailer->send();

            return array(
                'sent'   => true,
                'method' => 'client_smtp_' . sanitize_key((string) ($options['provider'] ?? 'other')),
                'error'  => '',
            );
        } catch (Exception $exception) {
            return array(
                'sent'   => false,
                'method' => 'client_smtp_failed',
                'error'  => $exception->getMessage(),
            );
        }
    }
}

if (!function_exists('matewa_client_mail_delivery_get_last_test')) {
    function matewa_client_mail_delivery_get_last_test() {
        $result = get_option(MATEWA_CLIENT_MAIL_DELIVERY_TEST_OPTION, array());
        return is_array($result) ? $result : array();
    }
}

if (!function_exists('matewa_client_mail_delivery_store_last_test')) {
    function matewa_client_mail_delivery_store_last_test(array $result) {
        $result['tested_at'] = current_time('mysql');
        update_option(MATEWA_CLIENT_MAIL_DELIVERY_TEST_OPTION, $result, false);
    }
}

if (!function_exists('matewa_client_mail_delivery_status_label')) {
    function matewa_client_mail_delivery_status_label() {
        $options = matewa_client_mail_delivery_get_options();
        $last = matewa_client_mail_delivery_get_last_test();

        if (!empty($last['sent'])) {
            return 'Client mail route test passed on ' . sanitize_text_field((string) ($last['tested_at'] ?? 'recently')) . '.';
        }

        if (!empty($last['error'])) {
            return 'Last client mail route test failed: ' . sanitize_text_field((string) $last['error']);
        }

        if ('managed_relay' === (string) ($options['route'] ?? '')) {
            if (function_exists('matewa_client_mail_delivery_managed_relay_is_eligible') && matewa_client_mail_delivery_managed_relay_is_eligible()) {
                return '247ChatMate managed sender is active. No relay token is needed for licensed, trial or agency-managed sites.';
            }

            return '247ChatMate managed sender needs an active trial, licence or agency-managed setup before it can send.';
        }

        if ('client_smtp' === (string) ($options['route'] ?? '') && !matewa_client_mail_delivery_has_complete_smtp($options)) {
            return 'Client SMTP is selected, but the SMTP details are not complete yet.';
        }

        return 'Using this website’s mail setup. SMTP plugins such as WP Mail SMTP, Microsoft 365, Google Workspace or Hostinger SMTP can handle delivery.';
    }
}

if (!function_exists('matewa_client_mail_delivery_handle_test_request')) {
    function matewa_client_mail_delivery_handle_test_request() {
        if (!is_admin() || !current_user_can('manage_options')) {
            return;
        }

        $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
        if ('247chatmate' !== $page || empty($_POST['matewa_client_mail_delivery_action'])) {
            return;
        }

        check_admin_referer('matewa_client_mail_delivery_nonce', 'matewa_client_mail_delivery_nonce');

        $action = sanitize_key(wp_unslash($_POST['matewa_client_mail_delivery_action']));
        if ('test_route' !== $action) {
            wp_safe_redirect(admin_url('admin.php?page=247chatmate&matewa_saved_section=setup&matewa_scroll_target=client_mail_delivery&matewa_client_mail_notice=unknown_action#matewa-client-mail-delivery-options'));
            exit;
        }

        $recipient = sanitize_email((string) get_option('matewa_leads_email', ''));
        if (!is_email($recipient)) {
            $result = array(
                'sent'  => false,
                'error' => 'Add a valid Lead destination inbox before testing mail delivery.',
                'method'=> 'client_mail_test_missing_recipient',
            );
            matewa_client_mail_delivery_store_last_test($result);
            wp_safe_redirect(admin_url('admin.php?page=247chatmate&matewa_saved_section=setup&matewa_scroll_target=client_mail_delivery&matewa_client_mail_notice=missing_recipient#matewa-client-mail-delivery-options'));
            exit;
        }

        $subject = 'Test: 247ChatMate client mail delivery route';
        $message = '<div style="font-family:Inter,Arial,sans-serif;max-width:620px;padding:22px;border:1px solid #d9f5df;border-radius:16px;background:#f7fff9;color:#102a1d;">'
            . '<h2 style="margin:0 0 10px;font-size:22px;">247ChatMate mail delivery test</h2>'
            . '<p style="margin:0 0 10px;line-height:1.5;">This confirms the selected client-site mail route can send lead alerts and weekly summaries.</p>'
            . '<p style="margin:0;line-height:1.5;"><strong>Site:</strong> ' . esc_html(wp_parse_url(home_url(), PHP_URL_HOST)) . '</p>'
            . '</div>';

        $headers = array('Content-Type: text/html; charset=UTF-8');
        $result = function_exists('matewa_fallback_mail_send')
            ? matewa_fallback_mail_send('weekly_summary_test', $recipient, $subject, $message, $headers, array('test_type' => 'client_mail_delivery'), false)
            : array('sent' => wp_mail($recipient, $subject, $message, $headers), 'method' => 'wp_mail_legacy', 'error' => '');

        matewa_client_mail_delivery_store_last_test($result);
        $notice = !empty($result['sent']) ? 'test_sent' : 'test_failed';
        wp_safe_redirect(admin_url('admin.php?page=247chatmate&matewa_saved_section=setup&matewa_scroll_target=client_mail_delivery&matewa_client_mail_notice=' . $notice . '#matewa-client-mail-delivery-options'));
        exit;
    }
}

add_action('admin_init', 'matewa_client_mail_delivery_handle_test_request', 2);

if (!function_exists('matewa_client_mail_delivery_render_action_forms')) {
    function matewa_client_mail_delivery_render_action_forms() {
        ?>
        <form id="matewa-client-mail-delivery-test-form" class="matewa-admin-hidden-action-form" method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>" aria-hidden="true">
            <?php wp_nonce_field('matewa_client_mail_delivery_nonce', 'matewa_client_mail_delivery_nonce'); ?>
            <input type="hidden" name="matewa_client_mail_delivery_action" value="test_route">
        </form>
        <?php
    }
}

if (!function_exists('matewa_client_mail_delivery_render_panel')) {
    function matewa_client_mail_delivery_render_panel() {
        $options = matewa_client_mail_delivery_get_options();
        $presets = matewa_client_mail_delivery_provider_presets();
        $notice_key = isset($_GET['matewa_client_mail_notice']) ? sanitize_key(wp_unslash($_GET['matewa_client_mail_notice'])) : '';
        $notice_messages = array(
            'test_sent'         => array('type' => 'success', 'message' => 'Client mail route test sent to the Lead destination inbox.'),
            'test_failed'       => array('type' => 'warning', 'message' => 'Client mail route test failed. Check the selected route and saved SMTP details.'),
            'missing_recipient' => array('type' => 'warning', 'message' => 'Add a valid Lead destination inbox before testing mail delivery.'),
            'unknown_action'    => array('type' => 'warning', 'message' => 'Client mail delivery action was not recognised.'),
        );
        $notice = $notice_messages[$notice_key] ?? array();
        $provider_note = $presets[$options['provider']]['note'] ?? $presets['other']['note'];
        $managed_relay_eligible = function_exists('matewa_client_mail_delivery_managed_relay_is_eligible') && matewa_client_mail_delivery_managed_relay_is_eligible();
        $internal_relay_fields = defined('MATEWA_SHOW_INTERNAL_MAIL_RELAY_FIELDS') && true === MATEWA_SHOW_INTERNAL_MAIL_RELAY_FIELDS;
        ?>
        <details id="matewa-client-mail-delivery-options" class="matewa-client-mail-delivery-zone matewa-setup-lead-detail">
            <summary>Advanced email delivery settings</summary>
            <div class="matewa-client-mail-delivery-card" data-matewa-client-mail-card aria-label="Advanced email delivery settings">
                <div class="matewa-client-mail-delivery-heading">
                    <div>
                        <strong>Client mail delivery route</strong>
                        <p>Most clients can leave this alone. Use the managed sender for simple fallback delivery, or open client SMTP only when their IT/email provider gives SMTP details.</p>
                    </div>
                    <span class="matewa-client-mail-status-pill"><?php echo esc_html('managed_relay' === $options['route'] ? '247ChatMate managed sender' : ('client_smtp' === $options['route'] ? 'Client SMTP route' : 'Website mail route')); ?></span>
                </div>

                <?php if (!empty($notice)) : ?>
                    <div class="matewa-dashboard-history-notice is-<?php echo esc_attr($notice['type']); ?>" role="status" aria-live="polite">
                        <?php echo esc_html($notice['message']); ?>
                    </div>
                <?php endif; ?>

                <div class="matewa-dashboard-history-notice is-success" role="status" aria-live="polite">
                    <strong>Status:</strong> <?php echo esc_html(matewa_client_mail_delivery_status_label()); ?>
                </div>

                <div class="matewa-client-mail-delivery-grid">
                    <label>
                        Delivery route
                        <select form="matewa-settings-form" name="matewa_client_mail_delivery[route]" data-matewa-client-mail-route>
                            <option value="site_mail" <?php selected($options['route'], 'site_mail'); ?>>Use this website’s mail setup</option>
                            <option value="managed_relay" <?php selected($options['route'], 'managed_relay'); ?>>Use 247ChatMate managed sender</option>
                            <option value="client_smtp" <?php selected($options['route'], 'client_smtp'); ?>>Use client SMTP details saved on this site</option>
                        </select>
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        Provider preset
                        <select form="matewa-settings-form" name="matewa_client_mail_delivery[provider]">
                            <?php foreach ($presets as $preset_key => $preset) : ?>
                                <option value="<?php echo esc_attr($preset_key); ?>" <?php selected($options['provider'], $preset_key); ?>><?php echo esc_html($preset['label']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>

                    <label class="matewa-client-mail-sender-field">
                        Sender name
                        <input form="matewa-settings-form" type="text" name="matewa_client_mail_delivery[sender_name]" value="<?php echo esc_attr((string) $options['sender_name']); ?>" placeholder="e.g. Jim’s Cars Website Leads">
                    </label>

                    <label class="matewa-client-mail-sender-field">
                        Sender email
                        <input form="matewa-settings-form" type="email" name="matewa_client_mail_delivery[sender_email]" value="<?php echo esc_attr((string) $options['sender_email']); ?>" placeholder="e.g. hello@clientbusiness.co.uk">
                    </label>

                    <label class="matewa-client-mail-sender-field">
                        Reply-To email
                        <input form="matewa-settings-form" type="email" name="matewa_client_mail_delivery[reply_to_email]" value="<?php echo esc_attr((string) $options['reply_to_email']); ?>" placeholder="e.g. enquiries@clientbusiness.co.uk">
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        SMTP host
                        <input form="matewa-settings-form" type="text" name="matewa_client_mail_delivery[smtp_host]" value="<?php echo esc_attr((string) $options['smtp_host']); ?>" placeholder="<?php echo esc_attr((string) ($presets[$options['provider']]['host'] ?? 'smtp.example.com')); ?>">
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        SMTP port
                        <input form="matewa-settings-form" type="number" min="1" max="65535" name="matewa_client_mail_delivery[smtp_port]" value="<?php echo esc_attr((string) $options['smtp_port']); ?>" placeholder="<?php echo esc_attr((string) ($presets[$options['provider']]['port'] ?? '587')); ?>">
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        Encryption
                        <select form="matewa-settings-form" name="matewa_client_mail_delivery[smtp_encryption]">
                            <option value="tls" <?php selected($options['smtp_encryption'], 'tls'); ?>>TLS / STARTTLS</option>
                            <option value="ssl" <?php selected($options['smtp_encryption'], 'ssl'); ?>>SSL</option>
                            <option value="none" <?php selected($options['smtp_encryption'], 'none'); ?>>None</option>
                        </select>
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        SMTP username
                        <input form="matewa-settings-form" type="text" name="matewa_client_mail_delivery[smtp_username]" value="<?php echo esc_attr((string) $options['smtp_username']); ?>" placeholder="e.g. full mailbox email">
                    </label>

                    <label class="matewa-client-mail-smtp-field">
                        SMTP password / app password
                        <input form="matewa-settings-form" type="password" name="matewa_client_mail_delivery[smtp_password]" value="" autocomplete="new-password" placeholder="<?php echo !empty($options['smtp_password_set']) ? esc_attr('Saved — leave blank to keep') : esc_attr('Paste password if using client SMTP'); ?>">
                    </label>

                    <?php if ($internal_relay_fields) : ?>
                        <label class="matewa-client-mail-relay-field">
                            Managed relay endpoint
                            <input form="matewa-settings-form" type="url" name="matewa_client_mail_delivery[relay_endpoint]" value="<?php echo esc_attr((string) $options['relay_endpoint']); ?>" placeholder="https://247chatmate.com/wp-json/247chatmate/v1/mail/relay">
                        </label>

                        <label class="matewa-client-mail-relay-field">
                            Managed relay token
                            <input form="matewa-settings-form" type="password" name="matewa_client_mail_delivery[relay_token]" value="" autocomplete="new-password" placeholder="<?php echo !empty($options['relay_token_set']) ? esc_attr('Saved — leave blank to keep') : esc_attr('Internal support token only'); ?>">
                        </label>
                    <?php endif; ?>
                </div>

                <div class="matewa-client-mail-managed-note" data-matewa-client-mail-managed-note>
                    <strong>247ChatMate managed sender</strong>
                    <p><?php echo esc_html($managed_relay_eligible ? 'Available for this site. No relay token is needed; the plugin uses the site’s trial, licence or agency-managed status to allow the managed sender.' : 'This route becomes available after an active trial, licence or agency-managed setup is detected.'); ?></p>
                </div>

                <p class="description matewa-client-mail-provider-note"><?php echo esc_html($provider_note); ?></p>

                <div class="matewa-client-mail-checks">
                    <label class="matewa-client-mail-smtp-field">
                        <input form="matewa-settings-form" type="checkbox" name="matewa_client_mail_delivery[allow_site_fallback]" value="1" <?php checked($options['allow_site_fallback'], '1'); ?>>
                        If client SMTP fails, try this website’s normal WordPress mail route before queueing.
                    </label>
                    <label class="matewa-client-mail-smtp-field">
                        <input form="matewa-settings-form" type="checkbox" name="matewa_client_mail_delivery[clear_password]" value="1">
                        Clear saved SMTP password on next save.
                    </label>
                    <?php if ($internal_relay_fields) : ?>
                        <label class="matewa-client-mail-relay-field">
                            <input form="matewa-settings-form" type="checkbox" name="matewa_client_mail_delivery[clear_relay_token]" value="1">
                            Clear saved managed relay token on next save.
                        </label>
                    <?php endif; ?>
                </div>

                <div class="matewa-client-mail-actions">
                    <button type="submit" form="matewa-settings-form" class="matewa-dashboard-history-button is-secondary">Save delivery settings</button>
                    <button type="submit" form="matewa-client-mail-delivery-test-form" class="matewa-dashboard-history-button is-secondary">Send client route test</button>
                </div>

                <p class="description">Security: SMTP passwords are never printed back into the page. Blank password fields keep the saved value. Agency-imported client sites can still change these local settings after the setup pack is pushed.</p>
            </div>
        </details>
        <?php
    }
}
