<?php
if (function_exists('epw_fs') && !epw_fs()->can_use_premium_code__premium_only()) :
    ?>
    <div class="wrap">
        <h1><?php esc_html_e('Metrics', 'nexdirectory'); ?></h1>
        <p><?php esc_html_e('Metrics dashboard is available on premium plans only.', 'nexdirectory'); ?></p>
    </div>
    <?php
    return;
endif;

$brand_name = $this->get_white_label_brand_name();
$days = isset($_GET['eep_days']) ? absint((int) wp_unslash($_GET['eep_days'])) : 30;
if (!in_array($days, [7, 30, 90, 365], true)) {
    $days = 30;
}
$report = $this->get_platform_analytics_report($days);
$summary = is_array($report['summary'] ?? null) ? $report['summary'] : [];
$most_viewed = is_array($report['most_viewed_listings'] ?? null) ? $report['most_viewed_listings'] : [];
$most_searched = is_array($report['most_searched_categories'] ?? null) ? $report['most_searched_categories'] : [];
$most_active_areas = is_array($report['most_active_areas'] ?? null) ? $report['most_active_areas'] : [];
$top_businesses = is_array($report['top_performing_businesses'] ?? null) ? $report['top_performing_businesses'] : [];
$growth = is_array($report['growth'] ?? null) ? $report['growth'] : [];
$ui_labels = method_exists($this, 'get_site_profile_ui_labels') ? (array) $this->get_site_profile_ui_labels() : [];
$plural_label = sanitize_text_field((string) ($ui_labels['plural'] ?? __('Events', 'nexdirectory')));
if ($plural_label === '') {
    $plural_label = __('Events', 'nexdirectory');
}
?>
<div class="wrap eep-admin-premium eep-metrics-page">
    <?php $this->render_admin_premium_shell_styles(); ?>
    <section class="eep-admin-hero">
        <span class="eep-admin-hero-kicker"><?php esc_html_e('Performance Dashboard', 'nexdirectory'); ?></span>
        <h1><?php echo esc_html(sprintf(__('%s Metrics', 'nexdirectory'), $brand_name)); ?></h1>
        <p><?php esc_html_e('Track growth, conversion, demand signals, and top-performing listings with export-ready reporting.', 'nexdirectory'); ?></p>
    </section>

    <section class="eep-admin-panel">
        <form method="get" class="eep-admin-toolbar">
            <input type="hidden" name="page" value="eep-metrics">
            <label for="eep_days"><strong><?php esc_html_e('Reporting Window', 'nexdirectory'); ?></strong></label>
            <select id="eep_days" name="eep_days">
                <option value="7" <?php selected($days, 7); ?>><?php esc_html_e('Last 7 days', 'nexdirectory'); ?></option>
                <option value="30" <?php selected($days, 30); ?>><?php esc_html_e('Last 30 days', 'nexdirectory'); ?></option>
                <option value="90" <?php selected($days, 90); ?>><?php esc_html_e('Last 90 days', 'nexdirectory'); ?></option>
                <option value="365" <?php selected($days, 365); ?>><?php esc_html_e('Last 12 months', 'nexdirectory'); ?></option>
            </select>
            <?php submit_button(__('Apply', 'nexdirectory'), 'secondary', '', false); ?>
        </form>
        <div class="eep-admin-grid" style="margin-bottom:14px;">
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Total Listings', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['total_listings'] ?? 0))); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Total Users', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['total_users'] ?? 0))); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Listing Views', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['listing_views'] ?? 0))); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Enquiries', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['enquiries_received'] ?? 0))); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('View → Enquiry', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((float) ($summary['view_to_enquiry_conversion'] ?? 0))); ?>%</p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Revenue (Estimated)', 'nexdirectory'); ?></p><p class="eep-admin-stat-value">$<?php echo esc_html(number_format((float) ($summary['revenue_estimate'] ?? 0), 2)); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Boosts Sold', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['boosts_sold'] ?? 0))); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Boost Revenue', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ($summary['boost_revenue'] ?? 0)); ?> <?php echo esc_html((string) get_option('eep_boost_currency', 'USD')); ?></p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Renewal Rate', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((float) ($summary['renewal_rate'] ?? 0))); ?>%</p></div>
            <div class="eep-admin-stat-card"><p class="eep-admin-stat-label"><?php esc_html_e('Active Boosted', 'nexdirectory'); ?></p><p class="eep-admin-stat-value"><?php echo esc_html((string) ((int) ($summary['active_boosted_listings'] ?? 0))); ?></p></div>
        </div>
        <div class="eep-admin-table-wrap" style="margin-bottom:12px;">
            <table class="widefat striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Metric', 'nexdirectory'); ?></th>
                        <th><?php esc_html_e('Value', 'nexdirectory'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <tr><td><?php esc_html_e('Total Submissions', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['total_submissions'] ?? 0))); ?></td></tr>
                    <tr><td><?php esc_html_e('Approved (Published)', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['approved'] ?? 0))); ?></td></tr>
                    <tr><td><?php esc_html_e('Pending Review', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['pending'] ?? 0))); ?></td></tr>
                    <tr><td><?php echo esc_html(sprintf(__('Imported %s (all time)', 'nexdirectory'), $plural_label)); ?></td><td><?php echo esc_html((string) ((int) ($summary['imports'] ?? 0))); ?></td></tr>
                    <tr><td><?php esc_html_e('Spam Blocks', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['spam_blocks'] ?? 0))); ?></td></tr>
                    <tr><td><?php esc_html_e('Approval Conversion', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((float) ($summary['approval_conversion'] ?? 0))); ?>%</td></tr>
                    <tr><td><?php esc_html_e('Boosts Sold', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['boosts_sold'] ?? 0))); ?></td></tr>
                    <tr><td><?php esc_html_e('Boost Revenue', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ($summary['boost_revenue'] ?? 0)); ?> <?php echo esc_html((string) get_option('eep_boost_currency', 'USD')); ?></td></tr>
                    <tr><td><?php esc_html_e('Renewal Rate', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((float) ($summary['renewal_rate'] ?? 0))); ?>%</td></tr>
                    <tr><td><?php esc_html_e('Active Boosted Listings', 'nexdirectory'); ?></td><td><?php echo esc_html((string) ((int) ($summary['active_boosted_listings'] ?? 0))); ?></td></tr>
                </tbody>
            </table>
        </div>
        <div class="eep-admin-toolbar" style="margin:12px 0 2px;">
            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="eep-admin-toolbar">
                <input type="hidden" name="action" value="eep_export_platform_analytics">
                <?php wp_nonce_field('eep_export_platform_analytics_nonce', 'eep_export_platform_analytics_nonce'); ?>
                <input type="hidden" name="eep_days" value="<?php echo esc_attr((string) $days); ?>">
                <label for="eep_platform_analytics_format" class="screen-reader-text"><?php esc_html_e('Export format', 'nexdirectory'); ?></label>
                <select id="eep_platform_analytics_format" name="eep_export_format">
                    <option value="csv"><?php esc_html_e('CSV', 'nexdirectory'); ?></option>
                    <option value="json"><?php esc_html_e('JSON', 'nexdirectory'); ?></option>
                </select>
                <?php submit_button(__('Export Metrics Report', 'nexdirectory'), 'secondary', 'submit', false); ?>
            </form>
        </div>
    </section>

    <section class="eep-admin-panel">
        <h2><span class="dashicons dashicons-chart-line"></span><?php esc_html_e('Growth Over Time', 'nexdirectory'); ?></h2>
        <div class="eep-admin-table-wrap">
            <table class="widefat striped">
                <thead><tr><th><?php esc_html_e('Month', 'nexdirectory'); ?></th><th><?php esc_html_e('Listings', 'nexdirectory'); ?></th><th><?php esc_html_e('Users', 'nexdirectory'); ?></th></tr></thead>
                <tbody>
                    <?php if (!empty($growth)) : ?>
                        <?php foreach ($growth as $row) : ?>
                            <tr>
                                <td><?php echo esc_html((string) ($row['label'] ?? '')); ?></td>
                                <td><?php echo esc_html((string) ((int) ($row['listings'] ?? 0))); ?></td>
                                <td><?php echo esc_html((string) ((int) ($row['users'] ?? 0))); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr><td colspan="3"><?php esc_html_e('No growth data available yet.', 'nexdirectory'); ?></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </section>

    <section class="eep-admin-panel">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(340px,1fr));gap:14px;">
            <div>
                <h2><?php esc_html_e('Most Viewed Listings', 'nexdirectory'); ?></h2>
                <div class="eep-admin-table-wrap"><table class="widefat striped">
                    <thead><tr><th><?php esc_html_e('Listing', 'nexdirectory'); ?></th><th><?php esc_html_e('Views', 'nexdirectory'); ?></th></tr></thead>
                    <tbody>
                        <?php if (!empty($most_viewed)) : ?>
                            <?php foreach ($most_viewed as $row) : ?>
                                <tr>
                                    <td><a href="<?php echo esc_url((string) ($row['url'] ?? '#')); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html((string) ($row['title'] ?? ('#' . (string) ($row['event_id'] ?? '')))); ?></a></td>
                                    <td><?php echo esc_html((string) ((int) ($row['total'] ?? 0))); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr><td colspan="2"><?php esc_html_e('No view data yet.', 'nexdirectory'); ?></td></tr>
                        <?php endif; ?>
                    </tbody>
                </table></div>
            </div>

            <div>
                <h2><?php esc_html_e('Most Searched Categories', 'nexdirectory'); ?></h2>
                <div class="eep-admin-table-wrap"><table class="widefat striped">
                    <thead><tr><th><?php esc_html_e('Category', 'nexdirectory'); ?></th><th><?php esc_html_e('Searches', 'nexdirectory'); ?></th></tr></thead>
                    <tbody>
                        <?php if (!empty($most_searched)) : ?>
                            <?php foreach ($most_searched as $row) : ?>
                                <tr>
                                    <td><?php echo esc_html((string) ($row['label'] ?? '')); ?></td>
                                    <td><?php echo esc_html((string) ((int) ($row['total'] ?? 0))); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr><td colspan="2"><?php esc_html_e('No category search data yet.', 'nexdirectory'); ?></td></tr>
                        <?php endif; ?>
                    </tbody>
                </table></div>
            </div>

            <div>
                <h2><?php esc_html_e('Most Active Map Areas', 'nexdirectory'); ?></h2>
                <div class="eep-admin-table-wrap"><table class="widefat striped">
                    <thead><tr><th><?php esc_html_e('Area Bucket (lat,lng)', 'nexdirectory'); ?></th><th><?php esc_html_e('Searches', 'nexdirectory'); ?></th></tr></thead>
                    <tbody>
                        <?php if (!empty($most_active_areas)) : ?>
                            <?php foreach ($most_active_areas as $row) : ?>
                                <tr>
                                    <td><?php echo esc_html((string) ($row['bucket'] ?? '')); ?></td>
                                    <td><?php echo esc_html((string) ((int) ($row['total'] ?? 0))); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr><td colspan="2"><?php esc_html_e('No map-area search data yet.', 'nexdirectory'); ?></td></tr>
                        <?php endif; ?>
                    </tbody>
                </table></div>
            </div>

            <div>
                <h2><?php esc_html_e('Top Performing Businesses', 'nexdirectory'); ?></h2>
                <div class="eep-admin-table-wrap"><table class="widefat striped">
                    <thead><tr><th><?php esc_html_e('Owner', 'nexdirectory'); ?></th><th><?php esc_html_e('Views', 'nexdirectory'); ?></th><th><?php esc_html_e('Enquiries', 'nexdirectory'); ?></th><th><?php esc_html_e('Conversion', 'nexdirectory'); ?></th></tr></thead>
                    <tbody>
                        <?php if (!empty($top_businesses)) : ?>
                            <?php foreach ($top_businesses as $row) : ?>
                                <tr>
                                    <td><?php echo esc_html((string) ($row['owner_name'] ?? '')); ?></td>
                                    <td><?php echo esc_html((string) ((int) ($row['listing_views'] ?? 0))); ?></td>
                                    <td><?php echo esc_html((string) ((int) ($row['enquiries_received'] ?? 0))); ?></td>
                                    <td><?php echo esc_html((string) ((float) ($row['conversion_rate'] ?? 0))); ?>%</td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr><td colspan="4"><?php esc_html_e('No owner performance data yet.', 'nexdirectory'); ?></td></tr>
                        <?php endif; ?>
                    </tbody>
                </table></div>
            </div>
        </div>
    </section>
</div>
