<?php

if (!defined('ABSPATH')) {
    exit;
}

add_action('wp_dashboard_setup', function () {

    wp_add_dashboard_widget(
        'wnc_dashboard',
        'Where Next',
        'wnc_render_dashboard_widget'
    );

});

function wnc_render_dashboard_widget() {

    $options = wnc_get_options();

    global $wpdb;

    $table = $wpdb->prefix . 'wnc_clicks';

    $clicks = 0;

    if ($wpdb->get_var($wpdb->prepare(
        "SHOW TABLES LIKE %s",
        $table
    ))) {

        $clicks = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$table}"
        );
    }

    ?>

    <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">

        <div style="background:#f8fafc;padding:16px;border-radius:12px;">

            <strong>Button Label</strong>

            <div style="font-size:18px;margin-top:6px;">

                <?php echo esc_html(
                    $options['button_label'] ?? 'Send to Where Next'
                ); ?>

            </div>

        </div>

        <div style="background:#f8fafc;padding:16px;border-radius:12px;">

            <strong>Total Clicks</strong>

            <div style="font-size:28px;font-weight:700;margin-top:6px;">

                <?php echo esc_html($clicks); ?>

            </div>

        </div>

        <div style="background:#f8fafc;padding:16px;border-radius:12px;">

            <strong>Pro</strong>

            <div style="margin-top:6px;">

                <?php echo function_exists('wnc_is_pro_active') && wnc_is_pro_active()
                    ? '✅ Enabled'
                    : '❌ Disabled'; ?>

            </div>

        </div>

        <div style="background:#f8fafc;padding:16px;border-radius:12px;">

            <strong>Version</strong>

            <div style="margin-top:6px;">

                <?php echo esc_html(WNC_VERSION); ?>

            </div>

        </div>

    </div>

    <?php
}