<?php
/**
 * 247ChatMate — Client View Home Partial
 *
 * Phase 5-A-13C / 5-A-14E / 5-A-14F / 5-A-14G0 / 5-A-14G1 / 5-A-14G3 / 5-A-15 / 5-A-16 / 5-A-16A / 5-A-17 / 5-A-18 / 5-A-19 / 5-A-19D
 * Checkpoint: CLIENTVIEWHOMERENDERSPLIT5A13CLOCK / DASHBOARDLEADSSTATUSTOGGLE5A14ELOCK / HISTORYPANELEXPORTRESET5A14FLOCK / LEADHISTORYLIVEAGGREGATE5A14G0LOCK / PHONEONLYABANDONEDLEAD5A14G1LOCK / WIDGETOPENSHANDOFFRATIO5A14G3LOCK / SETUPHEALTHSTATUSPANEL5A15LOCK / LEADACTIVITYSNAPSHOTCLARITY5A16LOCK / LEADACTIVITYSNAPSHOTSIMPLIFY5A16ALOCK / TOPWIDGETPAGESBETAPANEL5A17LOCK / LEADINBOXVALUEPANEL5A18LOCK / DASHBOARDMOBILEACCESSIBILITYQA5A19LOCK / DASHBOARDREPETITIONRIGHTRAIL5A19DLOCK
 *
 * Purpose:
 * - Renders the Client View dashboard homepage panel.
 *
 * Responsibility:
 * - Keep Client View homepage markup separate from the main admin render file.
 * - Use dashboard values prepared by admin/settings-ui.php.
 *
 * Does:
 * - Render existing Client View homepage HTML.
 * - Render the Leads/Status circle toggle using prepared values.
 * - Render the dynamic right rail for Lead History or Setup Status.
 * - Render a simplified Lead activity rail using existing local aggregate values only.
 * - Render a Top widget pages beta panel using safe local page-path aggregates only.
 * - Render a compact Lead inbox value card using prepared local/admin values.
 * - Preserve accessible regions, toggle controls and mobile-safe layout hooks.
 * - Provide a light refresh link so newly captured aggregate totals can be pulled in without adding AJAX/REST.
 * - Preserve existing classes, links, dashboard variables and escaping.
 *
 * Does not:
 * - Save settings directly.
 * - Change entitlement, trial, registry, widget, chip, phone, lead routing or WhatsApp handoff logic.
 * - Store raw chats, IPs, visitor IDs, remote history or personal tracking data.
 */

