<?php
/**
 * Admin extras: dashboard widget, onboarding checklist, admin bar, help tabs, site health, action links.
 *
 * Used as a trait by the main plugin class to keep the bootstrap file lean.
 *
 * @package My Trail Camera
 * @since 8.2.0
 */

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

trait Mtcam_Admin_Extras {

    /**
     * Add Settings link on Plugins page.
     *
     * @param array $links Existing action links.
     * @return array
     */
    public function add_action_links( $links ) {
        $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=mtcam-settings' ) ) . '">'
            . esc_html__( 'Settings', 'my-trail-camera' ) . '</a>';
        array_unshift( $links, $settings_link );
        return $links;
    }

    // ========================================================================
    // ONBOARDING CHECKLIST
    // ========================================================================

    /**
     * Render dynamic onboarding checklist on plugin admin pages.
     */
    public function render_onboarding_checklist() {
        // Only on plugin dashboard page.
        $screen = get_current_screen();
        if ( ! $screen || strpos( $screen->id, 'mtcam-dashboard' ) === false ) {
            return;
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        // Dismissed by user.
        if ( get_option( 'mtcam_onboarding_dismissed', false ) ) {
            return;
        }

        // Check steps.
        $has_email = ( 'yes' === get_option( 'mtcam_notifications', 'yes' ) && '' !== get_option( 'mtcam_notify_email', '' ) );
        $cameras = mtcam_get_cameras();
        $has_camera = ! empty( $cameras );
        $has_tested = false;
        if ( $has_camera ) {
            foreach ( $cameras as $cam ) {
                if ( mtcam_is_tested( $cam ) ) {
                    $has_tested = true;
                    break;
                }
            }
        }

        // All done — auto-hide and stop showing.
        if ( $has_email && $has_camera && $has_tested ) {
            update_option( 'mtcam_onboarding_dismissed', true );
            return;
        }

        $steps = array(
            array(
                'done'  => $has_email,
                'label' => __( 'Set up notification email', 'my-trail-camera' ),
                'desc'  => __( 'Get alerted when a bait file is triggered by a thief.', 'my-trail-camera' ),
                'url'   => admin_url( 'admin.php?page=mtcam-settings' ),
                'link'  => __( 'Go to Settings', 'my-trail-camera' ),
            ),
            array(
                'done'  => $has_camera,
                'label' => __( 'Add your first camera', 'my-trail-camera' ),
                'desc'  => __( 'Register a camera to generate bait files and track its deployment.', 'my-trail-camera' ),
                'url'   => admin_url( 'admin.php?page=mtcam-add-camera' ),
                'link'  => __( 'Add Camera', 'my-trail-camera' ),
            ),
            array(
                'done'  => $has_tested,
                'label' => __( 'Test your bait file', 'my-trail-camera' ),
                'desc'  => __( 'Download the bait ZIP, extract to SD card, and open a file on your computer to verify it works.', 'my-trail-camera' ),
                'url'   => admin_url( 'admin.php?page=mtcam-dashboard' ),
                'link'  => __( 'Go to Dashboard', 'my-trail-camera' ),
            ),
        );

        $done_count = ( $has_email ? 1 : 0 ) + ( $has_camera ? 1 : 0 ) + ( $has_tested ? 1 : 0 );
        ?>
        <div class="notice notice-info" id="mtcam-onboarding" style="padding:12px 16px;border-left-color:#2271b1;">
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
                <strong style="font-size:14px;">
                    <?php esc_html_e( 'My Trail Camera — Getting Started', 'my-trail-camera' ); ?>
                    <span style="color:#888;font-weight:normal;margin-left:8px;">(<?php echo esc_html( $done_count ); ?>/3)</span>
                </strong>
                <button type="button" class="notice-dismiss" id="mtcam-onboarding-dismiss" title="<?php esc_attr_e( 'Dismiss', 'my-trail-camera' ); ?>">
                    <span class="screen-reader-text"><?php esc_html_e( 'Dismiss', 'my-trail-camera' ); ?></span>
                </button>
            </div>
            <ol style="margin:0 0 0 18px;padding:0;">
            <?php foreach ( $steps as $step ) : ?>
                <li style="margin:6px 0;<?php echo $step['done'] ? 'color:#888;' : ''; ?>">
                    <?php if ( $step['done'] ) : ?>
                        <span style="color:#00a32a;font-weight:bold;margin-right:4px;">&#10003;</span>
                        <s><?php echo esc_html( $step['label'] ); ?></s>
                    <?php else : ?>
                        <span style="margin-right:4px;">&#9744;</span>
                        <?php echo esc_html( $step['label'] ); ?>
                        <span style="color:#666;"> — <?php echo esc_html( $step['desc'] ); ?></span>
                        <a href="<?php echo esc_url( $step['url'] ); ?>" style="margin-left:6px;"><?php echo esc_html( $step['link'] ); ?> &rarr;</a>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
            </ol>
        </div>
        <?php
        $mtc_dismiss_js = 'document.getElementById("mtcam-onboarding-dismiss").addEventListener("click", function() {'
            . 'document.getElementById("mtcam-onboarding").remove();'
            . 'var xhr = new XMLHttpRequest();'
            . 'xhr.open("POST", "' . esc_url( admin_url( 'admin-ajax.php' ) ) . '");'
            . 'xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");'
            . 'xhr.send("action=mtcam_dismiss_onboarding&_wpnonce=' . esc_js( wp_create_nonce( 'mtcam_dismiss_onboarding' ) ) . '");'
            . '});';
        wp_add_inline_script( 'mtc-admin-js', $mtc_dismiss_js );
        ?>
        <?php
    }

    /**
     * AJAX handler to dismiss onboarding checklist.
     */
    public function ajax_dismiss_onboarding() {
        check_ajax_referer( 'mtcam_dismiss_onboarding' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error();
        }
        update_option( 'mtcam_onboarding_dismissed', true );
        wp_send_json_success();
    }

    // ========================================================================
    // DEPRECATION NOTICE — moving to mytrailcamera.pro
    // ========================================================================

    /**
     * Render plugin "moving to web app" notice on Dashboard, Plugins page, and plugin's own pages.
     */
    public function render_deprecation_notice() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }
        $is_plugin_screen = ( false !== strpos( $screen->id, 'mtcam' ) );
        $allowed_screens  = array( 'dashboard', 'plugins' );
        if ( ! $is_plugin_screen && ! in_array( $screen->id, $allowed_screens, true ) ) {
            return;
        }
        if ( get_user_meta( get_current_user_id(), 'mtcam_deprecation_dismissed', true ) ) {
            return;
        }
        ?>
        <div class="notice notice-info is-dismissible mtcam-deprecation-notice" id="mtcam-deprecation-notice" style="border-left-color:#2271b1;padding:12px 16px;">
            <p style="margin:0 0 6px;font-size:14px;">
                <strong><?php esc_html_e( 'My Trail Camera is growing — and moving to its own home.', 'my-trail-camera' ); ?></strong>
            </p>
            <p style="margin:0;">
                <?php
                printf(
                    wp_kses(
                        /* translators: %s: link to mytrailcamera.pro */
                        __( 'We\'ve decided to give the project its own space at %s — a standalone web app with more flexibility than a WordPress plugin can offer: faster updates, no WordPress required, and room to grow well beyond what fits inside wp-admin. This plugin will keep working for existing installations but will not receive further updates.', 'my-trail-camera' ),
                        array(
                            'a'      => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
                            'strong' => array(),
                        )
                    ),
                    '<a href="https://mytrailcamera.pro/" target="_blank" rel="noopener"><strong>mytrailcamera.pro</strong></a>'
                );
                ?>
            </p>
        </div>
        <?php
        $mtc_dismiss_js = 'document.addEventListener("click", function(e) {'
            . 'var n = e.target && e.target.closest && e.target.closest("#mtcam-deprecation-notice .notice-dismiss");'
            . 'if (!n) { return; }'
            . 'var xhr = new XMLHttpRequest();'
            . 'xhr.open("POST", "' . esc_url( admin_url( 'admin-ajax.php' ) ) . '");'
            . 'xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");'
            . 'xhr.send("action=mtcam_dismiss_deprecation&_wpnonce=' . esc_js( wp_create_nonce( 'mtcam_dismiss_deprecation' ) ) . '");'
            . '});';
        wp_add_inline_script( 'common', $mtc_dismiss_js );
    }

    /**
     * AJAX handler to persist deprecation notice dismissal per user.
     */
    public function ajax_dismiss_deprecation() {
        check_ajax_referer( 'mtcam_dismiss_deprecation' );
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error();
        }
        update_user_meta( get_current_user_id(), 'mtcam_deprecation_dismissed', 1 );
        wp_send_json_success();
    }

    // ========================================================================
    // ADMIN BAR
    // ========================================================================

    /**
     * Add plugin node to the WordPress admin bar.
     *
     * @param WP_Admin_Bar $admin_bar Admin bar instance.
     */
    public function add_admin_bar_node( $admin_bar ) {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        global $wpdb;
        // Count detections in the last 24 hours.
        $recent = (int) mtcam_db_get_var(
            "SELECT COUNT(*) FROM `{$wpdb->prefix}mtcam_detections` WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)"
        );

        $title = 'MTC';
        if ( $recent > 0 ) {
            $title .= ' <span class="ab-label" style="background:#d63638;color:#fff;padding:0 6px;border-radius:10px;font-size:11px;margin-left:4px;">'
                . $recent . '</span>';
        }

        $admin_bar->add_node( array(
            'id'    => 'my-trail-camera',
            'title' => $title,
            'href'  => esc_url( admin_url( 'admin.php?page=mtcam-dashboard' ) ),
            'meta'  => array( 'title' => __( 'My Trail Camera', 'my-trail-camera' ) ),
        ) );
        $admin_bar->add_node( array(
            'parent' => 'my-trail-camera',
            'id'     => 'mtc-dashboard',
            'title'  => __( 'Dashboard', 'my-trail-camera' ),
            'href'   => esc_url( admin_url( 'admin.php?page=mtcam-dashboard' ) ),
        ) );
        $admin_bar->add_node( array(
            'parent' => 'my-trail-camera',
            'id'     => 'mtc-detections',
            'title'  => __( 'Theft Alerts', 'my-trail-camera' ),
            'href'   => esc_url( admin_url( 'admin.php?page=mtcam-logs' ) ),
        ) );
        $admin_bar->add_node( array(
            'parent' => 'my-trail-camera',
            'id'     => 'mtc-add-camera',
            'title'  => __( 'Add Camera', 'my-trail-camera' ),
            'href'   => esc_url( admin_url( 'admin.php?page=mtcam-add-camera' ) ),
        ) );
        $admin_bar->add_node( array(
            'parent' => 'my-trail-camera',
            'id'     => 'mtc-settings',
            'title'  => __( 'Settings', 'my-trail-camera' ),
            'href'   => esc_url( admin_url( 'admin.php?page=mtcam-settings' ) ),
        ) );
    }

    // ========================================================================
    // HELP TABS
    // ========================================================================

    /**
     * Add contextual help tabs to plugin admin pages.
     */
    public function add_help_tabs() {
        $screen = get_current_screen();
        if ( ! $screen || strpos( $screen->id, 'mtcam' ) === false ) {
            return;
        }

        $page = '';
        if ( strpos( $screen->id, 'mtcam-dashboard' ) !== false ) {
            $page = 'dashboard';
        } elseif ( strpos( $screen->id, 'mtcam-add-camera' ) !== false ) {
            $page = 'add-camera';
        } elseif ( strpos( $screen->id, 'mtcam-logs' ) !== false ) {
            $page = 'detections';
        } elseif ( strpos( $screen->id, 'mtcam-locations' ) !== false ) {
            $page = 'locations';
        } elseif ( strpos( $screen->id, 'mtcam-settings' ) !== false ) {
            $page = 'settings';
        }

        if ( 'dashboard' === $page ) {
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-overview',
                'title'   => __( 'Overview', 'my-trail-camera' ),
                'content' => '<p>' . __( 'The Dashboard shows all your registered cameras. Click on a camera to see details, edit, download bait files, or check battery estimation.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'Camera status icons indicate whether a camera is active, tested, or reported stolen.', 'my-trail-camera' ) . '</p>',
            ) );
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-bait',
                'title'   => __( 'Bait Files', 'my-trail-camera' ),
                'content' => '<p>' . __( 'Bait files are honeypot files placed on your SD card. When a thief opens them, the files silently collect device fingerprints (IP address, GPS, browser info) and send them to your site.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'Download the bait ZIP from camera detail and extract it to the root of your SD card.', 'my-trail-camera' ) . '</p>',
            ) );
        }

        if ( 'add-camera' === $page ) {
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-add',
                'title'   => __( 'Adding a Camera', 'my-trail-camera' ),
                'content' => '<p>' . __( 'Camera ID is a unique identifier for your camera (e.g. serial number). It is used to link detection logs to a specific camera.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'Select a camera model profile to auto-fill battery and power specifications, or enter them manually.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'GPS coordinates are used for location reviews and battery estimation.', 'my-trail-camera' ) . '</p>',
            ) );
        }

        if ( 'detections' === $page ) {
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-detections',
                'title'   => __( 'Theft Alerts', 'my-trail-camera' ),
                'content' => '<p>' . __( 'When someone opens a bait file from a stolen SD card, a detection record is created here with their device fingerprint.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'Each detection includes a recovery chance score based on the quality of collected data (GPS accuracy, device fingerprint).', 'my-trail-camera' ) . '</p>',
            ) );
        }

        if ( 'locations' === $page ) {
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-locations',
                'title'   => __( 'Location Reviews', 'my-trail-camera' ),
                'content' => '<p>' . __( 'Location reviews help you evaluate and score camera deployment spots. Each location gets a letter grade (A-D) based on criteria like accessibility, cover, and wildlife activity.', 'my-trail-camera' ) . '</p>',
            ) );
        }

        if ( 'settings' === $page ) {
            $screen->add_help_tab( array(
                'id'      => 'mtc-help-settings',
                'title'   => __( 'Settings', 'my-trail-camera' ),
                'content' => '<p>' . __( 'Configure notification emails, data retention, public widget visibility, and integration options.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . __( 'Use the Maintenance tab to export/import backups or clean up orphaned data.', 'my-trail-camera' ) . '</p>',
            ) );
        }

        $screen->set_help_sidebar(
            '<p><strong>' . __( 'Useful Links', 'my-trail-camera' ) . '</strong></p>'
            . '<p><a href="' . esc_url( admin_url( 'admin.php?page=mtcam-dashboard' ) ) . '">' . __( 'Dashboard', 'my-trail-camera' ) . '</a></p>'
            . '<p><a href="' . esc_url( admin_url( 'admin.php?page=mtcam-settings' ) ) . '">' . __( 'Settings', 'my-trail-camera' ) . '</a></p>'
            . '<p><a href="https://wordpress.org/plugins/my-trail-camera/" target="_blank">' . __( 'Plugin Page', 'my-trail-camera' ) . '</a></p>'
        );
    }

    // ========================================================================
    // SITE HEALTH
    // ========================================================================

    /**
     * Register Site Health tests.
     *
     * @param array $tests Existing tests.
     * @return array
     */
    public function register_site_health_tests( $tests ) {
        $tests['direct']['my_trail_camera_tables'] = array(
            'label' => __( 'My Trail Camera database tables', 'my-trail-camera' ),
            'test'  => array($this, 'site_health_test_tables'),
        );
        $tests['direct']['my_trail_camera_notifications'] = array(
            'label' => __( 'My Trail Camera notification email', 'my-trail-camera' ),
            'test'  => array($this, 'site_health_test_notifications'),
        );
        $tests['direct']['my_trail_camera_rest_api'] = array(
            'label' => __( 'My Trail Camera REST API', 'my-trail-camera' ),
            'test'  => array($this, 'site_health_test_rest_api'),
        );
        return $tests;
    }

    /**
     * Site Health: check that all required database tables exist.
     *
     * @return array
     */
    public function site_health_test_tables() {
        global $wpdb;
        $required = array(
            'mtcam_detections',
            'mtcam_cameras',
            'mtcam_location_reviews',
        );
        $missing = array();
        foreach ( $required as $table ) {
            $full = $wpdb->prefix . $table;
            $exists = mtcam_db_get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $full ) );
            if ( ! $exists ) {
                $missing[] = $full;
            }
        }
        if ( empty( $missing ) ) {
            return array(
                'label'       => __( 'My Trail Camera database tables are present', 'my-trail-camera' ),
                'status'      => 'good',
                'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'blue' ),
                'description' => '<p>' . __( 'All required database tables exist.', 'my-trail-camera' ) . '</p>',
                'test'        => 'my_trail_camera_tables',
            );
        }
        return array(
            'label'       => __( 'My Trail Camera: missing database tables', 'my-trail-camera' ),
            'status'      => 'critical',
            'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'red' ),
            'description' => '<p>' . sprintf(
                /* translators: %s: comma-separated list of missing tables */
                __( 'The following tables are missing: %s. Try deactivating and reactivating the plugin.', 'my-trail-camera' ),
                '<code>' . implode( '</code>, <code>', array_map( 'esc_html', $missing ) ) . '</code>'
            ) . '</p>',
            'actions'     => '<p><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">' . __( 'Go to Plugins', 'my-trail-camera' ) . '</a></p>',
            'test'        => 'my_trail_camera_tables',
        );
    }

    /**
     * Site Health: check notification email is configured.
     *
     * @return array
     */
    public function site_health_test_notifications() {
        $notifications = get_option( 'mtcam_notifications', 'yes' );
        $email = get_option( 'mtcam_notify_email', '' );
        if ( 'yes' === $notifications && ! empty( $email ) ) {
            return array(
                'label'       => __( 'My Trail Camera notifications are configured', 'my-trail-camera' ),
                'status'      => 'good',
                'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'blue' ),
                'description' => '<p>' . sprintf(
                    /* translators: %s: notification email address */
                    __( 'Detection alerts will be sent to %s.', 'my-trail-camera' ),
                    '<code>' . esc_html( $email ) . '</code>'
                ) . '</p>',
                'test'        => 'my_trail_camera_notifications',
            );
        }
        return array(
            'label'       => __( 'My Trail Camera: notifications not configured', 'my-trail-camera' ),
            'status'      => 'recommended',
            'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'orange' ),
            'description' => '<p>' . __( 'Email notifications for theft detections are not enabled. You may miss alerts when a bait file is triggered.', 'my-trail-camera' ) . '</p>',
            'actions'     => '<p><a href="' . esc_url( admin_url( 'admin.php?page=mtcam-settings' ) ) . '">' . __( 'Configure notifications', 'my-trail-camera' ) . '</a></p>',
            'test'        => 'my_trail_camera_notifications',
        );
    }

    /**
     * Site Health: check REST API endpoint is reachable.
     *
     * @return array
     */
    public function site_health_test_rest_api() {
        $url = rest_url( 'mtcam/v1/log' );
        $response = wp_remote_get( $url, array( 'timeout' => 10, 'sslverify' => false ) );
        if ( is_wp_error( $response ) ) {
            return array(
                'label'       => __( 'My Trail Camera REST API is unreachable', 'my-trail-camera' ),
                'status'      => 'critical',
                'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'red' ),
                'description' => '<p>' . __( 'The bait file callback endpoint is not accessible. Bait files will not be able to report theft detections.', 'my-trail-camera' ) . '</p>'
                    . '<p>' . esc_html( $response->get_error_message() ) . '</p>',
                'test'        => 'my_trail_camera_rest_api',
            );
        }
        $code = wp_remote_retrieve_response_code( $response );
        // Any response (even 400/403) means the endpoint is reachable.
        if ( $code >= 200 && $code < 500 ) {
            return array(
                'label'       => __( 'My Trail Camera REST API is reachable', 'my-trail-camera' ),
                'status'      => 'good',
                'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'blue' ),
                'description' => '<p>' . __( 'The bait file callback endpoint is accessible and ready to receive detection reports.', 'my-trail-camera' ) . '</p>',
                'test'        => 'my_trail_camera_rest_api',
            );
        }
        return array(
            'label'       => __( 'My Trail Camera REST API returned an error', 'my-trail-camera' ),
            'status'      => 'recommended',
            'badge'       => array( 'label' => __( 'My Trail Camera', 'my-trail-camera' ), 'color' => 'orange' ),
            'description' => '<p>' . sprintf(
                /* translators: %d: HTTP status code */
                __( 'The endpoint returned HTTP %d. This may indicate a server configuration issue.', 'my-trail-camera' ),
                $code
            ) . '</p>',
            'test'        => 'my_trail_camera_rest_api',
        );
    }

    // ========================================================================
    // DASHBOARD WIDGET
    // ========================================================================

    /**
     * Register dashboard widget.
     */
    public function add_dashboard_widget() {
        wp_add_dashboard_widget(
            'mtcam_dashboard_widget',
            __( 'My Trail Camera', 'my-trail-camera' ),
            array($this, 'render_dashboard_widget')
        );
    }

    /**
     * Render dashboard widget content.
     */
    public function render_dashboard_widget() {
        global $wpdb;
        $cameras = mtcam_get_cameras();
        $total = count( $cameras );
        $active = 0;
        $stolen = 0;
        $low_battery = array();

        foreach ( $cameras as $cam ) {
            if ( mtcam_is_stolen( $cam ) ) {
                $stolen++;
            } elseif ( mtcam_is_active( $cam ) ) {
                $active++;
            }
            // Battery warning for deployed cameras with battery_pct field.
            if ( mtcam_is_active( $cam ) && ! mtcam_is_stolen( $cam ) && ! empty( $cam['deploy_date'] ) ) {
                $estimate = function_exists( 'mtcam_estimate_battery_simple' ) ? mtcam_estimate_battery_simple( $cam ) : null;
                if ( $estimate && $estimate['battery_pct'] < 20 ) {
                    $low_battery[] = $cam;
                }
            }
        }

        // Recent detections.
        $det_24h = (int) mtcam_db_get_var(
            "SELECT COUNT(*) FROM `{$wpdb->prefix}mtcam_detections` WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)"
        );
        $det_7d = (int) mtcam_db_get_var(
            "SELECT COUNT(*) FROM `{$wpdb->prefix}mtcam_detections` WHERE time > DATE_SUB(NOW(), INTERVAL 7 DAY)"
        );
        ?>
        <div style="display:flex;gap:16px;margin-bottom:12px;">
            <div style="flex:1;text-align:center;padding:8px;background:#f0f6fc;border-radius:4px;">
                <div style="font-size:24px;font-weight:bold;color:#2271b1;"><?php echo esc_html( $active ); ?></div>
                <div style="font-size:12px;color:#666;"><?php esc_html_e( 'Active', 'my-trail-camera' ); ?></div>
            </div>
            <div style="flex:1;text-align:center;padding:8px;background:<?php echo $stolen > 0 ? '#fcf0f1' : '#f0f6fc'; ?>;border-radius:4px;">
                <div style="font-size:24px;font-weight:bold;color:<?php echo $stolen > 0 ? '#d63638' : '#2271b1'; ?>;"><?php echo esc_html( $stolen ); ?></div>
                <div style="font-size:12px;color:#666;"><?php esc_html_e( 'Stolen', 'my-trail-camera' ); ?></div>
            </div>
            <div style="flex:1;text-align:center;padding:8px;background:<?php echo $det_24h > 0 ? '#fcf0f1' : '#f0f6fc'; ?>;border-radius:4px;">
                <div style="font-size:24px;font-weight:bold;color:<?php echo $det_24h > 0 ? '#d63638' : '#2271b1'; ?>;"><?php echo esc_html( $det_24h ); ?></div>
                <div style="font-size:12px;color:#666;"><?php esc_html_e( 'Detections 24h', 'my-trail-camera' ); ?></div>
            </div>
        </div>
        <?php if ( $det_7d > 0 ) : ?>
            <p style="margin:4px 0;color:#666;font-size:13px;">
                <?php
                printf(
                    /* translators: %d: number of detections */
                    esc_html__( '%d detections in the last 7 days.', 'my-trail-camera' ),
                    intval( $det_7d )
                );
                ?>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-logs' ) ); ?>"><?php esc_html_e( 'View all', 'my-trail-camera' ); ?> &rarr;</a>
            </p>
        <?php endif; ?>
        <?php if ( ! empty( $low_battery ) ) : ?>
            <div style="background:#fff8e5;border-left:4px solid #dba617;padding:8px 12px;margin:8px 0;border-radius:2px;">
                <strong><?php esc_html_e( 'Low battery warning', 'my-trail-camera' ); ?></strong>
                <ul style="margin:4px 0 0 16px;padding:0;">
                <?php foreach ( $low_battery as $cam ) : ?>
                    <li>
                        <?php echo esc_html( $cam['name'] ?: $cam['cam_id'] ); ?>
                        (<?php echo esc_html( round( mtcam_estimate_battery_simple( $cam )['battery_pct'] ) ); ?>%)
                    </li>
                <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        <p style="margin:8px 0 0;">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-dashboard' ) ); ?>"><?php esc_html_e( 'Dashboard', 'my-trail-camera' ); ?></a>
            | <a href="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-add-camera' ) ); ?>"><?php esc_html_e( 'Add Camera', 'my-trail-camera' ); ?></a>
            | <a href="<?php echo esc_url( admin_url( 'admin.php?page=mtcam-settings' ) ); ?>"><?php esc_html_e( 'Settings', 'my-trail-camera' ); ?></a>
        </p>
        <?php
    }
}
