<?php

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

class AVIN_Installer {
	public static function activate() {
		self::create_tables();
		self::seed_options();
		update_option( 'avin_version', AVIN_VERSION, false );
		update_option( 'avin_db_version', AVIN_DB_VERSION, false );
	}

	public static function deactivate() {
		wp_clear_scheduled_hook( 'avin_daily_maintenance' );
	}

	public static function create_tables() {
		global $wpdb;

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

		$charset        = $wpdb->get_charset_collate();
		$totals         = $wpdb->prefix . 'avin_view_totals';
		$daily          = $wpdb->prefix . 'avin_view_daily';
		$traffic_daily  = $wpdb->prefix . 'avin_traffic_daily';
		$device_daily   = $wpdb->prefix . 'avin_device_daily';
		$source_daily   = $wpdb->prefix . 'avin_source_daily';
		$social_daily   = $wpdb->prefix . 'avin_social_daily';
		$country_daily  = $wpdb->prefix . 'avin_country_daily';
		$daily_visitors = $wpdb->prefix . 'avin_daily_visitors';
		$daily_sessions = $wpdb->prefix . 'avin_daily_sessions';
		$active         = $wpdb->prefix . 'avin_active_sessions';

		dbDelta(
			"CREATE TABLE {$totals} (
				object_id BIGINT UNSIGNED NOT NULL,
				object_type VARCHAR(32) NOT NULL,
				total_views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				last_viewed_at DATETIME NULL,
				PRIMARY KEY  (object_id),
				KEY object_type_total (object_type, total_views),
				KEY last_viewed_at (last_viewed_at)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$daily} (
				view_date DATE NOT NULL,
				object_id BIGINT UNSIGNED NOT NULL,
				object_type VARCHAR(32) NOT NULL,
				views INT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date, object_id),
				KEY object_type_date_views (object_type, view_date, views),
				KEY object_id_date (object_id, view_date)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$traffic_daily} (
				view_date DATE NOT NULL,
				unique_visitors BIGINT UNSIGNED NOT NULL DEFAULT 0,
				sessions BIGINT UNSIGNED NOT NULL DEFAULT 0,
				page_views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				product_views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				add_to_cart BIGINT UNSIGNED NOT NULL DEFAULT 0,
				checkout_starts BIGINT UNSIGNED NOT NULL DEFAULT 0,
				orders BIGINT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$device_daily} (
				view_date DATE NOT NULL,
				device_type VARCHAR(20) NOT NULL,
				visitors BIGINT UNSIGNED NOT NULL DEFAULT 0,
				sessions BIGINT UNSIGNED NOT NULL DEFAULT 0,
				page_views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date, device_type),
				KEY device_type (device_type)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$source_daily} (
				view_date DATE NOT NULL,
				source_type VARCHAR(40) NOT NULL,
				visits BIGINT UNSIGNED NOT NULL DEFAULT 0,
				views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				conversions BIGINT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date, source_type),
				KEY source_type (source_type)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$social_daily} (
				view_date DATE NOT NULL,
				social_source VARCHAR(40) NOT NULL,
				visits BIGINT UNSIGNED NOT NULL DEFAULT 0,
				views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				conversions BIGINT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date, social_source),
				KEY social_source (social_source)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$country_daily} (
				view_date DATE NOT NULL,
				country_code VARCHAR(2) NOT NULL,
				country_name VARCHAR(80) NOT NULL,
				visitors BIGINT UNSIGNED NOT NULL DEFAULT 0,
				views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				PRIMARY KEY  (view_date, country_code),
				KEY country_name (country_name)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$daily_visitors} (
				view_date DATE NOT NULL,
				visitor_hash CHAR(64) NOT NULL,
				created_at DATETIME NOT NULL,
				PRIMARY KEY  (view_date, visitor_hash)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$daily_sessions} (
				view_date DATE NOT NULL,
				session_hash CHAR(64) NOT NULL,
				visitor_hash CHAR(64) NOT NULL,
				device_type VARCHAR(20) NOT NULL,
				source_type VARCHAR(40) NOT NULL,
				social_source VARCHAR(40) NOT NULL DEFAULT '',
				country_code VARCHAR(2) NOT NULL DEFAULT 'ZZ',
				started_at DATETIME NOT NULL,
				last_seen_at DATETIME NOT NULL,
				PRIMARY KEY  (view_date, session_hash),
				KEY visitor_hash (visitor_hash),
				KEY last_seen_at (last_seen_at)
			) {$charset};"
		);

		dbDelta(
			"CREATE TABLE {$active} (
				session_hash CHAR(64) NOT NULL,
				visitor_hash CHAR(64) NOT NULL,
				last_seen_at DATETIME NOT NULL,
				current_object_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
				current_object_type VARCHAR(32) NOT NULL DEFAULT '',
				current_title VARCHAR(255) NOT NULL DEFAULT '',
				current_url VARCHAR(255) NOT NULL DEFAULT '',
				page_views BIGINT UNSIGNED NOT NULL DEFAULT 0,
				device_type VARCHAR(20) NOT NULL DEFAULT 'unknown',
				source_type VARCHAR(40) NOT NULL DEFAULT 'unknown',
				social_source VARCHAR(40) NOT NULL DEFAULT '',
				country_code VARCHAR(2) NOT NULL DEFAULT 'ZZ',
				country_name VARCHAR(80) NOT NULL DEFAULT 'Unknown',
				PRIMARY KEY  (session_hash),
				KEY last_seen_at (last_seen_at),
				KEY current_object (current_object_type, current_object_id)
			) {$charset};"
		);
	}

	public static function seed_options() {
		add_option( 'avin_tracked_post_types', array( 'post', 'page', 'product' ), '', false );
		add_option( 'avin_exclude_admins', '1', '', false );
		add_option( 'avin_exclude_logged_in', '0', '', false );
		add_option( 'avin_bot_filtering', '1', '', false );
		add_option( 'avin_tracking_mode', 'beacon', '', false );
		add_option( 'avin_retention_days', 730, '', false );
		add_option( 'avin_dashboard_theme', 'dark', '', false );

		if ( ! wp_next_scheduled( 'avin_daily_maintenance' ) ) {
			wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'avin_daily_maintenance' );
		}
	}
}
