<?php
class Surftrust_Analytics_Controller
{
    /**
     * Records a 'view' event in the analytics table.
     *
     * @param WP_REST_Request $request The request object.
     * @return WP_REST_Response A success or error response.
     */


    /**
     * Records a 'click' event in the analytics table.
     *
     * @param WP_REST_Request $request The request object.
     * @return WP_REST_Response A success or error response.
     */


    /**
     * Helper function to insert an event into the analytics database.
     *
     * @param string $event_type The type of event ('view' or 'click').
     * @param array  $params     The data sent with the request.
     * @return WP_REST_Response A success or error response.
     */

    public function get_analytics_data(WP_REST_Request $request)
    {
        global $wpdb;
        $table_name = esc_sql($wpdb->prefix . 'surftrust_analytics');

        $total_views  = $wpdb->get_var(
            $wpdb->prepare("SELECT COUNT(id) FROM `$table_name` WHERE event_type = %s", 'view')
        );

        $total_clicks = $wpdb->get_var(
            $wpdb->prepare("SELECT COUNT(id) FROM `$table_name` WHERE event_type = %s", 'click')
        );

        $ctr = ($total_views > 0) ? round(($total_clicks / $total_views) * 100, 2) : 0;

        // --- 2. Get Chart Data for the Last 7 Days ---
        $chart_data = [];
        $today = new DateTime();
        $start_date = (new DateTime())->modify('-7 days');



        $daily_stats = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT DATE(timestamp) AS date, event_type, COUNT(id) AS count
         FROM `$table_name`
         WHERE timestamp >= %s
         GROUP BY DATE(timestamp), event_type
         ORDER BY date ASC",
                $start_date->format('Y-m-d H:i:s')
            )
        );

        // Initialize an array with all dates for the last 7 days
        $period = new DatePeriod($start_date, new DateInterval('P1D'), $today->modify('+1 day'));
        $dates = [];
        foreach ($period as $date) {
            $dates[$date->format('Y-m-d')] = ['views' => 0, 'clicks' => 0];
        }

        // Populate the array with our stats from the database
        foreach ($daily_stats as $stat) {
            if (isset($dates[$stat->date])) {
                if ($stat->event_type === 'view') {
                    $dates[$stat->date]['views'] = (int) $stat->count;
                } elseif ($stat->event_type === 'click') {
                    $dates[$stat->date]['clicks'] = (int) $stat->count;
                }
            }
        }

        // Format for Chart.js
        $chart_data = [
            'labels' => array_map(function ($date_str) {
                return gmdate('M j', strtotime($date_str));
            }, array_keys($dates)),
            'views'  => array_column($dates, 'views'),
            'clicks' => array_column($dates, 'clicks'),
        ];

        // --- 3. Final Combined Data ---
        $data = [
            'totals' => [
                'views'  => (int) $total_views,
                'clicks' => (int) $total_clicks,
                'ctr'    => $ctr,
            ],
            'chart' => $chart_data,
        ];

        return new WP_REST_Response($data, 200);
    }
    public function track_view(WP_REST_Request $request)
    {
        $params = $request->get_json_params();
        return $this->insert_analytics_event('view', $params);
    }
    public function track_click(WP_REST_Request $request)
    {
        $params = $request->get_json_params();
        return $this->insert_analytics_event('click', $params);
    }
    private function insert_analytics_event($event_type, $params)
    {
        global $wpdb;

            $table_name = esc_sql($wpdb->prefix . 'surftrust_analytics');

        // Basic validation
        $notification_type = isset($params['notification_type']) ? sanitize_text_field($params['notification_type']) : '';
        $notification_id   = isset($params['notification_id']) ? absint($params['notification_id']) : 0;
        $product_id        = isset($params['product_id']) ? absint($params['product_id']) : 0;

        if (empty($notification_type)) {
            return new WP_REST_Response(['success' => false, 'message' => 'Missing notification type.'], 400);
        }

        $wpdb->insert(
            $table_name,
            [
                'event_type'        => $event_type,
                'notification_type' => $notification_type,
                'notification_id'   => $notification_id,
                'product_id'        => $product_id,
                'timestamp'         => current_time('mysql'),
            ]
        );



        return new WP_REST_Response(['success' => true], 200);
    }
    public function handle_heartbeat(WP_REST_Request $request)
    {
        $cache_key = 'surftrust_live_users';
        $cache_group = 'surftrust';

        // 1. Get or create a unique but anonymous user ID
        $user_id = isset($_COOKIE['surftrust_user_id']) ? sanitize_key($_COOKIE['surftrust_user_id']) : wp_generate_uuid4();

        // 2. Get the current list of users from the object cache
        $live_users = wp_cache_get($cache_key, $cache_group);
        if (! is_array($live_users)) {
            $live_users = [];
        }

        // 3. Update this user's timestamp
        $live_users[$user_id] = time(); // Store the current time

        // 4. Filter out users who haven't sent a heartbeat in the last 3 minutes
        $expiration_time = 3 * MINUTE_IN_SECONDS;
        $active_users = array_filter($live_users, function ($timestamp) use ($expiration_time) {
            return (time() - $timestamp) < $expiration_time;
        });

        // 5. Save the cleaned list back to the cache
        wp_cache_set($cache_key, $active_users, $cache_group, $expiration_time);

        // 6. Set the cookie for the user for the next request
        setcookie('surftrust_user_id', $user_id, time() + (30 * DAY_IN_SECONDS), '/');

        // 7. Return the current count
        return new WP_REST_Response(['count' => count($active_users)], 200);
    }
}
