<?php
/**
 * Activation logic and schema creation.
 *
 * @package Live_Traffic_Analytics
 */

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

/**
 * Handles plugin activation tasks.
 */
class WPLTA_Activator {

	/**
	 * Create plugin tables.
	 *
	 * @return void
	 */
	public static function activate() {
		global $wpdb;

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$charset_collate = $wpdb->get_charset_collate();
		$activity_table  = WPLTA_Repository::get_activity_table_name();
		$sessions_table  = WPLTA_Repository::get_sessions_table_name();
		$page_stats      = WPLTA_Repository::get_page_stats_table_name();

			$sql = array(
			"CREATE TABLE {$activity_table} (
				id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				session_id VARCHAR(64) NOT NULL,
				page_view_id VARCHAR(64) NOT NULL,
				ip_address VARCHAR(100) NOT NULL,
				country VARCHAR(100) NOT NULL DEFAULT '',
				latitude DECIMAL(10,6) NULL DEFAULT NULL,
				longitude DECIMAL(10,6) NULL DEFAULT NULL,
				page_url TEXT NOT NULL,
				page_url_hash CHAR(32) NOT NULL,
				page_title VARCHAR(255) NOT NULL DEFAULT '',
				referrer TEXT NULL,
				referrer_host VARCHAR(191) NOT NULL DEFAULT '',
				device_type VARCHAR(20) NOT NULL DEFAULT 'desktop',
				browser VARCHAR(60) NOT NULL DEFAULT 'Unknown',
				user_agent TEXT NULL,
				is_bot TINYINT(1) NOT NULL DEFAULT 0,
				bot_name VARCHAR(100) NOT NULL DEFAULT '',
				time_on_page INT(10) UNSIGNED NOT NULL DEFAULT 0,
				page_entered_at DATETIME NOT NULL,
				last_seen_at DATETIME NOT NULL,
				is_active TINYINT(1) NOT NULL DEFAULT 1,
				created_at DATETIME NOT NULL,
				updated_at DATETIME NOT NULL,
				PRIMARY KEY  (id),
				UNIQUE KEY page_view (page_view_id),
				KEY session_id (session_id),
				KEY is_active_last_seen (is_active, last_seen_at),
				KEY live_map (is_active, latitude, longitude),
				KEY bot_activity (is_bot, last_seen_at),
				KEY page_url_hash (page_url_hash),
				KEY referrer_host (referrer_host)
			) {$charset_collate};",
			"CREATE TABLE {$sessions_table} (
				id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				session_id VARCHAR(64) NOT NULL,
				ip_address VARCHAR(100) NOT NULL,
				country VARCHAR(100) NOT NULL DEFAULT '',
				device_type VARCHAR(20) NOT NULL DEFAULT 'desktop',
				browser VARCHAR(60) NOT NULL DEFAULT 'Unknown',
				entry_page TEXT NOT NULL,
				current_page TEXT NOT NULL,
				referrer TEXT NULL,
				referrer_host VARCHAR(191) NOT NULL DEFAULT '',
				page_count INT(10) UNSIGNED NOT NULL DEFAULT 1,
				journey_path LONGTEXT NOT NULL,
				started_at DATETIME NOT NULL,
				last_activity_at DATETIME NOT NULL,
				created_at DATETIME NOT NULL,
				updated_at DATETIME NOT NULL,
				PRIMARY KEY  (id),
				UNIQUE KEY session_id (session_id),
				KEY last_activity_at (last_activity_at),
				KEY referrer_host (referrer_host)
			) {$charset_collate};",
			"CREATE TABLE {$page_stats} (
				id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				page_url TEXT NOT NULL,
				page_url_hash CHAR(32) NOT NULL,
				page_title VARCHAR(255) NOT NULL DEFAULT '',
				total_views BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
				last_visited_at DATETIME NOT NULL,
				created_at DATETIME NOT NULL,
				updated_at DATETIME NOT NULL,
				PRIMARY KEY  (id),
				UNIQUE KEY page_url_hash (page_url_hash),
				KEY total_views (total_views),
				KEY last_visited_at (last_visited_at)
			) {$charset_collate};",
		);

		foreach ( $sql as $statement ) {
			dbDelta( $statement );
		}

		update_option( 'wplta_db_version', WPLTA_VERSION );
	}

	/**
	 * Upgrade schema when plugin version changes.
	 *
	 * @return void
	 */
	public static function maybe_upgrade() {
		$current_version = get_option( 'wplta_db_version' );

		if ( WPLTA_VERSION !== $current_version ) {
			self::activate();
		}
	}
}
