<?php
/**
 * 247ChatMate — Agency Hub partial.
 * Checkpoint: AGENCYHUBRESPONSIBILITYSPLIT1
 *
 * Display-only split from admin/views/partials/agency-hub.php.
 * Expects parent Agency Hub variables to be prepared before include.
 */

defined('ABSPATH') || exit;
?>
    <section class="matewa-agency-client-board" aria-label="Agency reporting and automation foundation">
        <div class="matewa-agency-client-board-header">
            <div>
                <span class="matewa-agency-hub-kicker">Agency foundation</span>
                <h4>Reporting, setup and review controls</h4>
                <p>These controls prepare the agency reporting flow without sending weekly emails, parsing PDFs, or enforcing payment/grace automation yet.</p>
            </div>
            <div class="matewa-agency-client-board-actions" aria-label="Agency foundation status">
                <span class="matewa-agency-soft-pill <?php echo $matewa_agency_routing_ready ? esc_attr('is-ready') : esc_attr('is-warning'); ?>"><?php echo $matewa_agency_routing_ready ? esc_html('Routing ready') : esc_html('Routing needs email'); ?></span>
                <span class="matewa-agency-soft-pill">Automation parked</span>
            </div>
        </div>

        <?php if ('' !== $matewa_agency_setup_request_notice_message) : ?>
            <div class="matewa-agency-bundle-notice is-<?php echo esc_attr($matewa_agency_setup_request_notice_type); ?>" role="status" aria-live="polite">
                <?php echo esc_html($matewa_agency_setup_request_notice_message); ?>
            </div>
        <?php endif; ?>

        <div class="matewa-agency-client-pack-form matewa-agency-routing-panel<?php echo $matewa_agency_routing_sent ? esc_attr(' is-sent') : ''; ?>" aria-label="Agency email routing fields">
            <div class="matewa-agency-client-pack-form-head">
                <div>
                    <strong>Agency email routing</strong>
                    <span>Store the agency inboxes now. Weekly roundup sending remains a later locked checkpoint.</span>
                </div>
                <em><?php echo $matewa_agency_routing_sent ? esc_html('Last test accepted') : ($matewa_agency_routing_ready ? esc_html('Ready') : esc_html('Needs setup')); ?></em>
            </div>
            <?php if ('' !== $matewa_agency_routing_notice_message) : ?>
                <div class="matewa-agency-bundle-notice is-<?php echo esc_attr($matewa_agency_routing_notice_type); ?>" role="status" aria-live="polite">
                    <?php echo esc_html($matewa_agency_routing_notice_message); ?>
                </div>
            <?php endif; ?>
            <?php if ($matewa_agency_last_routing_time > 0 && !empty($matewa_agency_last_routing_results)) : ?>
                <div class="matewa-agency-bundle-note" role="status" aria-live="polite">
                    <strong>Last Send now test:</strong>
                    <?php echo esc_html(wp_date((string) get_option('date_format', 'Y-m-d') . ' ' . (string) get_option('time_format', 'H:i'), $matewa_agency_last_routing_time)); ?>.
                    <?php if ('' !== $matewa_agency_last_routing_test_id) : ?>
                        <br>Test ID: <strong><?php echo esc_html($matewa_agency_last_routing_test_id); ?></strong>
                    <?php endif; ?>
                    <?php foreach ($matewa_agency_last_routing_results as $matewa_route_result) : ?>
                        <?php
                        $matewa_route_label = isset($matewa_route_result['label']) ? sanitize_text_field((string) $matewa_route_result['label']) : 'Route';
                        $matewa_route_email = isset($matewa_route_result['email']) ? sanitize_email((string) $matewa_route_result['email']) : '';
                        $matewa_route_sent = !empty($matewa_route_result['sent']);
                        $matewa_route_method = isset($matewa_route_result['method']) ? sanitize_text_field((string) $matewa_route_result['method']) : '';
                        $matewa_route_error = isset($matewa_route_result['error']) ? sanitize_text_field((string) $matewa_route_result['error']) : '';
                        ?>
                        <br><?php echo esc_html($matewa_route_label); ?>:
                        <strong><?php echo $matewa_route_sent ? esc_html('accepted') : esc_html('failed'); ?></strong>
                        <?php echo '' !== $matewa_route_email ? esc_html('to ' . $matewa_route_email) : ''; ?>
                        <?php echo $matewa_route_sent && '' !== $matewa_route_method ? esc_html(' via ' . str_replace('_', ' ', $matewa_route_method)) : ''; ?>
                        <?php echo !$matewa_route_sent && '' !== $matewa_route_error ? esc_html(' — ' . $matewa_route_error) : ''; ?>
                    <?php endforeach; ?>
                    <br><small>Accepted means WordPress handed the message to the site mailer. This version uses the original WordPress/Hostinger sender address for delivery and only changes the visible sender name to 247ChatMate. If it is not visible in the inbox, check Spam/Junk and search for the test ID/subject. Proper inbox delivery will need the later SMTP/Postmark/Brevo email automation checkpoint.</small>
                </div>
            <?php endif; ?>
            <div class="matewa-agency-client-pack-grid is-details">
                <label>
                    <span>Agency admin email</span>
                    <input type="email" name="matewa_agency_admin_email" placeholder="e.g. admin@agency.co.uk" value="<?php echo esc_attr($matewa_agency_admin_email); ?>" autocomplete="email">
                </label>
                <label>
                    <span>Agency roundup email</span>
                    <input type="email" name="matewa_agency_roundup_email" placeholder="e.g. reports@agency.co.uk" value="<?php echo esc_attr($matewa_agency_roundup_email); ?>" autocomplete="email">
                </label>
            </div>
            <div class="matewa-agency-client-pack-actions matewa-agency-routing-actions">
                <button type="submit" class="button button-primary matewa-agency-routing-save-button" name="matewa_agency_routing_action" value="save">Save agency routing</button>
                <button type="submit" class="button button-primary matewa-agency-routing-send-now-button" name="matewa_agency_routing_action" value="send_roundup_now">Send now</button>
                <?php if ($matewa_agency_last_routing_time > 0 || '' !== $matewa_agency_routing_notice_code) : ?>
                    <button type="submit" class="button matewa-agency-routing-clear-button" name="matewa_agency_routing_action" value="clear_last_test">Clear test result</button>
                <?php endif; ?>
                <small>Client lead alerts still use each setup pack’s Client leads email. These agency fields are for agency notices and future weekly client roundups. Send now saves these fields, then sends a fresh manual test to both agency inboxes every time it is pressed.</small>
            </div>

            <details class="matewa-email-profile-card is-agency matewa-email-profile-card--agency-advanced" aria-label="Agency email proxy and sender architecture">
                <summary class="matewa-email-profile-card__summary">
                    <div class="matewa-email-profile-card__head">
                        <div>
                            <strong>Agency email proxy &amp; sender settings</strong>
                            <span>Optional advanced architecture. Agencies can store their own proxy endpoint and hashed token here; fallback delivery stays live until the proxy route is tested and deliberately enabled.</span>
                        </div>
                        <em><?php echo esc_html($matewa_agency_email_profile_status); ?></em>
                    </div>
                </summary>
                <div class="matewa-email-profile-card__body">
                    <div class="matewa-email-profile-architecture-note is-locked">
                        <strong>Current live route:</strong>
                        <span>Lead emails and weekly summaries still use the 247ChatMate fallback sender. Agency proxy details are stored as architecture only, so agencies do not open unfinished Postmark, Brevo, Google Workspace or SMTP settings inside the plugin.</span>
                    </div>
                    <?php $matewa_agency_email_architecture = function_exists('matewa_email_profile_architecture_status') ? matewa_email_profile_architecture_status('agency') : array(); ?>
                    <?php if (!empty($matewa_agency_email_architecture)) : ?>
                        <div class="matewa-email-architecture-grid" aria-label="Agency email sender architecture status">
                            <?php foreach ($matewa_agency_email_architecture as $matewa_email_architecture_item) : ?>
                                <div class="matewa-email-architecture-pill is-<?php echo esc_attr((string) ($matewa_email_architecture_item['state'] ?? 'ready')); ?>">
                                    <span><?php echo esc_html((string) ($matewa_email_architecture_item['label'] ?? 'Status')); ?></span>
                                    <strong><?php echo esc_html((string) ($matewa_email_architecture_item['value'] ?? 'Ready')); ?></strong>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>

                    <div class="matewa-email-profile-grid">
                        <label>
                            <span>Stored agency sender name</span>
                            <input type="text" name="matewa_email_profile_agency[sender_name]" value="<?php echo esc_attr((string) ($matewa_agency_email_profile['sender_name'] ?? '')); ?>" placeholder="e.g. ABC Agency">
                        </label>
                        <label>
                            <span>Stored agency sender email</span>
                            <input type="email" name="matewa_email_profile_agency[sender_email]" value="<?php echo esc_attr((string) ($matewa_agency_email_profile['sender_email'] ?? '')); ?>" placeholder="e.g. hello@agency.co.uk">
                        </label>
                        <label>
                            <span>Reply-To email</span>
                            <input type="email" name="matewa_email_profile_agency[reply_to_email]" value="<?php echo esc_attr((string) ($matewa_agency_email_profile['reply_to_email'] ?? $matewa_agency_admin_email)); ?>" placeholder="e.g. admin@agency.co.uk">
                        </label>
                        <label>
                            <span>Active delivery route</span>
                            <input type="text" value="<?php echo esc_attr(function_exists('matewa_email_profile_active_route_label') ? matewa_email_profile_active_route_label('agency') : '247ChatMate fallback sender'); ?>" readonly aria-readonly="true">
                        </label>
                    </div>

                    <div class="matewa-email-proxy-box" aria-label="Agency-owned email proxy architecture">
                        <div class="matewa-email-proxy-box__head">
                            <div>
                                <strong>Agency-owned email proxy</strong>
                                <span>For agencies that run their own verified sending layer. Store the endpoint and signing token here; the plain token is never shown again after save.</span>
                            </div>
                            <em><?php echo esc_html(function_exists('matewa_email_profile_proxy_status_label') ? matewa_email_profile_proxy_status_label($matewa_agency_email_profile) : 'Architecture slot'); ?></em>
                        </div>
                        <label class="matewa-email-proxy-toggle">
                            <input type="checkbox" name="matewa_email_profile_agency[proxy_enabled]" value="1" <?php checked('1', (string) ($matewa_agency_email_profile['proxy_enabled'] ?? '0')); ?>>
                            <span>Store agency proxy details for later controlled routing</span>
                        </label>
                        <div class="matewa-email-profile-grid is-proxy-grid">
                            <label>
                                <span>Proxy endpoint URL</span>
                                <input type="url" name="matewa_email_profile_agency[proxy_url]" value="<?php echo esc_attr((string) ($matewa_agency_email_profile['proxy_url'] ?? '')); ?>" placeholder="https://mail.agency.co.uk/247chatmate/email-proxy">
                            </label>
                            <label>
                                <span>Proxy route ID</span>
                                <input type="text" name="matewa_email_profile_agency[proxy_route_id]" value="<?php echo esc_attr((string) ($matewa_agency_email_profile['proxy_route_id'] ?? '')); ?>" placeholder="e.g. agency-main-route">
                            </label>
                            <label>
                                <span>Proxy signing token</span>
                                <input type="password" name="matewa_email_profile_agency[proxy_token]" value="" placeholder="Paste new token; blank keeps saved token" autocomplete="new-password">
                            </label>
                            <label>
                                <span>Saved token status</span>
                                <input type="text" value="<?php echo !empty($matewa_agency_email_profile['proxy_token_set']) && '1' === (string) $matewa_agency_email_profile['proxy_token_set'] ? esc_attr('Hashed token stored' . (!empty($matewa_agency_email_profile['proxy_token_hint']) ? ' ending ' . (string) $matewa_agency_email_profile['proxy_token_hint'] : '')) : esc_attr('No proxy token stored'); ?>" readonly aria-readonly="true">
                            </label>
                        </div>
                        <?php if (!empty($matewa_agency_email_profile['proxy_token_set']) && '1' === (string) $matewa_agency_email_profile['proxy_token_set']) : ?>
                            <label class="matewa-email-proxy-clear">
                                <input type="checkbox" name="matewa_email_profile_agency[proxy_token_clear]" value="1">
                                <span>Clear saved proxy token on next save</span>
                            </label>
                        <?php endif; ?>
                        <p class="description">HTTPS is required for stored proxy endpoints. The plugin stores only the token hash, keeps 247ChatMate fallback delivery live, and does not expose direct provider API keys on client sites.</p>
                    </div>

                    <input type="hidden" name="matewa_email_profile_agency[provider]" value="fallback_247chatmate">
                    <input type="hidden" name="matewa_email_profile_agency[use_for_setup]" value="1">
                    <input type="hidden" name="matewa_email_profile_agency[use_for_roundups]" value="1">
                    <p class="description"><?php echo esc_html($matewa_agency_email_profile_note); ?></p>
                </div>
            </details>
        </div>

        <div class="matewa-agency-client-pack-form matewa-agency-setup-request-panel" aria-label="Client setup request controls" data-matewa-agency-setup-request data-matewa-agency-name="<?php echo esc_attr($matewa_agency_setup_request_agency_name); ?>">
            <div class="matewa-agency-client-pack-form-head">
                <div>
                    <strong>Client setup request pack</strong>
                    <span>Send or copy the setup request the client fills in. PDF download and parsing stay parked; completed PDFs can now be stored safely in the Client Setup PDF Upload panel.</span>
                </div>
                <em>Agency action</em>
            </div>
            <div class="matewa-agency-client-pack-grid is-details">
                <label>
                    <span>Client contact name</span>
                    <input type="text" name="matewa_setup_request_client_name" placeholder="e.g. Jim" value="<?php echo esc_attr($matewa_agency_setup_request_client_name); ?>" data-matewa-setup-request-field="name" autocomplete="name">
                </label>
                <label>
                    <span>Client admin email</span>
                    <input type="email" name="matewa_setup_request_client_email" placeholder="e.g. admin@client.co.uk" value="<?php echo esc_attr($matewa_agency_setup_request_client_email); ?>" data-matewa-setup-request-field="email" autocomplete="email">
                </label>
                <label>
                    <span>Client domain</span>
                    <input type="text" name="matewa_setup_request_client_domain" placeholder="e.g. client.co.uk" value="<?php echo esc_attr($matewa_agency_setup_request_client_domain); ?>" data-matewa-setup-request-field="domain" autocomplete="url">
                </label>
            </div>
            <label class="matewa-agency-setup-request-message">
                <span>Setup request message</span>
                <textarea readonly rows="9" data-matewa-setup-request-message><?php echo esc_textarea($matewa_agency_setup_request_message); ?></textarea>
            </label>
            <div class="matewa-agency-setup-request-actions">
                <button type="button" class="button button-secondary" data-matewa-copy-setup-request>Copy setup message</button>
                <button type="submit" class="button button-primary matewa-agency-send-now-button" name="matewa_agency_setup_request_action" value="send_request">Send now</button>
                <button type="button" class="button button-secondary" disabled aria-disabled="true">Download blank setup PDF after signoff</button>
                <a class="button button-secondary" href="#matewa-agency-client-pdf-upload-panel">Open PDF upload panel</a>
                <span class="matewa-agency-client-row-note" data-matewa-copy-setup-request-status aria-live="polite"></span>
            </div>
            <div class="matewa-agency-setup-request-dropzone" aria-label="Completed setup PDF upload note">
                <strong>Completed setup PDFs</strong>
                <span>Use the Client Setup PDF Upload panel below to store the completed PDF against a saved setup pack. Parsing and field extraction are still a later checkpoint.</span>
            </div>
        </div>

        <div class="matewa-agency-hub-stats" aria-label="Agency foundation status cards">
            <article class="matewa-agency-hub-stat-card <?php echo $matewa_agency_routing_ready ? esc_attr('is-ready') : esc_attr('is-warning'); ?>">
                <span>Email routing</span>
                <strong><?php echo $matewa_agency_routing_ready ? esc_html('Ready') : esc_html('Needs setup'); ?></strong>
                <small>Agency admin + roundup inboxes</small>
            </article>
            <article class="matewa-agency-hub-stat-card">
                <span>Weekly roundup</span>
                <strong><?php echo esc_html((string) $matewa_agency_weekly_ready_count); ?></strong>
                <small>Client packs ready for future roundup</small>
            </article>
            <article class="matewa-agency-hub-stat-card <?php echo $matewa_agency_duplicate_pack_count > 0 ? esc_attr('is-warning') : esc_attr('is-ready'); ?>">
                <span>Review flags</span>
                <strong><?php echo esc_html((string) $matewa_agency_duplicate_pack_count); ?></strong>
                <small>Duplicate warnings only; not hard-blocked here</small>
            </article>
            <article class="matewa-agency-hub-stat-card">
                <span>Grace flow</span>
                <strong>Advisory</strong>
                <small>28-day flow placeholder; no client messaging yet</small>
            </article>
        </div>
    </section>