defined('ABSPATH') || exit;
?>

                    <div id="matewa-admin-panel-dashboard" class="matewa-admin-panel is-active" data-matewa-admin-panel="dashboard" aria-label="Dashboard overview">
                        <div class="matewa-dashboard-value-card" aria-label="Client View dashboard">
                            <div class="matewa-dashboard-hero-panel">
                                <div class="matewa-dashboard-hero-intro" aria-label="Dashboard summary">
                                    <p><strong>Turn website chats into WhatsApp leads.</strong></p>
                                    <ul aria-label="Dashboard value summary">
                                        <li>See new leads, saved phone numbers and chat activity.</li>
                                        <li>Check your setup is ready.</li>
                                    </ul>
                                </div>

                                <div class="matewa-dashboard-hero-status" aria-label="Current dashboard status" data-matewa-hero-status-pills>
                                    <span class="matewa-dashboard-status-pill <?php echo esc_attr($dashboard_widget_status_class); ?>">
                                        Widget: <?php echo esc_html($dashboard_widget_status_text); ?>
                                    </span>
                                    <span class="matewa-dashboard-status-pill <?php echo esc_attr($dashboard_widget_status_class); ?>">
                                        Plan: <?php echo esc_html($dashboard_plan_text); ?>
                                    </span>
                                    <span class="matewa-dashboard-status-pill <?php echo $dashboard_setup_complete ? 'is-live' : 'is-off'; ?>">
                                        Setup: <?php echo esc_html((string) $dashboard_setup_percent); ?>%
                                    </span>
                                </div>


                                <div class="matewa-dashboard-hero-actions" aria-label="Dashboard quick actions">
                                    <a class="matewa-dashboard-primary-button" href="<?php echo esc_url(home_url('/')); ?>" target="_blank" rel="noopener noreferrer">
                                        View live widget
                                        <span class="dashicons dashicons-external" aria-hidden="true"></span>
                                    </a>
                                    <a class="matewa-dashboard-secondary-button" href="<?php echo esc_url($dashboard_next_action_href); ?>">
                                        <?php echo esc_html($dashboard_next_action_label); ?>
                                    </a>
                                </div>
                            </div>

                            <div class="matewa-dashboard-lead-panel" aria-label="Lead and setup overview" data-matewa-circle-view-default="<?php echo esc_attr($dashboard_default_circle_view ?? 'status'); ?>">
                                <div class="matewa-dashboard-circle-header">
                                    <h3><span data-matewa-circle-heading><?php echo esc_html((string) ($dashboard_circle_views[$dashboard_default_circle_view]['heading'] ?? 'Lead activity')); ?></span> <span class="dashicons dashicons-info-outline" aria-hidden="true"></span></h3>
                                    <div class="matewa-dashboard-circle-toggle" role="group" aria-label="Choose dashboard circle view">
                                        <?php foreach ($dashboard_circle_views as $dashboard_circle_toggle_key => $dashboard_circle_toggle_view) : ?>
                                            <?php
                                            $dashboard_circle_toggle_key = sanitize_key((string) $dashboard_circle_toggle_key);
                                            $dashboard_circle_toggle_sidebar = isset($dashboard_circle_toggle_view['sidebar']) ? sanitize_key((string) $dashboard_circle_toggle_view['sidebar']) : '';
                                            $dashboard_circle_toggle_controls = 'matewa-circle-view-' . $dashboard_circle_toggle_key;
                                            if ('' !== $dashboard_circle_toggle_sidebar) {
                                                $dashboard_circle_toggle_controls .= ' ' . $dashboard_circle_toggle_sidebar;
                                            }
                                            ?>
                                            <button type="button" class="matewa-dashboard-circle-toggle-button" data-matewa-circle-view-toggle="<?php echo esc_attr($dashboard_circle_toggle_key); ?>" aria-pressed="false" aria-controls="<?php echo esc_attr($dashboard_circle_toggle_controls); ?>"><?php echo esc_html((string) ($dashboard_circle_toggle_view['label'] ?? 'View')); ?></button>
                                        <?php endforeach; ?>
                                    </div>
                                </div>

                                <?php foreach ($dashboard_circle_views as $dashboard_circle_view_key => $dashboard_circle_view) : ?>
                                    <?php
                                    $dashboard_circle_view_key = sanitize_key((string) $dashboard_circle_view_key);
                                    $dashboard_circle_centre = is_array($dashboard_circle_view['centre'] ?? null) ? $dashboard_circle_view['centre'] : [];
                                    $dashboard_circle_orbit = is_array($dashboard_circle_view['orbit'] ?? null) ? $dashboard_circle_view['orbit'] : [];
                                    $dashboard_circle_icon = sanitize_html_class((string) ($dashboard_circle_centre['icon'] ?? 'dashicons-format-chat'));
                                    $dashboard_circle_centre_class = sanitize_html_class((string) ($dashboard_circle_centre['class'] ?? ''));
                                    $dashboard_circle_is_active = $dashboard_circle_view_key === ($dashboard_default_circle_view ?? 'status');
                                    ?>
                                    <div id="matewa-circle-view-<?php echo esc_attr($dashboard_circle_view_key); ?>" class="matewa-dashboard-circle-view <?php echo $dashboard_circle_is_active ? 'is-active' : ''; ?>" data-matewa-circle-view="<?php echo esc_attr($dashboard_circle_view_key); ?>" data-matewa-circle-heading="<?php echo esc_attr((string) ($dashboard_circle_view['heading'] ?? 'Dashboard')); ?>" <?php echo $dashboard_circle_is_active ? '' : 'hidden aria-hidden="true"'; ?>>
                                        <div class="matewa-dashboard-radial" aria-label="<?php echo esc_attr((string) ($dashboard_circle_view['aria'] ?? 'Dashboard metrics')); ?>">
                                            <div class="matewa-dashboard-radial-center <?php echo esc_attr($dashboard_circle_centre_class); ?>">
                                                <span class="matewa-dashboard-radial-icon dashicons <?php echo esc_attr($dashboard_circle_icon); ?>" aria-hidden="true"></span>
                                                <strong><?php echo esc_html((string) ($dashboard_circle_centre['value'] ?? '0')); ?></strong>
                                                <span class="matewa-dashboard-radial-label"><?php echo esc_html((string) ($dashboard_circle_centre['label'] ?? 'Dashboard')); ?></span>
                                                <span class="matewa-dashboard-radial-trend is-<?php echo esc_attr(sanitize_html_class((string) ($dashboard_circle_centre['tone'] ?? 'up'))); ?>"><?php echo esc_html((string) ($dashboard_circle_centre['trend'] ?? '')); ?></span>
                                            </div>

                                            <?php foreach ($dashboard_circle_orbit as $dashboard_index => $dashboard_metric) : ?>
                                                <?php
                                                $dashboard_metric_tone = sanitize_html_class((string) ($dashboard_metric['tone'] ?? 'up'));
                                                $dashboard_metric_classes = 'matewa-dashboard-orbit-item is-position-' . (string) ($dashboard_index + 1) . ' is-' . $dashboard_metric_tone;
                                                $dashboard_metric_href = isset($dashboard_metric['href']) ? trim((string) $dashboard_metric['href']) : '';
                                                ?>
                                                <?php if ('' !== $dashboard_metric_href) : ?>
                                                    <a class="<?php echo esc_attr($dashboard_metric_classes . ' is-clickable'); ?>" href="<?php echo esc_url($dashboard_metric_href); ?>">
                                                        <strong class="matewa-dashboard-metric-value"><?php echo esc_html((string) ($dashboard_metric['value'] ?? '0')); ?></strong>
                                                        <span class="matewa-dashboard-metric-label"><?php echo esc_html((string) ($dashboard_metric['label'] ?? 'Metric')); ?></span>
                                                        <span class="matewa-dashboard-metric-trend"><?php echo esc_html((string) ($dashboard_metric['trend'] ?? '')); ?></span>
                                                    </a>
                                                <?php else : ?>
                                                    <div class="<?php echo esc_attr($dashboard_metric_classes); ?>">
                                                        <strong class="matewa-dashboard-metric-value"><?php echo esc_html((string) ($dashboard_metric['value'] ?? '0')); ?></strong>
                                                        <span class="matewa-dashboard-metric-label"><?php echo esc_html((string) ($dashboard_metric['label'] ?? 'Metric')); ?></span>
                                                        <span class="matewa-dashboard-metric-trend"><?php echo esc_html((string) ($dashboard_metric['trend'] ?? '')); ?></span>
                                                    </div>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>

                            <aside class="matewa-dashboard-setup-panel" aria-label="Dashboard detail panel" data-matewa-circle-sidebar>
                                <?php
                                $dashboard_plan_nudge_button_text = isset($dashboard_plan_nudge_label) ? (string) $dashboard_plan_nudge_label : 'Upgrade now';
                                if ('convert' === strtolower(trim($dashboard_plan_nudge_button_text))) {
                                    $dashboard_plan_nudge_button_text = 'Upgrade now';
                                }
                                ?>
                                <?php $dashboard_plan_nudge_opens_new_tab = !isset($dashboard_plan_nudge_new_tab) || !empty($dashboard_plan_nudge_new_tab); ?>
                                <a class="matewa-dashboard-plan-nudge <?php echo esc_attr($dashboard_plan_nudge_tone ?? 'is-free'); ?>" href="<?php echo esc_url($dashboard_plan_upgrade_url ?? 'https://247chatmate.lemonsqueezy.com/'); ?>" <?php echo $dashboard_plan_nudge_opens_new_tab ? 'target="_blank" rel="noopener noreferrer"' : ''; ?> aria-label="<?php echo esc_attr($dashboard_plan_nudge_opens_new_tab ? 'Open LemonSqueezy plan checkout' : 'Open Agency Hub'); ?>">
                                    <span><?php echo esc_html($dashboard_plan_nudge_title ?? 'Upgrade to Pro'); ?></span>
                                    <strong><?php echo esc_html($dashboard_plan_nudge_value ?? 'Ready when you are'); ?></strong>
                                    <small><?php echo esc_html($dashboard_plan_nudge_detail ?? 'Open LemonSqueezy to choose your plan.'); ?></small>
                                    <em><?php echo esc_html($dashboard_plan_nudge_button_text); ?></em>
                                </a>

                                <div id="matewa-dashboard-history-panel" class="matewa-dashboard-sidebar-view <?php echo ('leads' === ($dashboard_default_circle_view ?? 'status')) ? 'is-active' : ''; ?>" data-matewa-circle-sidebar-view="leads" aria-label="Lead export" role="region" <?php echo ('leads' === ($dashboard_default_circle_view ?? 'status')) ? '' : 'hidden aria-hidden="true"'; ?>>
                                    <div class="matewa-dashboard-export-card" aria-label="Lead CSV export">
                                        <h3>Lead export</h3>
                                        <p>Download local lead totals when you need a simple reporting copy.</p>
                                        <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>">
                                            <?php wp_nonce_field('matewa_history_controls_nonce', 'matewa_history_controls_nonce'); ?>
                                            <input type="hidden" name="matewa_history_action" value="download_csv">
                                            <button type="submit" class="matewa-dashboard-history-button" aria-label="Download lead history CSV">Download CSV</button>
                                        </form>
                                    </div>
                                </div>

                                <div id="matewa-circle-sidebar-status" class="matewa-dashboard-sidebar-view <?php echo ('status' === ($dashboard_default_circle_view ?? 'status')) ? 'is-active' : ''; ?>" data-matewa-circle-sidebar-view="status" aria-label="Setup status" role="region" <?php echo ('status' === ($dashboard_default_circle_view ?? 'status')) ? '' : 'hidden aria-hidden="true"'; ?>>
                                    <div class="matewa-dashboard-status-action-card <?php echo $dashboard_setup_complete ? 'is-complete' : 'is-needed'; ?>" aria-label="Setup status and next action">
                                        <section class="matewa-dashboard-status-action-section" aria-label="Setup status">
                                            <h3>Setup status</h3>
                                            <div class="matewa-dashboard-status-action-summary">
                                                <span class="dashicons <?php echo $dashboard_setup_complete ? 'dashicons-yes-alt' : 'dashicons-warning'; ?>" aria-hidden="true"></span>
                                                <div>
                                                    <strong><?php echo esc_html($dashboard_setup_status_title); ?></strong>
                                                    <p><?php echo esc_html($dashboard_setup_status_copy); ?></p>
                                                </div>
                                            </div>

                                            <div class="matewa-dashboard-status-action-progress" aria-label="Setup progress: <?php echo esc_attr((string) $dashboard_setup_percent); ?> percent complete">
                                                <span style="width: <?php echo esc_attr((string) $dashboard_setup_percent); ?>%;"></span>
                                            </div>
                                        </section>

                                        <section class="matewa-dashboard-next-action-section" aria-label="Next action">
                                            <span class="dashicons dashicons-lightbulb" aria-hidden="true"></span>
                                            <div>
                                                <small>Next action</small>
                                                <strong><?php echo esc_html($dashboard_next_action_label); ?></strong>
                                                <p><?php echo esc_html($dashboard_next_action_copy); ?></p>
                                            </div>
                                        </section>

                                        <a class="matewa-dashboard-status-action-button" href="<?php echo esc_url($dashboard_next_action_href); ?>">
                                            <?php echo esc_html($dashboard_next_action_label); ?>
                                            <span class="dashicons dashicons-external" aria-hidden="true"></span>
                                        </a>
                                    </div>
                                </div>

                                <?php if (!empty($dashboard_agency_active)) : ?>
                                    <div id="matewa-circle-sidebar-clients" class="matewa-dashboard-sidebar-view <?php echo ('clients' === ($dashboard_default_circle_view ?? 'status')) ? 'is-active' : ''; ?>" data-matewa-circle-sidebar-view="clients" aria-label="Agency client health" role="region" <?php echo ('clients' === ($dashboard_default_circle_view ?? 'status')) ? '' : 'hidden aria-hidden="true"'; ?>>
                                        <div class="matewa-dashboard-status-action-card is-needed matewa-dashboard-agency-client-card" aria-label="Agency client portfolio status">
                                            <section class="matewa-dashboard-status-action-section" aria-label="Agency client setup status">
                                                <h3>Client portfolio</h3>
                                                <div class="matewa-dashboard-status-action-summary">
                                                    <span class="dashicons dashicons-groups" aria-hidden="true"></span>
                                                    <div>
                                                        <strong><?php echo esc_html((string) ($dashboard_agency_client_used ?? 0)); ?> / <?php echo esc_html((string) ($dashboard_agency_client_total ?? 0)); ?> seats used</strong>
                                                        <p><?php echo !empty($dashboard_agency_client_total) ? esc_html((string) ($dashboard_agency_client_available ?? 0) . ' client seats available.') : esc_html('Buy an agency bundle before assigning client seats.'); ?></p>
                                                    </div>
                                                </div>
                                            </section>

                                            <section class="matewa-dashboard-next-action-section" aria-label="Agency next action">
                                                <span class="dashicons dashicons-admin-network" aria-hidden="true"></span>
                                                <div>
                                                    <small>Next action</small>
                                                    <strong>Build client setup packs</strong>
                                                    <p>Manual client entry is active and client-site setup JSON export is now available in Agency Hub. Import mapping and expiry alerts remain next checkpoints.</p>
                                                </div>
                                            </section>

                                            <a class="matewa-dashboard-status-action-button" href="#matewa-agency-hub-section">
                                                Open Agency Hub
                                                <span class="dashicons dashicons-external" aria-hidden="true"></span>
                                            </a>
                                            <a class="matewa-dashboard-status-action-button is-secondary" href="<?php echo esc_url(function_exists('matewa_agency_bundle_get_default_checkout_url') ? matewa_agency_bundle_get_default_checkout_url() : 'https://247chatmate.lemonsqueezy.com/'); ?>" target="_blank" rel="noopener noreferrer">
                                                Buy client licences
                                                <span class="dashicons dashicons-external" aria-hidden="true"></span>
                                            </a>
                                        </div>
                                    </div>
                                <?php endif; ?>

                            </aside>
                        </div>


                    </div>
