<?php

class PeepSo3_Search_Analytics {
    const TABLE = 'peepso_search_ranking';
    const AUX_TABLE = 'peepso_search_ranking_totals';
    private $db_version = 2;
    private $table;
    private $aux_table;

    public function __construct() {

        @include_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

        if(!function_exists('dbDelta')) {
            new PeepSoError("dbDelta() not found");
            return;
        }

        // Run dbDelta() once in a while no matter what
        $override = TRUE;(rand(1,100) == 1) ? TRUE : FALSE;

        global $wpdb;
        $this->table = $wpdb->prefix . self::TABLE;
        $this->aux_table = $wpdb->prefix . self::AUX_TABLE;

        $version = PeepSo::PLUGIN_VERSION.PeepSo::PLUGIN_RELEASE.'-'.$this->db_version;
        $charset_collate = $wpdb->get_charset_collate();

        // DB table: peepso_search_ranking
        if(get_option(self::TABLE) != $version || $override) {

            $sql = "CREATE TABLE {$this->table} (
					id BIGINT(20) NOT NULL AUTO_INCREMENT,
					user_id BIGINT(20) NOT NULL,
					search VARCHAR(256),	
					class VARCHAR(64),
					date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
				    PRIMARY KEY (id)
				  ) ENGINE=InnoDB $charset_collate;";

            dbDelta($sql);

            $sql = "CREATE TABLE {$this->aux_table} (
					id BIGINT(20) NOT NULL AUTO_INCREMENT,
					search VARCHAR(256),	
					class VARCHAR(64),
					count BIGINT(20) UNSIGNED DEFAULT '0',
					PRIMARY KEY (id)
				  ) ENGINE=InnoDB $charset_collate;";

            dbDelta($sql);

            update_option(self::TABLE, $version);
        }
    }

    public function store($search, $class, $user_id = null) {

        if(!$user_id) {
            $user_id = (int) get_current_user_id();
        }

        global $wpdb;

        if(!empty($search)) {
            $wpdb->insert($this->table, ['user_id' => $user_id, 'search' => $search, 'class'=>$class]);
            $this->update_total($search, $class);
        }
    }

    private function update_total($search, $class) {
        global $wpdb;

        // $search is the raw visitor-supplied search term, reaching here from the public
        // search endpoint — every statement below must be prepared.

        // Insert the search-class pair if it's missing
        $sql = $wpdb->prepare("SELECT id FROM {$this->aux_table} WHERE `search`=%s AND `class`=%s", $search, $class);
        $res = $wpdb->get_results($sql, ARRAY_A);

        if(!is_array($res) || !count($res)) {
            $sql = $wpdb->prepare("INSERT IGNORE INTO {$this->aux_table} SET `search`=%s, `class`=%s", $search, $class);
            $wpdb->query($sql);
        }

        // Update total
        $total = 0;
        $sql = $wpdb->prepare("SELECT count(id) as total FROM {$this->table} WHERE search=%s AND class=%s", $search, $class);
        $res = $wpdb->get_results($sql, ARRAY_A);

        if(is_array($res) && count($res)) {
            $total = max(0, intval($res[0]['total']));
        }

        $sql = $wpdb->prepare("UPDATE {$this->aux_table} SET `count`=%d WHERE `search`=%s AND `class`=%s", $total, $search, $class);
        $wpdb->query($sql);
    }

    public function get_recent_searches($limit = 10, $user_id=null) {
        if(!$user_id) {
            $user_id = (int) get_current_user_id();
        }
        if (!$user_id) {
            error_log("no uid");
            return NULL;
        }

        global $wpdb;

        // Sanitize the limit to ensure it's a valid number
        $limit = max(1, (int) $limit);

        // Query to get unique recent searches for the user
        $sql = $wpdb->prepare(
            "SELECT DISTINCT search, MAX(id) as max_id 
            FROM {$this->table} 
            WHERE user_id = %d 
            GROUP BY search 
            ORDER BY max_id DESC 
            LIMIT %d",
            $user_id,
            $limit
        );
        error_log($sql);

        $results = $wpdb->get_results($sql, ARRAY_A);

        // Convert results into a flat array of search terms
        $flat_results = array_map(function($row) {
            return $row['search'];
        }, $results);

        return $flat_results;
    }


}
