<?php

namespace PixelioCartAbandonRecoveryMore\Frontend\Repositories;


use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

class Pixelio_Cart_Abandon_Recovery_More_Session_Repository
{

	private $wpdb;
	private string $session_data;
	private string $anonymous_events;
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;

	public function __construct($pixelio_wordpress_wrapper)
	{
		$this->wpdb = $pixelio_wordpress_wrapper->pixelio_get_wpdb();
		$this->pixelio_wordpress_wrapper = $pixelio_wordpress_wrapper;
		$this->session_data = $this->wpdb->prefix . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_db_prefix . 'session_data';
		$this->anonymous_events = $this->wpdb->prefix . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_db_prefix . 'anonymous_events';
	}

	public function pixelio_cart_abandon_recovery_more_add_session_data(
		string $uuid,
		?string $page_url,
		int $product_id = 0,
		int $parent_product_id = 0,
		string $event_type = 'page_view',
		int $visit_duration = 0,
		int $scroll_percent = 0,
		string $tracking_mode = 'unknown'
	): void {
		global $wpdb;

		$wpdb->insert(
			$this->session_data,
			[
				'uuid' => $uuid,
				'event_type' => $event_type,
				'woo_product_id' => $product_id,
				'parent_woo_product_id' => $parent_product_id,
				'page_url' => $page_url,
				'visit_duration' => $visit_duration,
				'scroll_percent' => $scroll_percent,
				'tracking_mode' => $tracking_mode,
				'at' => current_time('mysql'),
				'synced' => 0,
			],
			[
				'%s',
				'%s',
				'%d',
				'%d',
				'%s',
				'%d',
				'%d',
				'%s',
				'%d',
			]
		);
	}

	public function pixelio_increment_anonymous_event(
		string $event_type,
		int $product_id = 0,
		string $page_type = 'unknown'
	): void {
		global $wpdb;

		$date = current_time('Y-m-d');
		$hour = (int) current_time('H');

		$wpdb->query(
			$wpdb->prepare(
				"INSERT INTO {$this->anonymous_events}
			(event_type, woo_product_id, page_type, event_date, event_hour, counter, synced)
		VALUES
			(%s, %d, %s, %s, %d, 1, 0)
		ON DUPLICATE KEY UPDATE
			counter = counter + 1,
			synced = 0",
				$event_type,
				$product_id,
				$page_type,
				$date,
				$hour
			)
		);
	}

	public function pixelio_get_anonymous_event_not_yet_synced(): array
	{
		return $this->wpdb->get_results(
			"SELECT * FROM {$this->anonymous_events} WHERE synced = 0"
		);
	}

	public function pixelio_mark_anonymous_event_synced(array $events): bool
	{
		foreach ($events as $event) {
			if (!isset($event->id)) {
				continue;
			}

			$result = $this->wpdb->update(
				$this->anonymous_events,
				['synced' => 1],
				['id' => (int) $event->id],
				['%d'],
				['%d']
			);

			if ($result === false) {
				return false;
			}
		}

		return true;
	}
	public function pixelio_get_session_data_not_yet_synced(): array
	{
		return $this->wpdb->get_results(
			"SELECT * FROM {$this->session_data} WHERE synced = 0"
		);
	}

	public function pixelio_mark_session_data_synced(array $events): bool
	{
		foreach ($events as $event) {
			if (!isset($event->id)) {
				continue;
			}

			$result = $this->wpdb->update(
				$this->session_data,
				['synced' => 1],
				['id' => (int) $event->id],
				['%d'],
				['%d']
			);

			if ($result === false) {
				return false;
			}
		}

		return true;
	}
}

